alexjercan/bugnet huggingface dataset:
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int r, c;
vector<vector<int>> field;

int search() {
  int counter;
  int max_num = 0;

  counter = 1 << r;
  for (int i = 0; i < counter; i++) {
    int num = 0;

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }

    for (int j = 0; j < c; j++) {
      int co_num = 0;
      for (int k = 0; k < r; k++) {
        co_num += field[k][j];
      }

      if (co_num <= r / 2) {
        num += r - co_num;
      } else {
        num += co_num; /*reverse*/
      }
    }

    if (max_num < num) {
      max_num = num;
    }

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }
  }

  return max_num;
}

int main(int argc, char **argv) {
  cin >> r >> c;

  while (r != 0 && c != 0) {
    field.clear();
    for (int i = 0; i < r; i++) {
      field.push_back(vector<int>(c));
      for (int j = 0; j < c; j++) {
        cin >> field[i][j];
      }
    }
    cout << search() << endl;
    cin >> r >> c;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int r, c;
vector<vector<int>> field;

int search() {
  int counter;
  int max_num = 0;

  counter = 1 << r;
  for (int i = 0; i < counter; i++) {
    int num = 0;

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }

    for (int j = 0; j < c; j++) {
      int co_num = 0;
      for (int k = 0; k < r; k++) {
        co_num += field[k][j];
      }

      if (co_num <= r / 2) {
        num += r - co_num;
      } else {
        num += co_num; /*reverse*/
      }
    }

    if (max_num < num) {
      max_num = num;
    }

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }
  }

  return max_num;
}

int main(int argc, char **argv) {
  cin >> r >> c;

  while (r != 0 && c != 0) {
    for (int i = 0; i < r; i++) {
      field.push_back(vector<int>(c));
      for (int j = 0; j < c; j++) {
        cin >> field[i][j];
      }
    }
    cout << search() << endl;
    cin >> r >> c;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
int m, n;
bool t[22][11000];
int dp[2000][11000];
int main() {
  while (cin >> m >> n, n || m) {
    rep(i, 2000) rep(j, 10000) dp[i][j] = 0;
    rep(i, 22) rep(j, 10000) t[i][j] = 0;
    rep(i, m) { rep(j, n) cin >> t[i][j]; }

    int ans = 0, cnt = 0;
    rep(i, (1 << m)) {

      cnt = 0;
      rep(j, n) {
        rep(k, m) {
          if (i & (1 << k) && t[k][j])
            dp[i][j]++;
          if ((!(i & (1 << k))) && !t[k][j])
            dp[i][j]++;
        }
      }
      rep(j, n) cnt += max(dp[i][j], m - dp[i][j]);
      ans = max(cnt, ans);
    }

    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
int m, n;
bool t[22][11000];
int dp[22][11000];
int main() {
  while (cin >> m >> n, n || m) {
    rep(i, 11) rep(j, 10000) dp[i][j] = t[i][j] = 0;
    rep(i, m) { rep(j, n) cin >> t[i][j]; }

    int ans = 0, cnt = 0;
    rep(i, (1 << m)) {

      cnt = 0;
      rep(j, n) {
        rep(k, m) {
          if (i & (1 << k) && t[k][j])
            dp[i][j]++;
          if ((!(i & (1 << k))) && !t[k][j])
            dp[i][j]++;
        }
      }
      rep(j, n) cnt += max(dp[i][j], m - dp[i][j]);
      ans = max(cnt, ans);
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

bool flg(int x, int y) {
  if (x & (1 << y))
    return true;
  else
    return false;
}

int main() {
  int n, m;
  while (cin >> n >> m, n, m) {
    int s[12][10010];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> s[i][j];
    }

    int bit = 1 << n;
    int ans = 0;
    for (int i = 0; i < bit; i++) {
      int ka[10020] = {};
      for (int j = 0; j < n; j++) {
        if (flg(i, j)) {
          for (int k = 0; k < m; k++) {
            if (s[j][k] == 0)
              ka[k]++;
          }
        } else {
          for (int k = 0; k < m; k++) {
            if (s[j][k] == 1)
              ka[k]++;
          }
        }
      }
      int a = 0;
      for (int l = 0; l < m; l++) {
        a += max(ka[l], n - ka[l]);
      }
      ans = max(ans, a);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

bool flg(int x, int y) {
  if (x & (1 << y))
    return true;
  else
    return false;
}

int main() {
  int n, m;
  while (cin >> n >> m, n, m) {
    bool s[12][10010];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> s[i][j];
    }

    int bit = 1 << n;
    int ans = 0;
    for (int i = 0; i < bit; i++) {
      for (int j = 0; j < n; j++) {
        if (flg(i, j)) {
          for (int k = 0; k < m; k++) {
            s[j][k] = !s[j][k];
          }
        }
        int a = 0;
        for (int k = 0; k < m; k++) {
          int b = 0, w = 0;
          ;
          for (int l = 0; l < n; l++) {
            if (s[l][k])
              b++;
            else
              w++;
          }
          a += max(b, w);
        }
        if (a > ans)
          ans = a;
        if (flg(i, j)) {
          for (int k = 0; k < m; k++) {
            s[j][k] = !s[j][k];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;
static const int INF = 1 << 27;
static const int NIL = -1;
int dijkstra(int n, int from, int to, int dist[][100]) {
  int d[100];
  bool used[100];
  fill(d, d + n, INF);
  fill(used, used + n, false);
  d[from] = 0;
  for (;;) {
    int v = NIL;
    for (int u = 0; u < n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == NIL)
      break;
    used[v] = true;
    for (int u = 0; u < n; u++) {
      d[u] = min(d[u], d[v] + dist[v][u]);
    }
  }
  if (d[to] == INF)
    return -1;
  else
    return d[to];
}
int main() {
  for (;;) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    int dist[100][100];
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        if (i == j)
          dist[i][j] = 0;
        else
          dist[i][j] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int def;
      cin >> def;
      if (def == 0) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        cout << dijkstra(n, a, b, dist) << endl;
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        if (e < dist[c][d] && e < dist[d][c]) {
          dist[c][d] = e;
          dist[d][c] = e;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
using namespace std;
static const int INF = 1 << 27;
int warshall_floyd(int n, int u, int v, int dist[][100]) {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  if (dist[u][v] == INF)
    return -1;
  else
    return dist[u][v];
}
int main() {
  for (;;) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    int dist[100][100];
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        if (i == j)
          dist[i][j] = 0;
        else
          dist[i][j] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int def;
      cin >> def;
      if (def == 0) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        cout << warshall_floyd(n, a, b, dist) << endl;
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        if (e < dist[c][d] && e < dist[d][c]) {
          dist[c][d] = e;
          dist[d][c] = e;
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b;
  while (cin >> a >> b, a | b) {
    int c[100][100];
    for (int x = 0; x < a; x++) {
      for (int y = 0; y < a; y++) {
        c[x][y] = 1 << 29;
      }
    }
    for (int e = 0; e < b; e++) {
      int f;
      cin >> f;
      if (f == 0) {
        int g, h;
        cin >> g >> h;
        g--;
        h--;
        if (c[g][h] != 1 << 29)
          cout << c[g][h] << endl;
        else
          cout << -1 << endl;
      } else {
        int g, h, i;
        cin >> g >> h >> i;
        g--;
        h--;
        if (c[g][h] > i) {
          c[g][h] = c[h][g] = i;
          for (int j = 0; j < a; j++) {
            for (int k = 0; k < a; k++) {
              for (int l = 0; l < a; l++) {
                if (k == l)
                  c[k][l] = 0;
                else
                  c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
              }
            }
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b;
  while (cin >> a >> b, a | b) {
    int c[100][100];
    for (int x = 0; x < a; x++) {
      for (int y = 0; y < a; y++) {
        c[x][y] = 1 << 29;
      }
    }
    for (int e = 0; e < b; e++) {
      int f;
      cin >> f;
      if (f == 0) {
        int g, h;
        cin >> g >> h;
        g--;
        h--;
        if (c[g][h] != 1 << 29)
          cout << c[g][h] << endl;
        else
          cout << -1 << endl;
      } else {
        int g, h, i;
        cin >> g >> h >> i;
        g--;
        h--;
        c[g][h] = c[h][g] = i;
        for (int j = 0; j < a; j++) {
          for (int k = 0; k < a; k++) {
            for (int l = 0; l < a; l++) {
              if (k == l)
                c[k][l] = 0;
              else
                c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
            }
          }
        }
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const long long INF = 1000000000;
struct P {
  long long cost, pos;
};
bool operator<(const P &a, const P &b) { return a.cost < b.cost; }
bool operator>(const P &a, const P &b) { return a.cost > b.cost; }
struct A {
  long long to, cost;
};
int main() {
  int n, k;
  while (cin >> n >> k && n != 0 && k != 0) {
    vector<A> a[101];
    for (int i = 0; i < k; i++) {
      int s;
      cin >> s;
      if (s == 0) {
        long long m, t;
        cin >> m >> t;
        long long b[101];
        for (int i = 1; i <= n; i++)
          b[i] = INF;
        priority_queue<P, vector<P>, greater<P>> p;

        b[m] = 0;
        p.push(P{0LL, m});
        while (!p.empty()) {
          P r = p.top();
          p.pop();
          int o = r.pos;
          for (int i = 0; i < (int)a[o].size(); i++) {
            if (b[a[o][i].to] > b[o] + a[o][i].cost) {
              b[a[o][i].to] = b[o] + a[o][i].cost;
              p.push(P{b[a[o][i].to], a[o][i].to});
            }
          }
        }
        if (b[t] == INF)
          cout << -1 << endl;
        else
          cout << b[t] << endl;
      } else {
        int moto, to, cost;
        cin >> moto >> to >> cost;
        A q;
        q.to = to;
        q.cost = cost;
        a[moto].push_back(q);
        q.to = moto;
        a[to].push_back(q);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const long long INF = 1000000;
struct P {
  long long cost, moto, to;
};
bool operator<(P a, P b) { return a.cost <= b.cost; }
struct A {
  long long to, cost;
};
int main() {
  int n, k;
  while (cin >> n >> k && n != 0 && k != 0) {
    vector<A> a[101];
    for (int i = 0; i < k; i++) {
      int s;
      cin >> s;
      if (s == 0) {
        int m, t;
        cin >> m >> t;
        priority_queue<P> p;
        for (int i = 0; i < a[m].size(); i++) {
          P simi;
          simi.to = a[m][i].to;
          simi.cost = a[m][i].cost;
          simi.moto = m;
          p.push(simi);
        }
        long long b[101];
        for (int i = 0; i <= n; i++)
          b[i] = INF;
        b[m] = 0;
        while (!p.empty()) {
          P simi = p.top();
          p.pop();
          if (b[simi.to] > b[simi.moto] + simi.cost) {
            b[simi.to] = b[simi.moto] + simi.cost;
            for (int i = 0; i < a[simi.to].size(); i++) {
              P q;
              q.to = a[simi.to][i].to;
              q.cost = a[simi.to][i].cost;
              q.moto = simi.to;
              p.push(q);
            }
          }
        }
        if (b[t] == INF)
          cout << -1 << endl;
        else
          cout << b[t] << endl;
      } else {
        int moto, to, cost;
        cin >> moto >> to >> cost;
        A q;
        q.to = to;
        q.cost = cost;
        a[moto].push_back(q);
        q.to = moto;
        a[to].push_back(q);
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int i, j;
  int a[100][100], b[100], c[100], m, n;
  int mn;
  while (cin >> n >> m && n + m) {
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        a[i][j] = 200000000;
    for (i = 0; i < m; i++) {
      int p, q, r;
      cin >> p;
      if (p) {
        cin >> p >> q >> r;
        p--;
        q--;
        a[p][q] = a[q][p] = min(a[p][q], r);
      } else {
        cin >> p >> q;
        p--;
        q--;
        memset(b, -1, sizeof(b));
        b[p] = 1;
        c[p] = 0;
        for (j = 0; j < n; j++) {
          if (j != p)
            c[j] = c[p] + a[p][j];
        }
        for (;;) {
          mn = q;
          for (j = 0; j < n; j++) {
            if (b[j] == -1 && c[mn] > c[j])
              mn = j;
          }
          if (mn == q)
            break;
          b[mn] = 1;
          for (j = 0; j < n; j++)
            c[j] = min(c[j], c[mn] + a[mn][j]);
        }
        if (c[q] == 200000000)
          cout << -1 << endl;
        else
          cout << c[q] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
void f(int a[][100], int b[], int p, int e, int n, int m) {
  int i;
  if (p == e) {
    if (b[p] == -1 || b[p] > m)
      b[p] = m;
  } else if (b[p] == -1 || b[p] > m) {
    b[p] = m;
    for (i = 0; i < n; i++) {
      if (a[p][i] != -1)
        f(a, b, i, e, n, m + a[p][i]);
    }
  }
  return;
}
int main() {
  int i;
  int a[100][100], b[100], m, n;
  while (cin >> n >> m && n + m) {
    memset(a, -1, sizeof(a));
    for (i = 0; i < m; i++) {
      int t, u, v;
      cin >> t;
      if (t) {
        cin >> t >> u >> v;
        t--;
        u--;
        if (a[t][u] == -1 || a[t][u] > v) {
          a[t][u] = v;
          a[u][t] = v;
        }
      } else {
        cin >> t >> u;
        t--;
        u--;
        memset(b, -1, sizeof(b));
        f(a, b, t, u, n, 0);
        cout << b[u] << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000000;
int warshall_floyd(int a, int b, vector<vector<int>> &vi, int V);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  while (cin >> n >> k && n != 0 && k != 0) {
    vector<vector<int>> wf(n + 1, vector<int>(n + 1));
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < n + 1; ++j) {
        if (i == j) {
          wf[i][j] = 0;
        } else {
          wf[i][j] = INF;
        }
      }
    }
    for (int i = 0; i < k; ++i) {
      int s;
      cin >> s;
      if (s == 0) {
        int a, b;
        cin >> a >> b;
        if (wf[a][b] < INF) {
          cout << wf[a][b] << "\\n";
        } else {
          cout << -1 << "\\n";
        }
      } else if (s == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        if (e < wf[c][d]) {
          wf[c][d] = e;
          wf[d][c] = e;
          warshall_floyd(1, 1, wf, n);
        }
      }
    }
  }
  return 0;
}

int warshall_floyd(int a, int b, vector<vector<int>> &vi, int V) {
  for (int k = 1; k <= V; ++k) {
    for (int i = 1; i <= V; ++i) {
      for (int j = 1; j <= V; ++j)
        vi[i][j] = min(vi[i][j], vi[i][k] + vi[k][j]);
    }
  }
  return vi[a][b];
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000000;
int warshall_floyd(int a, int b, vector<vector<int>> &vi, int V);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  while (cin >> n >> k && n != 0 && k != 0) {
    vector<vector<int>> wf(n + 1, vector<int>(n + 1));
    for (int i = 1; i < n + 1; ++i) {
      for (int j = 1; j < n + 1; ++j) {
        if (i == j) {
          wf[i][j] = 0;
        } else {
          wf[i][j] = INF;
        }
      }
    }
    for (int i = 0; i < k; ++i) {
      int s;
      cin >> s;
      if (s == 0) {
        int a, b;
        cin >> a >> b;
        if (warshall_floyd(a, b, wf, n) < INF) {
          cout << warshall_floyd(a, b, wf, n) << "\\n";
        } else {
          cout << -1 << "\\n";
        }
      } else if (s == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        if (e < wf[c][d]) {
          wf[c][d] = e;
          wf[d][c] = e;
        }
      }
    }
  }
  return 0;
}

int warshall_floyd(int a, int b, vector<vector<int>> &vi, int V) {
  for (int k = 1; k <= V; ++k) {
    for (int i = 1; i <= V; ++i) {
      for (int j = 1; j <= V; ++j)
        vi[i][j] = min(vi[i][j], vi[i][k] + vi[k][j]);
    }
  }
  return vi[a][b];
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int n, kk;
int dst[100][100];
int M = 1001001001;

void wf() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (dst[i][k] + dst[k][j] < dst[i][j]) {
          dst[i][j] = dst[i][k] + dst[k][j];
        }
      }
    }
  }
}

void addEdge(int a, int b, int c) {
  dst[a][b] = min(dst[a][b], c);
  dst[b][a] = min(dst[b][a], c);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dst[i][a] + c + dst[b][j] < dst[i][j]) {
        dst[i][j] = dst[i][a] + c + dst[b][j];
      }
      if (dst[i][b] + c + dst[a][j] < dst[i][j]) {
        dst[i][j] = dst[i][b] + c + dst[a][j];
      }
    }
  }
}

int main() {

  for (;;) {
    cin >> n >> kk;
    if (n == 0 && kk == 0)
      break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dst[i][j] = M;
        if (i == j)
          dst[i][j] = 0;
      }
    }

    int f, a, b, c, d, e;
    for (int i = 0; i < kk; i++) {
      cin >> f;
      if (f == 0) {
        cin >> a >> b;
        a--;
        b--;
        if (dst[a][b] == M) {
          cout << "-1" << endl;
        } else {
          cout << dst[a][b] << endl;
        }

      } else {
        cin >> c >> d >> e;
        c--;
        d--;
        addEdge(c, d, e);
        //				dst[c][d] = e;
        //				dst[d][c] = e;
        //				wf();
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, kk;
int dst[100][100];
int M = 1001001001;

void wf() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (dst[i][k] + dst[k][j] < dst[i][j]) {
          dst[i][j] = dst[i][k] + dst[k][j];
        }
      }
    }
  }
}

int main() {

  for (;;) {
    cin >> n >> kk;
    if (n == 0 && kk == 0)
      break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dst[i][j] = M;
        if (i == j)
          dst[i][j] = 0;
      }
    }

    int f, a, b, c, d, e;
    for (int i = 0; i < kk; i++) {
      cin >> f;
      if (f == 0) {
        cin >> a >> b;
        a--;
        b--;
        if (dst[a][b] == M) {
          cout << "-1" << endl;
        } else {
          cout << dst[a][b] << endl;
        }

      } else {
        cin >> c >> d >> e;
        c--;
        d--;
        dst[c][d] = e;
        dst[d][c] = e;
        wf();
      }
    }
  }
}