alexjercan/bugnet huggingface dataset:
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int, int> P;
int main(void) {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    int rin[n + 1][n + 1];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        rin[i][j] = M;
    for (int i = 0; i < m; i++) {
      int a, c, d, e;
      cin >> a;
      if (a == 1) {
        cin >> c >> d >> e;
        if (rin[c][d] > e)
          rin[c][d] = e;
        if (rin[d][c] > e)
          rin[d][c] = e;
      } else {
        cin >> c >> d;
        int leng[n + 1], f[n + 1];
        fill(leng, leng + n + 1, M);
        fill(f, f + n + 1, 0);
        leng[c] = 0;
        for (int l = 0; l < n; l++) {
          int mi = M, v;
          for (int j = 1; j <= n; j++) {
            if (f[j] == 0 && mi > leng[j]) {
              mi = leng[j];
              v = j;
            }
          }
          f[v] = 1;
          for (int j = 1; j <= n; j++) {
            int x = rin[v][j];
            if (x != M) {
              if (leng[j] > (leng[v] + x))
                leng[j] = leng[v] + x;
            }
          }
        }
        if (leng[d] != M)
          cout << leng[d] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M 100000007
typedef pair<int, int> P;
int main(void) {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    int rin[n + 1][n + 1];
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        rin[i][j] = M;
    for (int i = 0; i < m; i++) {
      int a, c, d, e;
      cin >> a;
      if (a == 1) {
        cin >> c >> d >> e;
        if (rin[c][d] > e)
          rin[c][d] = e;
        if (rin[d][c] > e)
          rin[d][c] = e;
      } else {
        cin >> c >> d;
        int leng[n + 1];
        priority_queue<P> que;
        fill(leng, leng + n + 1, M);
        leng[c] = 0;
        que.push(P(0, c));
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          int v = p.second;
          if (leng[v] < p.first)
            continue;
          for (int j = 1; j <= n; j++) {
            int x = rin[v][j];
            if (x != M) {
              if (leng[j] > leng[v] + x) {
                leng[j] = leng[v] + x;
                que.push(P(leng[j], j));
              }
            }
          }
        }
        if (leng[d] != M)
          cout << leng[d] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = numeric_limits<int>::max() / 3;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0)
      break;
    int dis[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        dis[i][j] = INF;
        if (i == j)
          dis[i][j] = 0;
      }
    for (int i = 0; i < k; i++) {
      int p;
      cin >> p;
      if (p) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        dis[c][d] = min(e, dis[c][d]);
        dis[d][c] = min(e, dis[d][c]);
        for (int s = 0; s < n; s++) {
          for (int g = 0; g < n; g++) {
            dis[s][g] = min(dis[s][g], e + min(dis[s][c] + dis[d][g],
                                               dis[s][d] + dis[c][g]));
          }
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (dis[a][b] == INF)
          cout << -1 << endl;
        else
          cout << dis[a][b] << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = numeric_limits<int>::max() / 2;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int n, k;
  while (true) {
    // cin>>n>>k;
    scanf("%d %d", &n, &k);
    if (n == 0)
      break;
    int dis[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        dis[i][j] = INF;
        if (i == j)
          dis[i][j] = 0;
      }
    for (int i = 0; i < k; i++) {
      int p;
      scanf("%d", &p);
      if (p) {
        int c, d, e; // cin>>c>>d>>e;//
        scanf("%d %d %d", &c, &d, &e);
        c--;
        d--;
        dis[c][d] = min(e, dis[c][d]);
        dis[d][c] = min(e, dis[d][c]);
        for (int m = 0; m < n; m++) {
          for (int j = 0; j < n; j++) {
            for (int l = 0; l < n; l++) {
              dis[j][l] = min(dis[j][l], dis[j][m] + dis[m][l]);
            }
          }
        }
      } else {
        int a, b;
        scanf("%d %d", &a, &b);
        // cin>>a>>b;
        a--;
        b--;
        if (dis[a][b] == INF)
          printf("-1\\n");
        else
          printf("%d\\n", dis[a][b]);
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int n, k;
int dis[100];
bool f = false;
int nowdis[100];
vector<vector<pair<int, int>>> edge; // to cost

int djk(int start, int end) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq; // cost pos
  for (int i = 0; i < n; i++) {
    dis[i] = 100000000;
  }
  pq.push(make_pair(0, start));
  while (pq.size()) {
    pair<int, int> tmp = pq.top();
    pq.pop();
    if (tmp.first < dis[tmp.second]) {
      dis[tmp.second] = tmp.first;
      for (int i = 0; i < edge[tmp.second].size(); i++) {
        pq.push(make_pair(tmp.first + edge[tmp.second][i].second,
                          edge[tmp.second][i].first));
      }
    }
  }
  if (dis[end] == 100000000)
    return -1;
  return dis[end];
}

int main() {
  while (cin >> n >> k, n * k) {
    edge.resize(n);
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      if (a == 0) {
        int b, c;
        cin >> b >> c;
        cout << djk(b - 1, c - 1) << endl;
      } else {
        int b, c, d;
        cin >> b >> c >> d;
        edge[b - 1].push_back(make_pair(c - 1, d));
        edge[c - 1].push_back(make_pair(b - 1, d));
      }
    }
    edge.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int n, k;
int dis[100];
vector<vector<pair<int, int>>> edge; // to cost

int djk(int start, int end) {
  priority_queue<pair<int, int>> pq; // cost pos
  for (int i = 0; i < n; i++) {
    dis[i] = 100000000;
  }
  pq.push(make_pair(0, start));
  while (pq.size()) {
    pair<int, int> tmp = pq.top();
    pq.pop();
    if (tmp.first < dis[tmp.second]) {
      dis[tmp.second] = tmp.first;
      for (int i = 0; i < edge[tmp.second].size(); i++) {
        pq.push(make_pair(tmp.first + edge[tmp.second][i].second,
                          edge[tmp.second][i].first));
      }
    }
  }
  if (dis[end] == 100000000)
    return -1;
  return dis[end];
}

int main() {
  while (cin >> n >> k, n * k) {
    edge.resize(n);
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      if (a == 0) {
        int b, c;
        cin >> b >> c;
        cout << djk(b - 1, c - 1) << endl;
      } else {
        int b, c, d;
        cin >> b >> c >> d;
        edge[b - 1].push_back(make_pair(c - 1, d));
        edge[c - 1].push_back(make_pair(b - 1, d));
      }
    }
    edge.clear();
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

constexpr auto MAX = 101;
constexpr auto INF = 10000000;

int cost[MAX][MAX];
auto n = 0;
auto k = 0;

auto init() -> void {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
      cost[i][j] = i == j ? 0 : INF;
    }
  }
}

auto update(int c, int d, int e) -> void {
  cost[c][d] = min(cost[c][d], e);
  cost[d][c] = cost[c][d];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cost[i][j] = min(cost[i][j], cost[i][c] + cost[c][d] + cost[d][j]);
      cost[j][i] = cost[i][j];
    }
  }
}

auto main(int argc, char const *argv[]) -> int {
  while (cin >> n >> k, n || k) {
    init();
    auto witch = 0;
    for (auto i = 0; i < k; i++) {
      cin >> witch;
      if (witch) {
        auto c = 0;
        auto d = 0;
        auto e = 0;
        cin >> c >> d >> e;
        update(c, d, e);
      } else {
        auto a = 0;
        auto b = 0;
        cin >> a >> b;
        if (cost[a][b] != INF)
          cout << cost[a][b] << endl;
        else
          cout << "-1" << endl;
      }
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

constexpr auto MAX = 101;
constexpr auto INF = 10000000;

int cost[MAX][MAX];
auto n = 0;
auto k = 0;

auto update(int c, int d, int e) -> void {
  cost[c][d] = min(cost[c][d], e);
  cost[d][c] = min(cost[d][c], e);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cost[j][k] = min(cost[i][j], cost[i][c] + cost[c][d] + cost[d][j]);
      cost[k][j] = cost[j][k];
    }
  }
}

auto main(int argc, char const *argv[]) -> int {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++) {
      cost[i][j] = INF;
    }
  }

  while (cin >> n >> k, n || k) {
    auto witch = 0;
    for (auto i = 0; i < k; i++) {
      cin >> witch;
      if (witch) {
        auto c = 0;
        auto d = 0;
        auto e = 0;
        cin >> c >> d >> e;
        update(c, d, e);
      } else {
        auto a = 0;
        auto b = 0;
        cin >> a >> b;
        if (cost[a][b] != INF)
          cout << cost[a][b] << endl;
        else
          cout << "-1" << endl;
      }
    }
  }

  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int INF = 1 << 20;
int n, m;
int cost[111][111];
bool used[111];
int d[111];
int main(void) {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 1; i <= n; i++) {
      fill(cost[i], cost[i] + n + 1, INF);
    }

    for (int i = 0; i < m; i++) {
      int f;
      cin >> f;
      if (f) {
        int a, b, c;
        cin >> a >> b >> c;
        cost[a][b] = cost[b][a] = min(cost[a][b], c);
      } else {
        int a, b;
        cin >> a >> b;
        memset(used, false, sizeof(used));
        fill(d, d + n + 1, INF);
        d[a] = 0;

        while (1) {
          int v = 0;
          for (int j = 1; j <= n; j++) {
            if (used[j])
              continue;
            if (!v || d[j] < d[v])
              v = j;
          }
          if (!v)
            break;
          used[v] = true;
          for (int j = 1; j <= n; j++) {
            d[j] = min(d[j], d[v] + cost[v][j]);
          }
        }

        cout << ((d[b] == INF) ? -1 : d[b]) << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct edge {
  int t, c;
  edge(int t, int c) : t(t), c(c) {}
};
typedef pair<int, int> P;
int INF = 1 << 20;
int n, m;
vector<edge> g[111];
int d[111];
int main(void) {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 1; i <= n; i++) {
      g[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int f;
      cin >> f;
      if (f) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back(edge(b, c));
        g[b].push_back(edge(a, c));
      } else {
        int a, b;
        cin >> a >> b;
        fill(d, d + n + 1, INF);
        priority_queue<P> que;
        que.push(P(0, a));
        d[a] = 0;
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          int v = p.second;
          if (d[v] < p.first)
            continue;
          for (int j = 0; j < (int)g[v].size(); j++) {
            if (d[g[v][j].t] > d[v] + g[v][j].c) {
              d[g[v][j].t] = d[v] + g[v][j].c;
              que.push(P(d[g[v][j].t], g[v][j].t));
            }
          }
        }
        cout << ((d[b] == INF) ? -1 : d[b]) << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

typedef long long ll;

#define chmin(a, b) a = min(a, b)
#define rep(i, x) for (ll i = 0; i < (x); ++i)

const ll linf = 3e18;

ll cost[100][100];
ll d[100];
bool used[100];

ll N, K;

void dijkstra(ll s) {
  fill_n(d, N, linf);
  fill_n(used, N, false);
  d[s] = 0;

  while (true) {
    ll v = -1;

    rep(u, N) {
      if (!used[u] && (v == -1 || d[v] > d[u]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;

    rep(u, N) { chmin(d[u], d[v] + cost[v][u]); }
  }
}

signed main() {
  while (scanf("%lld %lld", &N, &K), N || K) {
    fill(cost[0], cost[N], linf);

    rep(i, K) {
      ll t;
      scanf("%lld", &t);
      if (t == 1) {
        ll a, b, c;
        scanf("%lld %lld %lld", &a, &b, &c);
        a--;
        b--;
        chmin(cost[a][b], c);
        chmin(cost[b][a], c);
      } else {
        ll a, b;
        scanf("%lld %lld", &a, &b);
        a--;
        b--;
        dijkstra(a);
        printf("%lld\\n", d[b] == linf ? -1 : d[b]);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define PB push_back
#define chmin(a, b) a = min(a, b)
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rep1(i, x) for (int i = 1; i <= (x); ++i)

typedef long long ll;

const ll linf = 3e18;

vector<ll> graph[101];
ll cost[101][101];
bool done[101];

ll dfs(ll from, ll to) {
  if (from == to)
    return 0;

  if (done[from])
    return linf;
  done[from] = true;

  ll ret = linf;

  rep(i, graph[from].size())
      chmin(ret, cost[from][graph[from][i]] + dfs(graph[from][i], to));

  done[from] = false;

  if (ret == linf)
    return -1;

  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, k;
  while (cin >> n >> k, n || k) {
    memset(cost, 0, sizeof(cost));
    fill_n(graph, n + 1, vector<ll>());

    rep(i, k) {
      ll t;
      cin >> t;
      if (t) {
        ll c, d, e;
        cin >> c >> d >> e;
        if (cost[c][d] == 0) {
          graph[c].PB(d);
          graph[d].PB(c);
          cost[c][d] = cost[d][c] = e;
        } else {
          chmin(cost[c][d], e);
          chmin(cost[d][c], e);
        }
      } else {
        ll a, b;
        cin >> a >> b;

        memset(done, 0, sizeof(done));
        cout << dfs(a, b) << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  for (;;) {
    int n, k;
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    vector<vector<int>> edges(n, vector<int>(n, -1)),
        costs(n, vector<int>(n, -1));
    for (int i = 0; i < n; i++) {
      costs[i][i] = 0;
    }
    for (int ix = 0; ix < k; ix++) {
      int mo;
      scanf("%d", &mo);
      if (mo == 0) {
        int st, go;
        scanf("%d%d", &st, &go);
        st--;
        go--;
        printf("%d\\n", costs[st][go]);
      } else {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        c--;
        d--;
        if (edges[c][d] == -1 || edges[c][d] > e) {
          edges[c][d] = e;
          edges[d][c] = e;
          for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
              if (costs[i][c] != -1 && costs[d][j] != -1) {
                int newco = costs[i][c] + edges[c][d] + costs[d][j];
                if (costs[i][j] == -1 || costs[i][j] > newco) {
                  costs[i][j] = costs[j][i] = newco;
                }
              }
            }
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int main() {
  for (;;) {
    int n, k;
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    vector<vector<int>> edges(n, vector<int>(n, -1));
    for (int ix = 0; ix < k; ix++) {
      int mo;
      scanf("%d", &mo);
      if (mo == 0) {
        int st, go;
        scanf("%d%d", &st, &go);
        st--;
        go--;
        priority_queue<P, vector<P>, greater<P>> q;
        vector<int> d(n, -1);
        d[st] = 0;
        q.push(P(st, 0));
        while (!q.empty()) {
          P t = q.top();
          q.pop();
          int v = t.first, di = t.second;
          if (d[v] != -1 && d[v] < di)
            continue;
          for (int i = 0; i < n; i++) {
            if (edges[v][i] != -1 &&
                (d[i] == -1 || d[i] > d[v] + edges[v][i])) {
              d[i] = d[v] + edges[v][i];
              q.push(P(i, d[i]));
            }
          }
        }
        printf("%d\\n", d[go]);
      } else {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        if (edges[c - 1][d - 1] == -1 || edges[c - 1][d - 1] > e) {
          edges[c - 1][d - 1] = e;
          edges[d - 1][c - 1] = e;
        }
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1 << 20;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  for (int n, k; cin >> n >> k, n;) {
    vector<vector<int>> fare(n, vector<int>(n, MAX));
    for (int i = 0; i < n; ++i)
      fare[i][i] = 0;

    for (int i = 0; i < k; ++i) {
      int y;
      cin >> y;
      if (y == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        --c;
        --d;

        fare[c][d] = min(fare[c][d], e);
        fare[d][c] = min(fare[d][c], e);

        // wa-sharu floydo hou
        for (int o = 0; o < n; ++o)
          for (int p = 0; p < n; ++p) {
            fare[o][p] = min(fare[o][p], fare[o][c] + fare[c][d] + fare[d][p]);
            fare[o][p] = min(fare[o][p], fare[o][d] + fare[d][c] + fare[c][p]);
          }
      } else {
        int a, b;
        cin >> a >> b;
        --a;
        --b;

        if (fare[a][b] == MAX)
          cout << -1 << endl;
        else
          cout << fare[a][b] << endl;
      }
    }
  }

  return EXIT_SUCCESS;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1 << 20;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  for (int n, k; cin >> n >> k, n;) {
    vector<vector<int>> fare(n, vector<int>(n, MAX));

    for (int i = 0; i < k; ++i) {
      int y;
      cin >> y;
      if (y == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;

        if (fare[c][d] > e || fare[c][d] == MAX)
          fare[c][d] = e;
        if (fare[d][c] > e || fare[d][c] == MAX)
          fare[d][c] = e;

        // wa-sharu floydo hou
        for (int m = 0; m < n; ++m)
          for (int o = 0; o < n; ++o)
            for (int p = 0; p < n; ++p)
              fare[o][p] = min(fare[o][p], fare[o][m] + fare[m][p]);
      } else {
        int a, b;
        cin >> a >> b;
        --a;
        --b;

        if (fare[a][b] == MAX)
          cout << -1 << endl;
        else
          cout << fare[a][b] << endl;
      }
    }
  }

  return EXIT_SUCCESS;
}
Problem p00449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pi;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> Graph;

int Dijkstr(Graph &info, int s, int g) {

  // if(info.size() <= 0) return -1;

  vector<int> min_cost; // その場の最小値
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  min_cost.resize(info.size(), -1);
  que.push(Pi(0, s));
  min_cost[s] = 0;

  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    if (p.second == g)
      return p.first;
    if (p.first > min_cost[p.second])
      continue;
    for (int i = 0; i < info[p.second].size(); i++) {
      edge &e = info[p.second][i];
      if (min_cost[e.to] == -1 || e.cost + p.first < min_cost[e.to]) {
        min_cost[e.to] = p.first + e.cost;
        que.push(Pi(min_cost[e.to], e.to));
      }
    }
  }
  return -1;
}

int main() {

  int n, k, sele, a, b, c, d, e;
  while (cin >> n >> k, n) { // nが0以外の間
    Graph info;
    info.resize(k);
    for (int i = 0; i < k; i++) {
      cin >> sele;
      if (sele == 0) {
        cin >> a >> b;
        // aからbへの経路探索

        cout << Dijkstr(info, a, b) << endl;
      } else if (sele == 1) {
        cin >> c >> d >> e;
        info[c].push_back((edge){d, e});
        info[d].push_back((edge){c, e});
        // cout << "ehehe" << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pi;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> Graph;

int Dijkstr(Graph &info, int s, int g) {

  // if(info.size() <= 0) return -1;

  vector<int> min_cost; // その場の最小値
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  min_cost.resize(info.size(), -1);
  que.push(Pi(0, s));
  min_cost[s] = 0;

  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    if (p.second == g)
      return p.first;
    if (p.first > min_cost[p.second])
      continue;
    for (int i = 0; i < info[p.second].size(); i++) {
      edge &e = info[p.second][i];
      if (min_cost[e.to] == -1 || e.to + p.first < min_cost[e.to]) {
        min_cost[e.to] = p.first + e.cost;
        que.push(Pi(min_cost[e.to], e.to));
      }
    }
  }
  return -1;
}

int main() {

  int n, k, sele, a, b, c, d, e;
  while (cin >> n >> k, n) { // nが0以外の間
    Graph info;
    info.resize(k);
    for (int i = 0; i < k; i++) {
      cin >> sele;
      if (sele == 0) {
        cin >> a >> b;
        // aからbへの経路探索

        cout << Dijkstr(info, a, b) << endl;
      } else if (sele == 1) {
        cin >> c >> d >> e;
        info[c].push_back((edge){d, e});
        info[d].push_back((edge){c, e});
        // cout << "ehehe" << endl;
      }
    }
  }
}