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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

#define INF 100000000
int s[101][101];

int main() {

  while (1) {

    int n, k, p, a, b, c, d, e;

    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    REP(i, 1, n) REP(j, 1, n) if (i != j) s[i][j] = INF;

    rep(i, k) {

      cin >> p;
      if (p == 0) {

        cin >> a >> b;

        vector<int> cost, used;

        rep(j, n + 1) {
          if (j != a)
            cost.push_back(INF);
          else
            cost.push_back(0);
          used.push_back(0);
        }

        for (;;) {

          int x;
          int m = INF;
          REP(j, 1, n) {

            if (!used[j] && cost[j] < m) {
              m = cost[j];
              x = j;
            }
          }

          if (x == b || m == INF)
            break;

          used[x] = 1;

          REP(j, 1, n) cost[j] = min(cost[j], cost[x] + s[x][j]);
        }

        if (cost[b] == INF)
          cout << -1 << endl;
        else
          cout << cost[b] << endl;

        cost.clear();
        used.clear();
      }

      if (p == 1) {

        cin >> c >> d >> e;

        if (s[c][d] > e) {
          s[c][d] = e;
          s[d][c] = e;
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

#define INF 10000000
int s[101][101];

int main() {

  while (1) {

    int n, k, p, a, b, c, d, e;

    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    REP(i, 1, n) REP(j, 1, n) if (i != j) s[i][j] = INF;

    rep(i, k) {

      cin >> p;
      if (p == 0) {

        cin >> a >> b;

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

      if (p == 1) {

        cin >> c >> d >> e;

        if (s[c][d] > e)
          s[c][d] = e;
        s[d][c] = e;

        REP(k, 1, n) {
          REP(i, 1, n) {
            REP(j, 1, n) { s[i][j] = min(s[i][j], s[i][k] + s[k][j]); }
          }
        }
      }
    }
  }

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

#include <iostream>
#define INF 1 << 30
using namespace std;

void newboat(int cost[101][101]) {

  int c, d, e;
  cin >> c >> d >> e;

  if (cost[c][d] > e) {
    cost[c][d] = e;
    cost[d][c] = e;
  }
  return;
}

void go(int n, int b, int cost[101][101], int access[], bool used[],
        bool though[101][101]) {
  int minaccess = INF, minisland;

  for (int i = 1; i <= n; i++) {
    if (minaccess > access[i] && !used[i]) {
      minaccess = access[i];
      minisland = i;
    }
  }
  if (minaccess >= access[b]) {
    return;
  }
  used[minisland] = true;
  for (int i = 1; i <= n; i++) {
    if (cost[minisland][i] != INF && !though[minisland][i]) {
      though[minisland][i] = true;
      though[i][minisland] = true;
      if (access[i] > access[minisland] + cost[minisland][i]) {
        access[i] = access[minisland] + cost[minisland][i];
      }
    }
  }

  go(n, b, cost, access, used, though);
  return;
}

int solve(int n, int cost[101][101]) {
  int a, b, access[101];
  bool used[101], though[101][101];
  cin >> a >> b;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      though[i][j] = false;
    }
    used[i] = false;
    access[i] = INF;
  }

  access[a] = 0;

  go(n, b, cost, access, used, though);
  return (access[b] != INF) ? access[b] : -1;
}

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (!n && !k) {
      break;
    }

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

    for (int i = 0; i < k; i++) {
      bool how;
      cin >> how;
      if (how) {
        newboat(cost);
      } else {
        cout << solve(n, cost) << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void newboat(int map[101][101]) {
  int c, d, e;
  cin >> c >> d >> e;
  if (!map[c][d] || map[c][d] > e) {
    map[c][d] = e;
    map[d][c] = e;
  }
  return;
}

void go(int n, int a, int now, int map[101][101], int route[]) {
  for (int i = 1; i <= n; i++) {
    if (now == i || i == a) {
      continue;
    }
    if (map[now][i] && (!route[i] || route[i] > route[now] + map[now][i])) {
      route[i] = route[now] + map[now][i];
      go(n, a, i, map, route);
    }
  }
  return;
}

int solve(int n, int map[101][101]) {
  int a, b;
  cin >> a >> b;
  int route[101];
  for (int i = 1; i <= n; i++) {
    route[i] = 0;
  }

  go(n, a, a, map, route);
  return (!route[b]) ? -1 : route[b];
}

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (!n && !k) {
      break;
    }

    int map[101][101]; // not use each(a||b)==0
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
        map[i][j] = 0;
      }
    }

    for (int i = 0; i < k; i++) {
      bool how;
      cin >> how;
      if (how) {
        newboat(map);
      } else {
        cout << solve(n, map) << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#define INF 1 << 28
#define F first
#define S second
using namespace std;

typedef pair<int, int> P;
vector<P> g[111];
int ans[111];
int n;

int solve(int a) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(ans, ans + n + 1, INF);

  ans[a] = 0;
  que.push(P(0, a));

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    if (ans[p.S] < p.F)
      continue;
    for (int i = 0; i < g[p.S].size(); i++) {
      P pp = g[p.S][i];
      if (ans[pp.F] > ans[p.S] + pp.S) {
        ans[pp.F] = ans[p.S] + pp.S;
        que.push(P(ans[pp.F], pp.F));
      }
    }
  }
}

int main() {

  int k;
  int q, a, b, c;

  while (true) {
    cin >> n >> k;
    if (!n && !k)
      break;

    for (int i = 0; i < 111; i++)
      g[i].clear();

    while (k--) {
      cin >> q;
      if (q) {
        cin >> a >> b >> c;
        g[a].push_back(P(b, c));
        g[b].push_back(P(a, c));
      } else {
        cin >> a >> b;
        solve(a);

        if (ans[b] == INF)
          cout << -1 << endl;
        else
          cout << ans[b] << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1 << 28
#define MAX_V 111

struct edge {
  int to, cost; // 次に行く場所、その時かかるコスト
  edge(int to, int cost) : to(to), cost(cost){};
};

typedef pair<int, int> P; // 最短距離、頂点の番号

int V;                  // 頂点数
vector<edge> G[MAX_V];  // 今いる場所
long long int d[MAX_V]; // 頂点sからの最短距離
int m;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>>
      que; // firstが小さい順になるようにする

  d[s] = 0; // スタート地点の最小コストは0
  que.push(P(d[s], s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second; // 頂点を取り出す

    if (d[v] < p.first)
      continue; // その頂点の最小コストより次に行くコストが高かったら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 a, b, c;
  while (true) {
    cin >> V >> m; // 頂点数、入力数
    if (!V && !m)
      break;

    fill(d, d + MAX_V, INF); // d(最小コスト)をINFで初期化
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();

    for (int i = 0; i < m; i++) {
      cin >> a;

      if (a) {
        cin >> a >> b >> c;
        G[a].push_back(edge(b, c));
        G[b].push_back(edge(a, c));
      }

      else if (!a) {
        fill(d, d + MAX_V, INF);
        cin >> a >> b;

        for (int k = 0; k < MAX_V; k++) { // ノード数までループ
          int n = G[k].size();
          for (int j = 0; j < n; j++) { // そのノードからいけるノード数までループ
            G[G[k][j].to].push_back(edge(i, G[k][j].cost)); // hogehogeを突っ込む
            G[k].push_back(edge(G[k][j].to, G[k][j].cost));
          }
        }

        dijkstra(a);

        if (d[b] == INF)
          cout << -1 << endl;
        else
          cout << d[b] << endl;
      }
    }
  }

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

// AOJ 0526
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int INF = 1 << 30;
typedef std::pair<int, int> P;

int n, k;
std::vector<P> G[100];
int d[100];

void dijkstra(int s) {
  std::fill(d, d + n, INF);
  std::priority_queue<P> pque;
  pque.push(P(0, s));
  d[s] = 0;

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();
    int dist = -p.first, v = p.second;

    if (d[v] < dist)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        pque.push(P(-d[e.first], e.first));
      }
    }
  }

  return;
}

int main() {
  while (scanf("%d%d", &n, &k), n | k) {
    rep(i, 100) G[i].clear();

    rep(i, k) {
      int t;
      scanf("%d", &t);

      if (t) {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        --c;
        --d;

        G[c].push_back(P(d, e));
        G[d].push_back(P(c, e));
      } else {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;

        dijkstra(a);

        printf("%d\\n", d[b] == INF ? -1 : d[b]);
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

// AOJ 0526
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int INF = 1 << 28;
typedef std::pair<int, int> P;

int n, k;
std::vector<P> G[100];
int d[100];

void dijkstra(int s) {
  std::fill((int *)d, (int *)(d + n), INF);
  std::priority_queue<P> pque;
  pque.push(P(0, s));
  d[s] = 0;

  while (!pque.empty()) {
    P p = pque.top();
    pque.pop();
    int dist = -p.first, v = p.second;

    if (d[v] < dist)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        pque.push(P(-d[e.first], e.first));
      }
    }
  }

  return;
}

int main() {
  while (scanf("%d%d", &n, &k), n | k) {
    rep(i, n) G[i].clear();

    rep(i, k) {
      int t;
      scanf("%d", &t);

      if (t) {
        int c, d, e;
        scanf("%d%d%d", &c, &d, &e);
        --c;
        --d;

        bool fl = false;
        rep(j, G[c].size()) {
          if (G[c][j].first == d) {
            G[c][j] = std::min(G[c][j], P(d, e));
            G[d][j] = std::min(G[d][j], P(c, e));
            fl = true;
          }
        }

        if (!fl) {
          G[c].push_back(P(d, e));
          G[d].push_back(P(c, e));
        }
      } else {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;

        dijkstra(a);

        printf("%d\\n", d[b] == INF ? -1 : d[b]);
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#define F 114514 * 1000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, k, f, t, a, b;
int q[1000], x[1000], head, tail;
int g[100][100], r[100];
void BFS(int p, int y) {
  rep(i, n) if (y + g[p][i] < r[i]) q[tail] = i, x[tail] = y + g[p][i],
                                    r[i] = x[tail], tail++;
  return;
}
int main() {
  while (true) {
    cin >> n >> k;
    if (!n && !k)
      break;
    rep(i, n) rep(j, n) g[i][j] = F;
    rep(u, k) {
      cin >> t >> a >> b;
      a--, b--;
      if (t) {
        cin >> t;
        g[a][b] = min(t, g[a][b]), g[b][a] = g[a][b];
      } else {
        rep(i, n) r[i] = F;
        head = 0, tail = 1, q[0] = a, x[0] = 0;
        while (true) {
          if (head == tail)
            break;
          BFS(q[head], x[head]);
          head++;
        }
        if (r[b] == F)
          r[b] = -1;
        cout << r[b] << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#define F 114514 * 1000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, k, f, t, a, b;
int q[10000], x[10000], head, tail;
int g[101][101], r[101];
void BFS(int p, int y) {
  r[p] = y;
  rep(i, n) if (y + g[p][i] < r[i]) q[tail] = i, x[tail] = y + g[p][i], tail++;
  return;
}
int main() {
  while (true) {
    cin >> n >> k;
    if (!n && !k)
      break;
    rep(i, n) rep(j, n) g[i][j] = F;
    rep(u, k) {
      cin >> t >> a >> b;
      a--, b--;
      if (t) {
        cin >> t;
        g[a][b] = min(t, g[a][b]), g[b][a] = g[a][b];
      } else {
        rep(i, n) r[i] = F;
        head = 0, tail = 1, q[0] = a, x[0] = 0;
        while (true) {
          if (head == tail)
            break;
          BFS(q[head], x[head]);
          head++;
        }
        if (r[b] == F)
          r[b] = -1;
        cout << r[b] << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

#define NONE INT_MAX

struct Data {
  int id, cost;
};
bool operator<(Data a, Data b) { return a.cost > b.cost; }

vector<vector<int>> A;

int Dijkstra(int start, int goal) {
  vector<int> cost(A.size(), NONE);
  priority_queue<Data> q;
  set<int> used;

  Data node = {start, 0};
  q.push(node);
  cost[start] = 0;

  while (q.size()) {
    node = q.top();
    q.pop();

    if (node.id == goal)
      return node.cost;
    if (used.count(node.id))
      continue;
    else
      used.insert(node.id);

    for (int i = 0; i < A[node.id].size(); i++) {
      if (used.count(i))
        continue;
      if (A[node.id][i] == NONE)
        continue;
      if (cost[i] <= cost[node.id] + A[node.id][i])
        continue;
      Data next = {i, cost[node.id] + A[node.id][i]};
      cost[i] = next.cost;
      q.push(next);
    }
  }

  return -1;
}

int main() {
  int n, k, a, b, c, d, e;
  while (cin >> n >> k, n | k) {
    A.resize(n);
    for (int i = 0; i < n; i++) {
      A[i].resize(n);
      for (int j = 0; j < n; j++)
        A[i][j] = NONE;
    }
    for (int i = 0; i < k; i++) {
      cin >> a;
      if (a == 0) {
        cin >> a >> b;
        a--;
        b--;
        cout << Dijkstra(a, b) << endl;
      } else if (a == 1) {
        cin >> c >> d >> e;
        c--;
        d--;
        if (A[c][d] > e)
          A[c][d] = A[d][c] = e;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Node {
  vector<int> edgesTo;
  vector<int> edgesCost;
  int cost;
};
bool operator<(Node a, Node b) { return a.cost < b.cost; }
bool operator>(Node a, Node b) { return a.cost > b.cost; }

vector<Node> nodes;

int Dijkstra(int start, int goal) {
  priority_queue<Node> q;

  nodes[start].cost = 0;
  q.push(nodes[start]);

  while (q.size()) {
    Node doneNode = q.top();
    q.pop();
    for (int i = 0; i < doneNode.edgesTo.size(); i++) {
      int to = doneNode.edgesTo[i];
      int cost = doneNode.cost + doneNode.edgesCost[i];
      if (nodes[to].cost < 0 || cost < nodes[to].cost) {
        nodes[to].cost = cost;
        q.push(nodes[to]);
      }
    }
  }
  return nodes[goal].cost;
}

int main() {
  int n, k, a, b, c, d, e;
  while (cin >> n >> k, n | k) {
    nodes.clear();
    nodes.resize(n);
    for (int i = 0; i < k; i++) {
      cin >> a;
      if (a == 0) {
        cin >> a >> b;
        a--;
        b--;
        for (int i = 0; i < n; i++)
          nodes[i].cost = -1;
        cout << Dijkstra(a, b) << endl;
      } else if (a == 1) {
        cin >> c >> d >> e;
        c--;
        d--;
        nodes[c].edgesTo.push_back(d);
        nodes[c].edgesCost.push_back(e);
        nodes[d].edgesTo.push_back(c);
        nodes[d].edgesCost.push_back(e);
      }
    }
    cout << endl;
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int INF = 1000000000;
int n, k;
vector<P> way[128];

int query(int a, int b);

int main() {
  while (scanf("%d %d", &n, &k), n != 0) {
    for (int i = 1; i <= n; i++)
      way[i].clear();
    while (k-- > 0) {
      int mode;
      scanf("%d", &mode);

      if (mode == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        int cost = query(a, b);
        if (cost == INF)
          puts("-1");
        else
          printf("%d\\n", cost);
      } else {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        way[c].push_back(P(e, d));
        way[d].push_back(P(e, c));
      }
    }
  }

  return 0;
}

int query(int a, int b) {
  static int mini[128];
  for (int i = 1; i <= n; i++)
    mini[i] = INF;

  priority_queue<P> q;
  q.push(P(0, a));
  mini[a] = 0;
  while (!q.empty()) {
    P p = q.top();
    q.pop();

    if (mini[p.second] < p.first)
      continue;
    for (int i = 0; i < way[p.second].size(); i++) {
      P tmp = way[p.second][i];

      if (mini[tmp.second] > mini[p.second] + tmp.first) {
        mini[tmp.second] = mini[p.second] + tmp.first;
        q.push(tmp);
      }
    }
  }

  return mini[b];
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int INF = 1000000000;
int n, k;
vector<P> way[128];

int query(int a, int b);

int main() {
  while (scanf("%d %d", &n, &k), n != 0) {
    for (int i = 1; i <= n; i++)
      way[i].clear();
    while (k-- > 0) {
      int mode;
      scanf("%d", &mode);

      if (mode == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        int cost = query(a, b);
        if (cost == INF)
          puts("-1");
        else
          printf("%d\\n", cost);
      } else {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        way[c].push_back(P(e, d));
        way[d].push_back(P(e, c));
      }
    }
  }

  return 0;
}

int query(int a, int b) {
  int mini[128];
  fill(mini, mini + n + 1, INF);

  priority_queue<P> q;
  q.push(P(0, a));
  mini[a] = 0;
  while (!q.empty()) {
    P p = q.top();
    q.pop();

    if (mini[p.second] < p.first)
      continue;
    for (int i = 0; i < way[p.second].size(); i++) {
      P tmp = way[p.second][i];

      if (mini[tmp.second] >= mini[p.second] + tmp.first) {
        mini[tmp.second] = mini[p.second] + tmp.first;
        q.push(tmp);
      }
    }
  }

  return mini[b];
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#define INF 10000000

using namespace std;

int main() {

  int n, m;
  int i, j, k, s;
  int r, a, b, c, d, e;
  int cost[102][102];

  while (1) {

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        cost[i][j] = INF;

    for (s = 0; s < m; s++) {
      cin >> r;

      if (r == 0) {
        cin >> a >> b;
        if (cost[a][b] == INF)
          cout << -1 << endl;
        else
          cout << cost[a][b] << endl;
      } else {
        cin >> c >> d >> e;

        if (cost[c][d] >= e) {
          cost[c][d] = cost[d][c] = e;

          for (i = 1; i < n + 1; i++) {
            for (j = 1; j < n + 1; j++) {
              for (k = 1; k < n + 1; k++) {
                cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
              }
            }
          }
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#define INF 10000000

using namespace std;

int main() {

  int n, m;
  int i, j, k, s;
  int r, a, b, c, d, e;
  int cost[102][102];

  while (1) {

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        cost[i][j] = INF;

    for (s = 0; s < m; s++) {
      cin >> r;

      if (r == 0) {
        cin >> a >> b;
        if (cost[a][b] == INF)
          cout << -1 << endl;
        else
          cout << cost[a][b] << endl;
      } else {
        cin >> c >> d >> e;

        if (cost[c][d] >= e)
          cost[c][d] = cost[d][c] = e;

        for (i = 1; i < n + 1; i++) {
          for (j = 1; j < n + 1; j++) {
            for (k = 1; k < n + 1; k++) {
              cost[j][k] = min(cost[j][k], cost[j][i] + cost[i][k]);
            }
          }
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;

#define INF 11451419
#define MAX_V 1010

int cost[MAX_V][MAX_V];
int V;

int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;

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

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

int main() {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    V = N;
    rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
    rep(i, K) {
      int op;
      cin >> op;
      if (op == 0) {
        int a, b;
        cin >> a >> b;
        dijkstra(a - 1);

        int x = d[b - 1];
        cout << (x == INF ? -1 : x) << "\\n";
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
        cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;

#define INF 11451419
#define MAX_V 1010

int cost[MAX_V][MAX_V];
int V;

int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;

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

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

int main() {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    V = N * (N - 1) / 2;
    rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
    rep(i, K) {
      int op;
      cin >> op;
      if (op == 0) {
        int a, b;
        cin >> a >> b;
        dijkstra(a - 1);

        int x = d[b - 1];
        cout << (x == INF ? -1 : x) << "\\n";
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
        cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
      }
    }
  }
  return 0;
}