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

#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+18
using namespace std;

struct Edge {
  int trg;
  long long w;
};

struct vertex {
  int v;
  long long w;
  bool operator<(const vertex &x) const { return w < x.w; }
  bool operator>(const vertex &x) const { return w > x.w; }
};

const int MAX_V = 100000;
vector<vector<Edge>> graph(MAX_V);
vector<long long> min_cost(MAX_V, INF);
vector<bool> used(MAX_V, false);

void dijkstra(int s, int V) {
  min_cost[s] = 0;

  priority_queue<vertex, vector<vertex>, greater<vertex>> pq;
  pq.push(vertex{s, 0});
  while (!pq.empty()) {
    vertex ver = pq.top();
    pq.pop();
    int v = ver.v;
    if (used[v])
      continue;
    used[v] = true;
    min_cost[v] = ver.w;

    for (int i = 0; i < graph[v].size(); i++) {
      if (!used[graph[v][i].trg]) {
        pq.push(vertex{graph[v][i].trg, min_cost[v] + graph[v][i].w});
      }
    }
  }
}

void print_array(vector<long long> &array, int node) {
  for (int i = 0; i < node; i++) {
    if (array[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << array[i] << endl;
    }
  }
}

int main() {

  int V, E, r;
  cin >> V >> E >> r;
  int s, t, d;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> d;
    graph[s].push_back(Edge{t, d});
  }
  dijkstra(r, V);
  print_array(min_cost, V);

  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+18
using namespace std;

struct Edge {
  int trg;
  long long w;
};

struct vertex {
  int v;
  long long w;
  bool operator<(const vertex &x) const { return w < x.w; }
  bool operator>(const vertex &x) const { return w > x.w; }
};

const int MAX_V = 10000;
vector<vector<Edge>> graph(MAX_V);
vector<long long> min_cost(MAX_V, INF);
vector<bool> used(MAX_V, false);

void dijkstra(int s, int V) {
  min_cost[s] = 0;

  priority_queue<vertex, vector<vertex>, greater<vertex>> pq;
  pq.push(vertex{s, 0});
  while (!pq.empty()) {
    vertex ver = pq.top();
    pq.pop();
    int v = ver.v;
    if (used[v])
      continue;
    used[v] = true;
    min_cost[v] = ver.w;

    for (int i = 0; i < graph[v].size(); i++) {
      if (!used[graph[v][i].trg]) {
        pq.push(vertex{graph[v][i].trg, min_cost[v] + graph[v][i].w});
      }
    }
  }
}

void print_array(vector<long long> &array, int node) {
  for (int i = 0; i < node; i++) {
    if (array[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << array[i] << endl;
    }
  }
}

int main() {

  int V, E, r;
  cin >> V >> E >> r;
  int s, t, d;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> d;
    graph[s].push_back(Edge{t, d});
  }
  dijkstra(r, V);
  print_array(min_cost, V);

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A

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

int V, E, r, s, t, d;
// int K[100010][100010]; 隣接行列を使おうと思ったがメモリが怖い。
// ので、structの配列で、隣接配列を使ってみようと思う。

struct Edge {
  int cur;
  int dst;
  int cost;
};

Edge edges[500010];

long long infty = 10000 * 100000 + 100; // ゆえにC[]はlong longを入れる。
long long C[100010]; // 最短コストの「上限」。最終的には答えが入る。

int main() {
  // 初期化
  for (int i = 0; i < 100010; i++) {
    C[i] = infty;
  }
  // 入力
  scanf("%d%d%d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &d);
    edges[i] = {s, t, d};
  }
  // Bellman-Ford
  C[r] = 0; // 始点はコスト0。
  for (int i = 0; i < V - 1;
       i++) { // V-1回繰り返す。iには「特別な」意味はない。
    int change =
        0; // 最短経路がV-1個の頂点をつないでいる確率は低い。更新しなくてよくなったらもう繰り返さない。
    for (int j = 0; j < E; j++) { // すべての頂点に対し
      if (C[edges[j].dst] > C[edges[j].cur] + edges[j].cost) {
        C[edges[j].dst] = C[edges[j].cur] + edges[j].cost;
        change++;
      }
    }
    if (change == 0) {
      break;
    }
  }
  for (int i = 0; i < V; i++) {
    if (C[i] < infty) {
      printf("%lld\\n", C[i]);
    } else {
      printf("INF\\n");
    }
  }
}

Error: TLE

Fixed submission:

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

int V, E, r, s, t, d;
// int K[100010][100010]; 隣接行列を使おうと思ったがメモリが怖い。
// ので、structの配列で、隣接配列を使ってみようと思う。

struct Edge {
  int cur;
  int dst;
  int cost;
};

Edge edges[500010];

long long infty = 10000 * 100000 + 100; // ゆえにC[]はlong longを入れる。
long long C[100010]; // 最短コストの「上限」。最終的には答えが入る。

int main() {
  // 初期化
  for (int i = 0; i < 100010; i++) {
    C[i] = infty;
  }
  // 入力
  scanf("%d%d%d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &d);
    edges[i] = {s, t, d};
  }
  // Bellman-Ford
  C[r] = 0; // 始点はコスト0。
  for (int i = 0; i < V - 1;
       i++) { // V-1回繰り返す。iには「特別な」意味はない。
    for (int j = 0; j < E; j++) { // すべての頂点に対し
      if (C[edges[j].dst] > C[edges[j].cur] + edges[j].cost) {
        C[edges[j].dst] = C[edges[j].cur] + edges[j].cost;
      }
    }
  }
  for (int i = 0; i < V; i++) {
    if (C[i] < infty) {
      printf("%lld\\n", C[i]);
    } else {
      printf("INF\\n");
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;
const long long Inf = 1e+16;

void dijkstra(vector<vector<pair<long long, long long>>> &graph,
              vector<long long> &used, long long r) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  pq.push(make_pair(0, r));
  while (pq.empty() == false) {
    long long corr_score = pq.top().first; // ????????????????????????????????¢
    long long corr_v = pq.top().second;    // ?????????????????????
    pq.pop();
    if (used[corr_v] != Inf) {
      continue;
    }
    used[corr_v] = corr_score;
    for (long long i = 0; i < graph[corr_v].size(); i++) {
      long long tgt = graph[corr_v][i].first;
      long long score = graph[corr_v][i].second;
      if (used[tgt] == Inf) {
        pq.push(make_pair(score + corr_score, tgt));
      }
    }
  }
}

// dijkstra ?????¢?????´????????? ~ ??£??\\?????????????????¨
int main() {
  long long v;
  long long e;
  long long r;
  cin >> v >> e >> r;
  vector<vector<pair<long long, long long>>> graph(v);
  for (long long i = 0; i < e; i++) {
    long long s;
    long long t;
    long long d;
    cin >> s >> t >> d;
    graph[s].push_back(make_pair(t, d));
  }

  vector<long long> used(v, Inf);
  dijkstra(graph, used, r);

  for (long long i = 0; i < v; i++) {
    if (used[i] == Inf) {
      cout << "INF" << endl;
    } else {
      cout << used[i] << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
// #include <>

using namespace std;
const int Inf = 1e+8;

void dijkstra(vector<vector<int>> &graph, vector<int> &used, int r) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  pq.push(make_pair(0, r));
  while (pq.empty() == false) {
    int corr_score = pq.top().first; // ????????????????????????????????¢
    int corr_v = pq.top().second;    // ?????????????????????
    pq.pop();
    if (used[corr_v] != Inf) {
      continue;
    }
    used[corr_v] = corr_score;
    for (int i = 0; i < graph.size(); i++) {
      if (graph[corr_v][i] != Inf && used[i] == Inf) {
        pq.push(make_pair(graph[corr_v][i] + corr_score, i));
      }
    }
  }
}

// dijkstra ?????¢?????´?????????
int main() {
  int v;
  int e;
  int r;
  cin >> v >> e >> r;
  vector<vector<int>> graph(v, vector<int>(v, Inf));
  for (int i = 0; i < e; i++) {
    int s;
    int t;
    int d;
    cin >> s >> t >> d;
    graph[s][t] = d;
  }

  vector<int> used(v, Inf);
  dijkstra(graph, used, r);

  for (int i = 0; i < v; i++) {
    if (used[i] == Inf) {
      cout << "INF" << endl;
    } else {
      cout << used[i] << endl;
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int V, E, r, cur, curD, dij, dijD;
  cin >> V >> E >> r;
  vector<int> s(E), t(E), d(E), answer(V), check(V, 0);
  vector<vector<pair<int, int>>> v(V);
  for (int i = 0; i < E; ++i) {
    cin >> s[i] >> t[i] >> d[i];
    v[s[i]].push_back(make_pair(d[i], t[i]));
  }
  check[r] = 1;
  queue<int> q;
  q.push(r);
  while (!q.empty()) {
    for (int i = 0; i < v[q.front()].size(); ++i)
      if (check[v[q.front()][i].second] == 0) {
        check[v[q.front()][i].second] = 1;
        q.push(v[q.front()][i].second);
      }
    q.pop();
  }
  vector<int> finish(V, 0);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      D;
  D.push(make_pair(0, r));
  while (!D.empty()) {
    curD = D.top().first;
    cur = D.top().second;
    if (finish[cur] == 1) {
      D.pop();
      continue;
    }
    finish[cur] = 1;
    answer[cur] = curD;
    D.pop();
    for (int i = 0; i < v[cur].size(); ++i) {
      dijD = v[cur][i].first;
      dij = v[cur][i].second;
      if (finish[dij] == 0)
        D.push(make_pair(dijD + curD, dij));
    }
  }
  for (int i = 0; i < V; ++i) {
    if (check[i] == 0)
      cout << "INF" << endl;
    else
      cout << answer[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
bool finishjudge(vector<int> &finish, vector<int> &check) {
  for (int i = 0; i < finish.size(); ++i)
    if (finish[i] == 0 && check[i] == 1)
      return true;
  return false;
}
int main() {
  int V, E, r, cur, curD, dij, dijD;
  cin >> V >> E >> r;
  vector<int> s(E), t(E), d(E), answer(V), check(V, 0);
  vector<vector<pair<int, int>>> v(V);
  for (int i = 0; i < E; ++i) {
    cin >> s[i] >> t[i] >> d[i];
    v[s[i]].push_back(make_pair(d[i], t[i]));
  }
  check[r] = 1;
  queue<int> q;
  q.push(r);
  while (!q.empty()) {
    for (int i = 0; i < v[q.front()].size(); ++i)
      if (check[v[q.front()][i].second] == 0) {
        check[v[q.front()][i].second] = 1;
        q.push(v[q.front()][i].second);
      }
    q.pop();
  }
  vector<int> finish(V, 0);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      D;
  D.push(make_pair(0, r));
  while (finishjudge(finish, check)) {
    curD = D.top().first;
    cur = D.top().second;
    if (finish[cur] == 1) {
      D.pop();
      continue;
    }
    finish[cur] = 1;
    answer[cur] = curD;
    D.pop();
    for (int i = 0; i < v[cur].size(); ++i) {
      dijD = v[cur][i].first;
      dij = v[cur][i].second;
      if (finish[dij] == 0)
        D.push(make_pair(dijD + curD, dij));
    }
  }
  for (int i = 0; i < V; ++i) {
    if (check[i] == 0)
      cout << "INF" << endl;
    else
      cout << answer[i] << endl;
  }
}
Problem p02361 (C++) — buggy submission, verdict: Memory Limit Exceeded

// GRL_1_A
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
#define N 1000000000
using namespace std;

struct edge {
  int adj;
  int dis;
};

int main() {
  int V, E, r, s, t, d, x;
  vector<vector<edge>> A;
  vector<int> distance;
  queue<int> ver;
  edge e;

  cin >> V >> E >> r;

  A.resize(V);
  for (int i = 0; i < V; i++) {
    distance.push_back(N);
  }
  distance[r] = 0;

  for (int j = 0; j < E; j++) {
    cin >> s >> t >> d;
    e.adj = t;
    e.dis = d;
    A[s].push_back(e);
  }

  ver.push(r);
  while (!ver.empty()) {
    x = ver.front();
    for (int i = 0; i < A[x].size(); i++) {
      if (distance[A[x][i].adj] > distance[x] + A[x][i].dis) {
        distance[A[x][i].adj] = distance[x] + A[x][i].dis;
        ver.push(A[x][i].adj);
      }
    }
    ver.pop();
  }

  for (int i = 0; i < V; i++) {
    if (distance[i] >= N)
      cout << "INF" << endl;
    else
      cout << distance[i] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// GRL_1_A
#include <iostream>
#include <queue>
#include <vector>
#define N 10000
using namespace std;

int main() {
  int V, E, r, s, t, d, x;
  vector<vector<int>> A;
  vector<int> dis;
  queue<int> ver;

  cin >> V >> E >> r;

  A.resize(V);
  for (int i = 0; i < V; i++) {
    A[i].resize(V);
    dis.push_back(N);
  }
  dis[r] = 0;

  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      A[i][j] = N;
    }
  }

  for (int j = 0; j < E; j++) {
    cin >> s >> t >> d;
    A[s][t] = d;
  }

  ver.push(r);
  while (!ver.empty()) {
    x = ver.front();
    for (int i = 0; i < V; i++) {
      if (dis[i] > dis[x] + A[x][i]) {
        dis[i] = dis[x] + A[x][i];
        ver.push(i);
      }
    }
    ver.pop();
  }

  for (int i = 0; i < V; i++) {
    if (dis[i] >= N)
      cout << "INF" << endl;
    else
      cout << dis[i] << endl;
  }

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

#include <cstdio>
#include <iostream>

using namespace std;

const long int INF = 5000000000;
const int MAX_E = 500000;
const int MAX_V = 100000;

struct edge //??????????????????cost???(from,to)??¨??????(?????????)???
{
  int from;
  int to;
  int cost;

  edge() {
    from = 0;
    to = 0;
    cost = 0;
  }
};

// edge es[MAX_E];

int V, E, r; //??°?????????????????°,?????°,?§????
long int d[MAX_V];
edge es[MAX_E];

void shortest_path(int r) {
  for (int i = 0; i < V; i++) {
    d[i] = INF;
  }

  d[r] = 0;

  while (true) {
    bool update = false;

    for (int i = 0; i < E; i++) {
      edge e = es[i];

      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }

    if (!update)
      break;
  }
}

int main() {
  int a[10];
  // edge es[MAX_E];

  scanf(" %d %d %d", &V, &E, &r);

  // cout << endl;
  // cout << V << E << r << endl;

  for (int i = 0; i < E; i++) {
    scanf(" %d %d %d", &es[i].from, &es[i].to, &es[i].cost);
  }

  /*for(int i = 0; i < E; i++)
  {
          //scanf("%d %d %d", &es[i].from, &es[i].to, &es[i].cost);
          printf("%d %d", i,es[i].from);
          printf(" %d", es[i].to);
          printf(" %d", es[i].cost);
          cout << endl;
  }


  cout << "okok";*/

  shortest_path(r);

  for (int i = 0; i < V; i++) {
    if (d[i] == INF) {
      printf("INF\\n");
    } else {
      printf("%ld\\n", d[i]);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

const int INF = 10001;
const int MAX_E = 100000;
const int MAX_V = 100000;

struct edge //??????????????????cost???(from,to)??¨??????(?????????)???
{
  int from;
  int to;
  int cost;
};

int V, E; //??°?????????????????°,?????°
int d[MAX_V];
edge es[MAX_E];

void shortest_path(int r) {
  for (int i = 0; i < V; i++) {
    d[i] = INF;
  }

  d[r] = 0;

  while (true) {
    bool update = false;

    for (int i = 0; i < E; i++) {
      edge e = es[i];

      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }

      if (!update) {
        break;
      }
    }
  }
}

int main() {
  int r;

  scanf("%d %d %d", &V, &E, &r);

  for (int i = 0; i < E; i++) {
    scanf("%d %d %d", &es[i].from, &es[i].to, &es[i].cost);
  }

  shortest_path(r);

  for (int i = 0; i < V; i++) {
    printf("%d\\n", d[i]);
  }

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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
#define pb push_back

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define INF (1e9)

class Edge {
public:
  int u, v, cost;
  Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
  bool operator<(const Edge &e) const {
    return cost != e.cost ? cost > e.cost : u != e.u ? u < e.u : v < e.v;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

/* Dijkstra Algorithm */
void dijkstra(Graph &G, vector<int> &d, vector<int> &p, int s) {
  int V = G.size();
  priority_queue<Edge> Q;

  d[s] = 0;
  Q.push(Edge(-2, s, 0)); // Edge.cost as d
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    // if(p[e.v] != -1) continue;
    // p[e.v] = e.u;
    if (d[e.v] == INF)
      continue;
    for (auto &f : G[e.v])
      if (d[f.v] > d[f.u] + f.cost) {
        d[f.v] = d[f.u] + f.cost;
        p[f.v] = f.u;
        Q.push(Edge(f.u, f.v, d[f.u] + f.cost));
      }
  }
}

int main() {
  int V, E, s;
  cin >> V >> E >> s;
  Graph G(V);
  rep(i, E) {
    int u, v, c;
    cin >> u >> v >> c;
    G[u].pb(Edge(u, v, c));
  }

  vector<int> d(V, INF), p(V, -1);

  dijkstra(G, d, p, s);

  rep(i, V) if (d[i] == INF) cout << "INF" << endl;
  else cout << d[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
#define pb push_back

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define INF (1e9)

class Edge {
public:
  int u, v, cost;
  Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
  bool operator<(const Edge &e) const {
    return cost != e.cost ? cost < e.cost : u != e.u ? u < e.u : v < e.v;
  }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

/* Dijkstra Algorithm */
void dijkstra(Graph &G, vector<int> &d, vector<int> &p, int s) {
  int V = G.size();
  priority_queue<Edge> Q;

  d[s] = 0;
  Q.push(Edge(s, s, 0)); // Edge.cost as d
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (d[e.v] == INF)
      continue;
    for (auto &f : G[e.v])
      if (d[f.v] > d[f.u] + f.cost) {
        d[f.v] = d[f.u] + f.cost;
        p[f.v] = f.u;
        Q.push(Edge(f.u, f.v, d[f.u] + f.cost));
      }
  }
}

int main() {
  int V, E, s;
  cin >> V >> E >> s;
  Graph G(V);
  rep(i, E) {
    int u, v, c;
    cin >> u >> v >> c;
    G[u].pb(Edge(u, v, c));
  }

  vector<int> d(V, INF), p(V, -1);

  dijkstra(G, d, p, s);

  rep(i, V) if (d[i] == INF) cout << "INF" << endl;
  else cout << d[i] << endl;
}
Problem p02361 (C++) — buggy submission, verdict: Memory Limit Exceeded

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, int>;

struct edge {
  edge(int to, ll cost) : to(to), cost(cost){};

  int to;
  ll cost;
};

int V, E, r, s, t;
ll d;
vector<ll> dist;
const ll INF = 1LL << 50;

int main() {
  cin >> V >> E >> r;
  vector<edge> G[V];

  rep(i, E) {
    cin >> s >> t >> d;
    G[s].push_back(edge(t, d));
  }

  priority_queue<P, vector<P>, greater<P>> que;
  dist.resize(V);
  fill(dist.begin(), dist.end(), INF);
  dist[r] = 0;

  que.push(P(0, r));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;

    if (dist[v] < p.first)
      continue;

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

      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }

  for (ll dist_ : dist) {
    if (dist_ == INF)
      cout << "INF" << endl;
    else
      cout << dist_ << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

// #define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int V, E, r, s, t, d;
vector<long long> dist;
vector<bool> visited;
vector<vector<long long>> cost;
const long long INF = 1LL << 50;

int main() {
  cin >> V >> E >> r;
  cost.resize(V);
  rep(i, V) cost[i].resize(V);
  rep(i, V) fill(cost[i].begin(), cost[i].end(), INF);
  dist.resize(V);
  fill(dist.begin(), dist.end(), INF);
  visited.resize(V);
  fill(visited.begin(), visited.end(), false);

  rep(i, E) {
    cin >> s >> t >> d;
    cost[s][t] = d;
  }

  dist[r] = 0;

  while (true) {
    int v = -1;

    // ?????????????????????
    rep(u, V) {
      if (!visited[u] && (v == -1 || dist[u] < dist[v]))
        v = u;
    }

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

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

  for (long long dist_ : dist) {
    if (dist_ == INF)
      cout << "INF" << endl;
    else
      cout << dist_ << endl;
  }
  return 0;
}
Problem p02361 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define to first
#define wgt second

using namespace std;

const int MAXN = 100100;

typedef pair<int, int> pii;
typedef vector<pii> vec;
typedef vector<vec> Graph;

Graph G(MAXN);
int n, d[MAXN];

void SPFA(int s) {
  queue<int> que;
  fill(d, d + n, INT_MAX);
  d[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < G[u].size(); i++)
      if (d[G[u][i].to] > d[u] + G[u][i].wgt) {
        d[G[u][i].to] = d[u] + G[u][i].wgt;
        que.push(G[u][i].to);
      }
  }
}

int main() {
  int e, r;
  scanf("%d %d %d", &n, &e, &r);
  for (int i = 0; i < e; i++) {
    int s, t, d;
    scanf("%d%d%d", &s, &t, &d);
    G[s].push_back(make_pair(t, d));
  }
  SPFA(r);
  for (int i = 0; i < n; i++)
    if (d[i] == INT_MAX)
      printf("INF\\n");
    else
      printf("%d\\n", d[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define to first
#define wgt second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vec;
typedef vector<vec> Graph;

Graph G(11000);
int n, d[11000];

void SPFA(int s) {
  queue<int> que;
  fill(d, d + n, INT_MAX);
  d[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < G[u].size(); i++)
      if (d[G[u][i].to] > d[u] + G[u][i].wgt) {
        d[G[u][i].to] = d[u] + G[u][i].wgt;
        que.push(G[u][i].to);
      }
  }
}

int main() {
  int e, r;
  scanf("%d %d %d", &n, &e, &r);
  for (int i = 0; i < e; i++) {
    int s, t, d;
    scanf("%d%d%d", &s, &t, &d);
    G[s].push_back(make_pair(t, d));
  }
  SPFA(r);
  for (int i = 0; i < n; i++)
    if (d[i] == INT_MAX)
      printf("INF\\n");
    else
      printf("%d\\n", d[i]);
  return 0;
}