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

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define INF 1000000000

int main() {
  int n, m, flg;
  ll a, b, c;
  while (cin >> n >> m && n && m) {
    vector<P> node[101];
    for (int k = 0; k < m; k++) {
      cin >> flg;
      if (flg) {
        cin >> a >> b >> c;
        P p;
        p.first = b;
        p.second = c;
        node[a].push_back(p);
        p.first = a;
        node[b].push_back(p);
      } else {
        bool f = true;
        cin >> a >> b;
        ll used[102];
        used[a] = 0;
        for (int j = 1; j <= n; j++)
          used[j] = INF;
        priority_queue<P, vector<P>, greater<P>> que;
        que.push(P(0, a));
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          ll cost = p.first;
          ll now = p.second;
          if (now == b) {
            cout << cost << endl;
            f = false;
            break;
          }
          int len = node[now].size();
          for (int i = 0; i < len; i++) {
            P p = node[now][i];
            ll next = p.first;
            ll nc = p.second;
            if (used[next] == INF || used[next] > cost + nc) {
              used[next] = cost + nc;
              que.push(P(cost + nc, next));
            }
          }
        }
        if (f)
          cout << "-1" << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

int main() {
  int n, m, flg;
  ll a, b, c;
  while (cin >> n >> m && n && m) {
    vector<P> node[101];
    for (int k = 0; k < m; k++) {
      cin >> flg;
      if (flg) {
        cin >> a >> b >> c;
        P p;
        p.first = b;
        p.second = c;
        node[a].push_back(p);
        p.first = a;
        node[b].push_back(p);
      } else {
        bool f = true;
        cin >> a >> b;
        priority_queue<P, vector<P>, greater<P>> que;
        que.push(P(0, a));
        while (!que.empty()) {
          P p = que.top();
          que.pop();
          ll cost = p.first;
          ll now = p.second;
          if (now == b) {
            cout << cost << endl;
            f = false;
            break;
          }
          int len = node[now].size();
          for (int i = 0; i < len; i++) {
            P p = node[now][i];
            ll next = p.first;
            ll nc = p.second;
            que.push(P(cost + nc, next));
          }
        }
        if (f)
          cout << "-1" << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000000;
int dist[101][101];

void calc(int c, int d, int e) {
  for (int i = 0; i < 101; ++i) {
    for (int j = 0; j < 101; ++j) {
      dist[i][j] = dist[j][i] = min(dist[i][j], dist[i][c] + e + dist[d][j]);
    }
  }
}

int main() {
  int n, k;
  while (cin >> n >> k, n) {
    // prepare
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        dist[i][j] = i == j ? 0 : INF;
      }
    }
    int type, a, b, c, d, e;
    for (int i = 0; i < k; ++i) {
      cin >> type;
      if (type == 0) {
        cin >> a >> b;
        if (dist[a][b] == INF) {
          cout << -1 << endl;
        } else {
          cout << dist[a][b] << endl;
        }
      } else {
        cin >> c >> d >> e;
        calc(c, d, e);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000000;
int dist[101][101];

void solve() {
  for (int k = 0; k < 101; ++k) {
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        dist[i][j] = dist[j][i] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
}

int main() {
  int n, k;
  while (cin >> n >> k, n) {
    // prepare
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        dist[i][j] = i == j ? 0 : INF;
      }
    }
    int type, a, b, c, d, e;
    for (int i = 0; i < k; ++i) {
      cin >> type;
      if (type == 0) {
        cin >> a >> b;
        if (dist[a][b] == INF) {
          cout << -1 << endl;
        } else {
          cout << dist[a][b] << endl;
        }
      } else {
        cin >> c >> d >> e;
        dist[c][d] = min(dist[c][d], e);
        dist[d][c] = min(dist[d][c], e);
        solve();
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
static const int INF = 1000000000000;
static const int MAX_N = 1e+6 + 10;
using namespace std;

struct edge {
  int to, cost;
  edge() {}
  edge(int p1, int p2) {
    to = p1;
    cost = p2;
  }
};

typedef pair<int, int> P;
vector<edge> G[MAX_N];
int D[MAX_N];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(D, D + 110, INF);
  D[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (D[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (D[e.to] > D[v] + e.cost) {
        D[e.to] = D[v] + e.cost;
        que.push(P(D[e.to], e.to));
      }
    }
  }
}

signed main() {
  int n, m;
  while (n != 0 && m != 0) {
    cin >> n >> m;
    for (int i = 0; i <= 110; ++i) {
      G[i].clear();
    }
    int cnt = m;
    while (cnt) {
      int a, b, c, d, e;
      int f;
      cin >> f;
      if (f == 0) {
        cin >> a >> b;
        dijkstra(a);
        if (D[b] != INF)
          cout << D[b] << endl;
        else
          cout << -1 << endl;
      } else {
        cin >> c >> d >> e;
        G[c].push_back(edge(d, e));
        G[d].push_back(edge(c, e));
      }
      cnt--;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
static const LL INF = 1000000000000;
static const LL MAX_N = 1e+7;
using namespace std;

struct edge {
  LL to, cost;
  edge() {}
  edge(LL p1, LL p2) {
    to = p1;
    cost = p2;
  }
};

typedef pair<LL, LL> P;
vector<edge> G[MAX_N];
LL D[MAX_N];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(D, D + MAX_N, INF);
  D[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (D[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (D[e.to] > D[v] + e.cost) {
        D[e.to] = D[v] + e.cost;
        que.push(P(D[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, m;
  int a, b, c, d, e;
  while (n != 0 && m != 0) {
    cin >> n >> m;
    for (int i = 0; i <= MAX_N; ++i) {
      G[i].clear();
    }
    int cnt = m;
    while (cnt) {
      int f;
      cin >> f;
      if (f == 0) {
        cin >> a >> b;
        dijkstra(a);
        if (D[b] != INF)
          cout << D[b] << endl;
        else
          cout << -1 << endl;
      } else {
        cin >> a >> b >> c;
        G[a].push_back(edge(b, c));
        G[b].push_back(edge(a, c));
      }
      cnt--;
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define INF (1 << 29)

int G[101][101];
int n, k;

void init() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[i][j] = INF;
      if (i == j)
        G[i][j] = 0;
    }
  }
}

int main() {
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    init();

    int op, a, b, e;
    for (int cs = 0; cs < k; cs++) {
      cin >> op;
      if (op == 1) {
        cin >> a >> b >> e;
        a--, b--;
        G[a][b] = min(G[a][b], e);
        G[b][a] = min(G[b][a], e);
        //	Warshall_Floyd();
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            G[i][j] = min(G[i][j], G[i][a] + G[a][b] + G[b][j]);
            G[j][i] = G[i][j];
          }
        }
      } else if (op == 0) {
        cin >> a >> b;
        a--, b--;
        if (G[a][b] == INF)
          cout << -1 << endl;
        else
          cout << G[a][b] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

#define INF (1 << 29)

int G[101][101];
int n, k;

void Warshall_Floyd() {
  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]);
      }
    }
  }
}

void init() {
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 101; j++)
      G[i][j] = INF;
}

int main() {
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    init();

    int op, a, b, e;
    for (int cs = 0; cs < k; cs++) {
      cin >> op;
      if (op == 1) {
        cin >> a >> b >> e;
        a--, b--;
        G[a][b] = G[b][a] = e;
        Warshall_Floyd();
      } else if (op == 0) {
        cin >> a >> b;
        a--, b--;
        if (G[a][b] == INF)
          cout << -1 << endl;
        else
          cout << G[a][b] << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 9999999
int dijkstra(int x, int y);
int D[128][128]; /*Adjacency matrix*/
int n;
int main() {
  int m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(D, 0, sizeof(D));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j)
          continue;
        D[i][j] = INF;
      }
    }

    for (int i = 0; i < m; i++) {
      int x;
      scanf("%d", &x);
      if (x == 0) {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%d\\n", dijkstra(a, b));
      } else {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        D[c][d] = min(D[c][d], e);
        D[d][c] = min(D[d][c], e);
      }
    }
  }
  return 0;
}
int dijkstra(int x, int y) {
  int dist[128];
  bool done[128] = {false};
  for (int i = 1; i <= n; i++) {
    dist[i] = INF;
  }

  dist[x] = 0;
  int cost = 0;
  while (1) {
    int e = -1;
    for (int i = 1; i <= n; i++) {
      if (done[i] == false && (e == -1 || dist[i] < dist[e])) {
        e = i;
      }
    }
    if (e == -1)
      break;
    done[e] = true;
    if (e == y) {
      cost = dist[e];
      break;
    }

    for (int i = 1; i <= n; i++) {
      dist[i] = min(dist[i], dist[e] + D[e][i]);
    }
  }

  if (cost == INF)
    return -1;
  else
    return cost;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 9999999
int warshall_floyed(int x, int y);
int D[128][128]; /*Adjacency matrix*/
int n;
int main() {
  int m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    memset(D, 0, sizeof(D));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j)
          continue;
        D[i][j] = INF;
      }
    }

    for (int i = 0; i < m; i++) {
      int x;
      scanf("%d", &x);
      if (x == 0) {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%d\\n", warshall_floyed(a, b));
      } else {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        D[c][d] = min(D[c][d], e);
        D[d][c] = min(D[d][c], e);
      }
    }
  }
  return 0;
}
int warshall_floyed(int x, int y) {
  int cost = 0;
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
      }
    }
  }
  (D[x][y] == INF) ? cost = -1 : cost = D[x][y];

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

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int path[101][101];

int dijkstra(int s, int g, int n) {
  int d[101];
  bool used[101];
  memset(d, 0x3f, sizeof(d));
  memset(used, 0, sizeof(used));
  d[s] = 0;

  while (1) {
    int v = -1;
    for (int u = 1; u <= n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }

    if (v == -1)
      break;

    used[v] = true;

    for (int u = 1; u <= n; u++) {
      d[u] = min(d[u], d[v] + path[v][u]);
    }
  }

  return d[g];
}

int main() {
  int n, k;

  while (~scanf("%d %d", &n, &k)) {
    if (n == k && k == 0)
      break;
    memset(path, 0x3f, sizeof(path));
    for (int i = 0; i < k; i++) {
      int ch;
      scanf("%d", &ch);
      if (ch) {
        int a, b, val;
        scanf("%d %d %d", &a, &b, &val);
        path[a][b] = path[b][a] = min(val, path[a][b]);
      } else {
        int a, b, res;
        scanf("%d %d", &a, &b);
        res = dijkstra(a, b, n);
        printf("%d\\n", res > INF ? -1 : res);
      }
    }
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double EPS = 1e-8;

int path[101][101];

int main() {
  int n, k;

  while (~scanf("%d %d", &n, &k)) {
    if (n == k && k == 0)
      break;
    memset(path, 0x3f, sizeof(path));

    bool calc = false;
    for (int i = 0; i < k; i++) {
      int ch;
      scanf("%d", &ch);
      if (ch) {
        int a, b, val;
        scanf("%d %d %d", &a, &b, &val);
        path[a][b] = path[b][a] = min(val, path[a][b]);
        calc = true;
      } else {
        int a, b;
        scanf("%d %d", &a, &b);
        if (calc) {
          for (int s = 1; s <= n; s++) {
            for (int t = 1; t <= n; t++) {
              for (int u = 1; u <= n; u++) {
                path[t][u] = min(path[t][s] + path[s][u], path[t][u]);
              }
            }
          }
          calc = false;
        }
        printf("%d\\n", path[a][b] > INF ? -1 : path[a][b]);
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> pii;

struct edge {
  int to, cost;
};

int main() {
  int n, k;
  while (cin >> n >> k, n) {
    int d[100];
    vector<edge> G[100];
    priority_queue<pii, vector<pii>, greater<pii>> q;

    for (int i = 0; i < k; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      b--;
      c--;

      if (a) {
        int e;
        cin >> e;

        edge E;
        E.to = c;
        E.cost = e;
        G[b].push_back(E);
        E.to = b;
        G[c].push_back(E);

      } else {
        fill(d, d + n, 1e9);
        d[b] = 0;
        q.push(make_pair(0, b));

        while (!q.empty()) {
          pii p = q.top();
          q.pop();
          int v = p.second;
          if (d[v] < p.first)
            continue;
          for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
              d[e.to] = d[v] + e.cost;
              q.push(make_pair(d[e.to], e.to));
            }
          }
        }

        cout << (d[c] == 1e9 ? -1 : d[c]) << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; ++i)

int main() {
  int n, k;
  while (cin >> n >> k, n) {

    int d[100][100];

    rep(i, 100) rep(j, 100) d[i][j] = 1e9;

    for (int i = 0; i < n; i++)
      d[i][i] = 0;

    for (int i = 0; i < k; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      b--;
      c--;

      if (a) {
        int e;
        cin >> e;
        d[b][c] = e;
        d[c][b] = e;

      } else {
        rep(k, n) rep(i, n) rep(j, n) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
        cout << (d[b][c] == 1e9 ? -1 : d[b][c]) << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

const int max = 1000000000;

int n;
int data[101][101];
long long cost[101];
bool used[101];

void dijkstra(int s) {
  for (int i = 1; i <= n; i++)
    cost[i] = max;
  for (int i = 1; i <= n; i++)
    used[i] = false;

  cost[s] = 0;
  while (true) {
    int v = -1;
    for (int i = 1; i <= n; i++) {
      if (!used[i] && (v == -1 || cost[i] < cost[v]))
        v = i;
    }
    if (v == -1)
      break;

    used[v] = true;

    for (int i = 1; i <= n; i++) {
      cost[i] = std::min(cost[i], cost[v] + data[v][i]);
    }
  }
}

int main() {

  int k;
  while (std::cin >> n >> k, n || k) {
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        data[i][j] = max;
      }
    }

    for (int i = 0; i < k; i++) {
      int t;
      std::cin >> t;

      if (t == 0) {
        int a, b;
        std::cin >> a >> b;

        dijkstra(a);
        if (cost[b] == max)
          std::cout << -1 << std::endl;
        else
          std::cout << cost[b] << std::endl;
      }

      if (t == 1) {
        int c, d, e;
        std::cin >> c >> d >> e;

        data[c][d] = std::min(data[c][d], e);
        data[d][c] = std::min(data[d][c], e);
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

const long long max = 1000000000;

long long data[101][101];

void warshall_floyd(int n) {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        data[i][j] = std::min(data[i][j], data[i][k] + data[k][j]);
      }
    }
  }
}

int main() {

  int n, k;
  while (std::cin >> n >> k, n || k) {
    for (int i = 1; i <= 100; i++) {
      for (int j = 1; j <= 100; j++) {
        data[i][j] = max;
      }
      data[i][i] = 0;
    }

    for (int i = 0; i < k; i++) {
      int t;
      std::cin >> t;

      if (t == 0) {
        int a, b;
        std::cin >> a >> b;

        if (data[a][b] == max)
          std::cout << -1 << std::endl;
        else
          std::cout << data[a][b] << std::endl;
      }

      if (t == 1) {
        int c, d, e;
        std::cin >> c >> d >> e;

        data[c][d] = e;
        data[d][c] = e;

        warshall_floyd(n);
      }
    }
  }

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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0)
      break;
    int route[100][100];
    REP(i, n) REP(j, n) route[i][j] = INF;
    int t, a, b, c;
    REP(i, k) {
      cin >> t;
      if (t == 1) {
        cin >> a >> b >> c;
        a--;
        b--;
        route[a][b] = min(route[a][b], c);
        route[b][a] = min(route[b][a], c);
        REP(i, n) REP(j, n) {
          route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
          route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
        }
        REP(i, n) REP(j, n) {
          route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
          route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
        }
      } else {
        cin >> a >> b;
        a--;
        b--;
        if (route[a][b] == INF)
          cout << -1 << endl;
        else
          cout << route[a][b] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0)
      break;
    int route[100][100];
    REP(i, n) REP(j, n) route[i][j] = INF;
    int t, a, b, c;
    REP(i, k) {
      cin >> t;
      if (t == 1) {
        cin >> a >> b >> c;
        a--;
        b--;
        route[a][b] = min(route[a][b], c);
        route[b][a] = min(route[b][a], c);
        REP(i, n) REP(j, n) REP(k, n) {
          route[i][j] = min(route[i][j], route[i][k] + route[k][j]);
        }
      } else {
        cin >> a >> b;
        a--;
        b--;
        if (route[a][b] == INF)
          cout << -1 << endl;
        else
          cout << route[a][b] << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define lli long long int

using namespace std;

const lli inf = 1000000000;

lli dij(lli cost[101][101], int n, int s, int t) {
  lli d[101];
  bool vis[101];

  fill(vis, vis + (n + 1), false);
  fill(d, d + (n + 1), inf);
  d[s] = 0;

  while (1) {
    int v = -1;
    for (int i = 1; i <= n; i++) {
      if (!vis[i] && (v == -1 || d[i] < d[v])) {
        v = i;
      }
    }
    if (v == -1) {
      break;
    }
    vis[v] = true;
    for (int i = 1; i <= n; i++) {
      d[i] = min(d[i], d[v] + cost[v][i]);
    }
  }

  return d[t];
}

int main() {
  int n, m, s, t;
  lli a[101][101], u;

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    fill(&a[1][1], &a[n][n], inf);
    for (int i = 1; i <= n; i++) {
      a[i][i] = 0;
    }

    while (m--) {
      cin >> s;
      if (s == 1) {
        cin >> s >> t >> u;
        if (a[s][t] > u) {
          a[s][t] = a[t][s] = u;
        }
      } else {
        cin >> s >> t;
        u = dij(a, n, s, t);
        if (u >= inf) {
          u = -1;
        }
        cout << u << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

typedef long long int lli;

const lli inf = 1000000000;

lli cst[101][101];
int n;

lli dij(int s, int t) {
  lli d[101];
  bool vis[101];

  fill(vis, vis + (n + 1), false);
  fill(d, d + (n + 1), inf);
  d[s] = 0;

  while (1) {
    int v = -1;
    for (int i = 1; i <= n; ++i)
      if (!vis[i] && (v == -1) || d[i] < d[v])
        v = i;

    if (v == -1)
      break;

    vis[v] = true;
    for (int i = 1; i <= n; ++i)
      d[i] = min(d[i], d[v] + cst[v][i]);
  }

  return d[t];
}

int main() {
  int s, t, u, m;

  while (cin >> n >> m && n + m) {
    fill(&cst[0][0], &cst[n + 1][0], inf);
    for (int i = 1; i <= n; ++i)
      cst[i][i] = 0;

    while (m--) {
      cin >> s;
      if (s) {
        cin >> s >> t >> u;
        if (cst[s][t] > u)
          cst[s][t] = cst[t][s] = u;
      } else {
        cin >> s >> t;
        u = dij(s, t);
        cout << ((u >= inf) ? -1 : u) << endl;
      }
    }
  }

  return 0;
}