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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    if (!N)
      break;
    vector<vector<int>> diss(N, vector<int>(N, 1e9));
    for (int i = 0; i < N; ++i)
      diss[i][i] = 0;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      if (a) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        diss[c][d] = min(diss[c][d], e);
        diss[d][c] = min(diss[d][c], e);
        for (int j = 0; j < N; ++j) {
          for (int k = 0; k < N; ++k) {
            diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
            diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
          }
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (diss[a][b] >= 1e9) {
          cout << -1 << endl;
        } else {
          cout << diss[a][b] << endl;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    vector<vector<int>> diss(N, vector<int>(N, 1e9));
    for (int i = 0; i < N; ++i)
      diss[i][i] = 0;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      if (a) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        diss[c][d] = min(diss[c][d], e);
        diss[d][c] = min(diss[d][c], e);
        for (int j = 0; j < N; ++j) {
          for (int k = 0; k < N; ++k) {
            diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
            diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
          }
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (diss[a][b] >= 1e9) {
          cout << -1 << endl;
        } else {
          cout << diss[a][b] << endl;
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, l;
int c[100][100];
const int INF = 10001000;

void wf(int x, int y) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      c[i][j] = min(c[i][j], c[i][x] + c[x][y] + c[y][j]);
      c[i][j] = min(c[i][j], c[i][y] + c[y][x] + c[x][j]);
    }
}

int main() {
  while (1) {
    scanf("%d %d", &n, &l);
    if (n == 0 && l == 0)
      break;
    for (int i = 0; i < 100; i++)
      for (int j = 0; j < 100; j++)
        c[i][j] = INF;
    for (int i = 0; i < n; i++)
      c[i][i] = 0;
    for (int h = 0; h < l; h++) {
      int t;
      scanf("%d", &t);
      if (t == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        if (c[a - 1][b - 1] == INF)
          printf("-1\\n");
        else
          printf("%d\\n", c[a - 1][b - 1]);
      } else {
        int x, y, z;
        scanf("%d %d %d", &x, &y, &z);
        c[x - 1][y - 1] = min(c[x - 1][y - 1], z);
        c[y - 1][x - 1] = c[x - 1][y - 1];
        wf(x - 1, y - 1);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int n, l;
int c[100][100];
const int INF = 10001000;

void wf() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
}

int main() {
  while (1) {
    scanf("%d %d", &n, &l);
    if (n == 0 && l == 0)
      break;
    for (int i = 0; i < 100; i++)
      for (int j = 0; j < 100; j++)
        c[i][j] = INF;
    for (int h = 0; h < l; h++) {
      int t;
      scanf("%d", &t);
      if (t == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        if (c[a - 1][b - 1] == INF)
          printf("-1\\n");
        else
          printf("%d\\n", c[a - 1][b - 1]);
      } else {
        int x, y, z;
        scanf("%d %d %d", &x, &y, &z);
        c[x - 1][y - 1] = min(c[x - 1][y - 1], z);
        c[y - 1][x - 1] = c[x - 1][y - 1];
        wf();
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (INT_MAX / 2)

int E[100][100];

int main() {
  int n, k;
  while (cin >> n >> k, n | k) {
    fill_n((int *)E, 100 * 100, INF);
    REP(i, n) E[i][i] = 0;
    bool updated = false;
    REP(i, k) {
      int a, b, c, d, e;
      int f;
      cin >> f;
      if (f == 1) {
        cin >> c >> d >> e;
        c--;
        d--;
        if (E[c][d] > e) {
          E[c][d] = E[d][c] = e;
          updated = true;
        }
      } else {
        if (updated) {
          updated = false;
          REP(i, n)
          REP(j, n) REP(k, n) E[j][k] = min(E[j][k], E[j][i] + E[i][k]);
        }
        cin >> a >> b;
        a--;
        b--;
        cout << (E[a][b] == INF ? -1 : E[a][b]) << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define INF (INT_MAX / 2)

int main() {
  int n, k;
  while (cin >> n >> k, n | k) {
    vvi E(n, vi(n, INF));
    REP(i, n) E[i][i] = 0;
    vvi E2 = E;
    bool updated = false;
    REP(i, k) {
      int a, b, c, d, e;
      int f;
      cin >> f;
      if (f == 1) {
        cin >> c >> d >> e;
        c--;
        d--;
        E[c][d] = min(E[c][d], e);
        E[d][c] = min(E[d][c], e);
        updated = true;
      } else {
        if (updated) {
          updated = false;
          E2 = E;
          REP(i, n)
          REP(j, n) REP(k, n) E2[j][k] = min(E2[j][k], E2[j][i] + E2[i][k]);
        }
        cin >> a >> b;
        a--;
        b--;
        cout << (E2[a][b] == INF ? -1 : E2[a][b]) << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define INF 100000000
using namespace std;
int field[101][101];
void dijik(int s, int g);
int n;
int main(void) {
  int k;
  while (scanf("%d%d", &n, &k), n + k) {
    memset(field, 0, sizeof(field));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j)
          continue;
        field[i][j] = INF;
      }
    }

    for (int i = 0; i < k; i++) {
      int f;
      scanf("%d", &f);
      if (f == 0) {
        int a, b;
        scanf("%d%d", &a, &b);
        dijik(a, b);
      } else {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        field[c][d] = min(field[c][d], e);
        field[d][c] = min(field[d][c], e);
      }
    }
  }
  return 0;
}

void dijik(int s, int g) {
  int cost[101];
  for (int i = 1; i <= n; i++)
    cost[i] = INF;
  int flag[101] = {0};
  cost[s] = 0;
  while (1) {
    int idx = -1;
    for (int i = 1; i <= n; i++) {
      if (flag[i] == 0 && (idx == -1 || cost[idx] > cost[i])) {
        idx = i;
      }
    }
    if (idx == -1) {
      break;
    }
    flag[idx] = 1;
    if (idx == g) {
      if (cost[idx] == INF) {
        printf("-1\\n");
      }

      else {
        printf("%d\\n", cost[idx]);
      }
      return;
    }

    for (int i = 1; i <= n; i++) {
      cost[i] = min(cost[i], cost[idx] + field[idx][i]);
    }
  }
  return;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
void dijik(int s, int g);
struct edge {
  int to, cost;
};
typedef pair<int, int> pi;
vector<vector<edge>> g(101, vector<edge>(0));
int main(void) {
  int n, k;
  while (scanf("%d%d", &n, &k), n + k) {
    for (int i = 0; i < k; i++) {
      int a, b, c, d;
      scanf("%d%d%d", &a, &b, &c);
      if (a == 0) {
        dijik(b, c);
      } else {
        scanf("%d", &d);
        struct edge e;
        e.to = c;
        e.cost = d;
        g[b].push_back(e);
        e.to = b;
        g[c].push_back(e);
      }
    }
    for (int i = 0; i < 101; i++) {
      g[i].clear();
    }
  }
  return 0;
}

void dijik(int s, int goal) {
  int isl[101];
  for (int i = 0; i < 101; i++)
    isl[i] = -1;
  priority_queue<pi> que;
  int mi = -1;
  que.push(pi(0, s));
  while (que.size()) {
    pi p = que.top();
    que.pop();
    if (p.second == goal) {
      if (mi == -1 || mi > p.first)
        mi = p.first;
    }
    if (isl[p.second] == -1 || isl[p.second] > p.first) {
      isl[p.second] = p.first;
      for (int i = 0; i < g[p.second].size(); i++) {
        que.push(pi(p.first + g[p.second][i].cost, g[p.second][i].to));
      }
    }
  }
  printf("%d\\n", mi);
  return;
}
Problem p00449 (C++) — buggy submission, verdict: Runtime Error

#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  const int INF = 10000000;
  int n, k, map[100][100], a, b, c;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;

  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j)
        map[i][j] = INF;
    }

    for (int g = 0; g < k; ++g) {
      scanf("%d", &a);
      if (a == 0) {
        // vZ
        int ans = -1;
        bool flag[100] = {};

        scanf("%d%d", &a, &b);
        q.push(pair<int, int>(0, a - 1));

        while (!q.empty()) {
          pair<int, int> p = q.top();
          q.pop();
          int &cost = p.first, &num = p.second;
          if (flag[num])
            continue;
          flag[num] = true;

          if (num == b - 1) {
            ans = cost;
            for (int i = q.size(); i > 0; --i)
              q.pop();
            break;
          }

          for (int i = 0; i < n; ++i) {
            if (map[num][i] == INF || flag[i])
              continue;
            q.push(pair<int, int>(cost + map[num][i], i));
          }
        }

        printf("%d\\n", ans);
      } else {
        // ÇÁ
        scanf("%d%d%d", &a, &b, &c);
        if (map[a - 1][b - 1] > c)
          map[a - 1][b - 1] = map[b - 1][a - 1] = c;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  const int INF = 10000000;
  int n, k, map[100][100], a, b, c;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;

  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j)
        map[i][j] = INF;
    }

    for (int g = 0; g < k; ++g) {
      scanf("%d", &a);
      if (a == 0) {
        // vZ
        int ans = -1;
        bool flag[100] = {};

        scanf("%d%d", &a, &b);
        q.push(pair<int, int>(0, a - 1));

        while (!q.empty()) {
          pair<int, int> p = q.top();
          q.pop();
          int &cost = p.first, &num = p.second;
          if (flag[num])
            continue;
          flag[num] = true;

          if (num == b - 1) {
            ans = cost;
            for (int i = q.size(); i > 0; ++i)
              q.pop();
            break;
          }

          for (int i = 0; i < n; ++i) {
            if (map[num][i] == INF || flag[i])
              continue;
            q.push(pair<int, int>(cost + map[num][i], i));
          }
        }

        printf("%d\\n", ans);
      } else {
        // ÇÁ
        scanf("%d%d%d", &a, &b, &c);
        if (map[a - 1][b - 1] > c)
          map[a - 1][b - 1] = map[b - 1][a - 1] = c;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define INF 114514810

int n, k;
int dist[120];
int cost[120][120];
bool used[120];

void dikstr(int s) {
  fill(dist, dist + n, INF);
  fill(used, used + n, false);
  dist[s] = 0;

  while (1) {
    int v = -1;
    rep(u, n) {
      if (!used[u] and (v == -1 or dist[u] < dist[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;

    rep(u, n) dist[u] = min(dist[u], dist[v] + cost[v][u]);
  }
}

int main(void) {
  while (1) {
    scanf("%d %d", &n, &k);
    if (n == 0 and k == 0)
      break;
    rep(i, n) rep(j, n) cost[i][j] = INF;

    rep(i, k) {
      int f, a, b, c, d, e;
      scanf("%d", &f);
      if (f == 0) {
        scanf("%d%d", &a, &b);
        a--;
        b--;
        dikstr(a);
        if (dist[b] >= INF)
          puts("-1");
        else
          cout << dist[b] << "\\n";
      } else if (f == 1) {
        scanf("%d%d%d", &c, &d, &e);
        c--;
        d--;
        cost[c][d] = cost[d][c] = min(cost[c][d], e);
      }
    }

    /* debug
    rep(i,n) rep(j,n) {
        printf("dist[%d][%d] = %d\\n", i, j, dist[i][j]);
    }
    */
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define INF 114514810

int n, k;
int dist[120][120];

void wf() {
  rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
      min(dist[i][j], dist[i][k] + dist[k][j]);
}

void init() {
  rep(i, n) rep(j, n) {
    if (i == j)
      dist[i][j] = 0;
    else
      dist[i][j] = INF;
  }
}

int main(void) {
  while (1) {
    scanf("%d %d", &n, &k);
    if (n == 0 and k == 0)
      break;
    init();

    rep(i, k) {
      int f;
      scanf("%d", &f);
      if (f == 0) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--;
        b--;
        if (dist[a][b] >= INF)
          puts("-1");
        else
          printf("%d\\n", dist[a][b]);
      } else if (f == 1) {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        c--;
        d--;
        dist[c][d] = dist[d][c] = min(dist[c][d], e);
        wf();
      }
    }

    /* debug
    rep(i,n) rep(j,n) {
        printf("dist[%d][%d] = %d\\n", i, j, dist[i][j]);
    }
    */
  }

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

#include <bits/stdc++.h>

using namespace std;

#define MAX 101
#define INF INT_MAX / 4

int G[MAX][MAX], N;

int dijkstra(int a, int b) {
  bool used[MAX];
  int cost[MAX];
  memset(used, false, sizeof(used));
  fill(cost, cost + N, INF);

  cost[a] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < N; u++) {
      if (!used[u] && (v == -1 || cost[u] < cost[v])) {
        v = u;
      }
    }

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

    for (int u = 0; u < N; u++) {
      cost[u] = min(cost[u], cost[v] + G[v][u]);
    }
  }

  return cost[b];
}

int main() {
  int K, num, a, b, c;

  while (scanf("%d%d", &N, &K), (N | K)) {
    fill(G[0], G[N], INF);

    while (K--) {
      scanf("%d", &num);
      if (num) {
        cin >> a >> b >> c;
        a--, b--;
        if (G[a][b] > c) {
          G[a][b] = G[b][a] = c;
        }
      } else {
        cin >> a >> b;
        a--, b--;
        int val = dijkstra(a, b);
        if (val != INF) {
          printf("%d\\n", val);
        } else {
          puts("-1");
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 101
#define INF INT_MAX / 4

int G[MAX][MAX], N;

void make() {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
      }
    }
  }
}

int main() {
  int K, num, a, b, c;

  while (scanf("%d%d", &N, &K), (N | K)) {
    fill(G[0], G[N], INF);
    while (K--) {
      scanf("%d", &num);
      if (num) {
        cin >> a >> b >> c;
        a--, b--;
        G[a][b] = G[b][a] = c;
        make();
      } else {
        cin >> a >> b;
        if (G[a - 1][b - 1] != INF) {
          printf("%d\\n", G[a - 1][b - 1]);
        } else {
          puts("-1");
        }
      }
    }
  }

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int n, k;
int w[101][101];
bool visited[101];
int dist[101];
const int INF = 99999999;

int main() {
  while (cin >> n >> k && n && k) {
    int a, b, c, d, e, t;

    for (int i = 1; i <= n; i++)
      fill(w[i], w[i] + 101, INF);

    for (int i = 0; i < k; i++) {
      cin >> t;
      if (t == 0) {
        cin >> a >> b;

        fill(visited, visited + 101, false);
        fill(dist, dist + 101, INF);
        priority_queue<P, vector<P>, greater<P>> que;
        que.push(P(0, a));
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          int v = p.second, cost = p.first;
          if (visited[v])
            continue;

          visited[v] = true;
          dist[v] = cost;

          if (v == b) {
            cout << cost << endl;
            break;
          }

          for (int u = 1; u <= n; u++) {
            if (dist[u] > dist[v] + w[v][u])
              que.push(P(dist[v] + w[v][u], u));
          }
        }
        if (!visited[b])
          cout << -1 << endl;

      } else {
        cin >> c >> d >> e;
        w[c][d] = min(w[c][d], e);
        w[d][c] = w[c][d];
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, k;
int w[101][101];
const int INF = 99999999;

int main() {
  while (cin >> n >> k && n && k) {
    int a, b, c, d, e, t;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        w[i][j] = (i == j) ? 0 : INF;

    int update_step = -1;
    for (int i = 0; i < k; i++) {
      cin >> t;
      if (t == 0) {
        cin >> a >> b;

        if (update_step != i - 1) {
          for (int k = 1; k <= n; k++) {
            for (int from = 1; from <= n; from++) {
              for (int to = 1; to <= n; to++) {
                w[from][to] = min(w[from][to], w[from][k] + w[k][to]);
              }
            }
          }
          update_step = i;
        }

        if (w[a][b] == INF)
          cout << -1 << endl;
        else
          cout << w[a][b] << endl;
      } else {
        cin >> c >> d >> e;
        w[c][d] = min(w[c][d], e);
        w[d][c] = w[c][d];
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dist[105], n, m, a, b, c, d;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q;
vector<pair<int, int>> x[105];
void add_edge(int a1, int a2, int a3) {
  x[a1].push_back(make_pair(a2, a3));
  x[a2].push_back(make_pair(a1, a3));
}
int solve(int b1, int b2) {
  for (int i = 0; i < 105; i++)
    dist[i] = 100000000;
  dist[b1] = 0;
  Q.push(make_pair(0, b1));
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < (int)x[a2].size(); i++) {
      int to = x[a2][i].first, len = x[a2][i].second;
      if (dist[to] > a1 + len) {
        dist[to] = a1 + len;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  if (dist[b2] == 100000000)
    return -1;
  return dist[b2];
}
int main() {
  while (true) {
    cin >> n >> m;
    for (int i = 0; i < 105; i++)
      x[i].clear();
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> a;
      if (a == 0) {
        cin >> b >> c;
        cout << solve(b, c) << endl;
      }
      if (a == 1) {
        cin >> b >> c >> d;
        add_edge(b, c, d);
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dist[105], n, m, a, b, c, d;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q;
vector<pair<int, int>> x[105];
void add_edge(int a1, int a2, int a3) {
  x[a1].push_back(make_pair(a2, a3));
  x[a2].push_back(make_pair(a1, a3));
}
int solve(int b1, int b2) {
  for (int i = 0; i < 105; i++)
    dist[i] = 100000000;
  dist[b1] = 0;
  while (!Q.empty()) {
    int a1 = Q.top().first, a2 = Q.top().second;
    Q.pop();
    for (int i = 0; i < x[a2].size(); i++) {
      int to = x[a2][i].first, len = x[a2][i].second;
      if (dist[to] > a1 + len) {
        dist[to] = a1 + len;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  if (dist[b2] == 100000000)
    return -1;
  return dist[b2];
}
int main() {
  while (true) {
    cin >> n >> m;
    for (int i = 0; i < 105; i++)
      x[i].clear();
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> a;
      if (a == 0)
        break;
      if (a == 0) {
        cin >> b >> c;
        cout << solve(b, c) << endl;
      }
      if (a == 1) {
        cin >> b >> c >> d;
        add_edge(b, c, d);
      }
    }
  }
}