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

#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pii;
namespace MyLib {

class Dijkstra {
  struct edge {
    int to, cost;
    edge(int to_, int cost_) : to(to_), cost(cost_) {}
  };
  bool isdirected;
  vector<int> d;
  vector<vector<edge>> G;
  int V, E;

public:
  Dijkstra(int _V, bool _isdirected = true)
      : isdirected(_isdirected), V(_V), E(0), d(_V), G(_V) {}
  void add(int a, int b, int c) {
    G[a].push_back(edge(b, c));
    if (!isdirected)
      G[b].push_back(edge(a, c));
    E++;
  }
  void calc(int start) {
    fill(d.begin(), d.end(), INF);
    priority_queue<pii, vector<pii>, greater<pii>> que;
    d[start] = 0;
    que.push(pii(0, start));

    while (!que.empty()) {
      pii p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (size_t 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(pii(d[e.to], e.to));
        }
      }
    }
  }
  int dist(int to) { return d[to]; }
};
} // namespace MyLib
using namespace MyLib;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  Dijkstra dk(v);
  for (int i = 0, s, t, d; i < e; i++) {
    cin >> s >> t >> d;
    dk.add(s, t, d);
  }
  dk.calc(r);
  for (int i = 0; i < v; i++) {
    if (dk.dist(i) == INF)
      cout << "INF" << endl;
    else
      cout << dk.dist(i) << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pii;
namespace MyLib {

class Dijkstra {
  struct edge {
    int to, cost;
    edge(int to_, int cost_) : to(to_), cost(cost_) {}
  };
  bool isdirected;
  vector<int> d;
  vector<vector<edge>> G;
  int V, E;

public:
  Dijkstra(int _V, bool _isdirected = true)
      : isdirected(_isdirected), V(_V), E(0), d(V), G(V) {}
  void add(int a, int b, int c) {
    G[a].push_back(edge(b, c));
    if (!isdirected)
      G[b].push_back(edge(a, c));
    E++;
  }
  void calc(int start) {
    fill(d.begin(), d.end(), INF);
    priority_queue<pii, vector<pii>, greater<pii>> que;
    d[start] = 0;
    que.push(pii(0, start));

    while (!que.empty()) {
      pii p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (size_t 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(pii(d[e.to], e.to));
        }
      }
    }
  }
  int dist(int to) { return d[to]; }
};
} // namespace MyLib
using namespace MyLib;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  Dijkstra dk(v);
  for (int i = 0, s, t, d; i < e; i++) {
    cin >> s >> t >> d;
    dk.add(s, t, d);
  }
  dk.calc(r);
  for (int i = 0; i < v; i++) {
    if (dk.dist(i) == INF)
      cout << "INF" << endl;
    else
      cout << dk.dist(i) << endl;
  }
  return 0;
}
Problem p02361 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

struct Node;

struct Edge {
  int node;
  int cost;
};

struct Node {
  bool done = false;
  int cost = -1;
  std::vector<Edge> edges;
};

struct ProcessTarget {
  int node;
  int cost;
};

bool compareTargets(const ProcessTarget &a, const ProcessTarget &b) {
  return a.cost > b.cost;
}

int main() {
  int nv, ne, r;
  cin >> nv >> ne >> r;
  vector<Node> V(nv);
  for (int i = 0; i < ne; ++i) {
    int begin, end, cost;
    cin >> begin >> end >> cost;
    Edge edge;
    edge.cost = cost;
    edge.node = end;
    V[begin].edges.push_back(edge);
  }
  priority_queue<ProcessTarget, vector<ProcessTarget>,
                 decltype(&compareTargets)>
      queue(compareTargets);
  ProcessTarget first;
  first.node = r;
  first.cost = 0;
  queue.push(first);
  V[r].cost = 0;

  while (!queue.empty()) {
    int processing = queue.top().node;
    queue.pop();

    for (auto &edge : V[processing].edges) {
      int newCost = V[processing].cost + edge.cost;
      if (V[edge.node].cost < 0 || newCost < V[edge.node].cost) {
        V[edge.node].cost = newCost;
        ProcessTarget next;
        next.node = edge.node;
        next.cost = newCost;
        queue.push(next);
      }
    }
  }

  for (auto &v : V) {
    if (v.cost == -1) {
      cout << "INF" << endl;
    } else {
      cout << v.cost << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

struct Node;

struct Edge {
  int node;
  int cost;
};

struct Node {
  bool done = false;
  int cost = -1;
  std::vector<Edge> edges;
};

int main() {
  int nv, ne, r;
  cin >> nv >> ne >> r;
  vector<Node> V(nv);
  for (int i = 0; i < ne; ++i) {
    int begin, end, cost;
    cin >> begin >> end >> cost;
    Edge edge;
    edge.cost = cost;
    edge.node = end;
    V[begin].edges.push_back(edge);
  }
  V[r].cost = 0;

  while (true) {
    // find closest node
    int closest = -1;
    for (int i = 0; i < nv; ++i) {
      if (!V[i].done && V[i].cost >= 0) {
        if (closest < 0 || V[i].cost < V[closest].cost) {
          closest = i;
        }
      }
    }
    if (closest < 0) {
      break; // done
    }
    V[closest].done = true;

    for (auto &edge : V[closest].edges) {
      // set distances
      int newCost = V[closest].cost + edge.cost;
      if (V[edge.node].cost < 0 || newCost < V[edge.node].cost) {
        V[edge.node].cost = newCost;
      }
    }
  }

  for (auto &v : V) {
    if (v.cost == -1) {
      cout << "INF" << endl;
    } else {
      cout << v.cost << endl;
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long int llg;

template <typename T>
using reverse_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

const int INF = 1000001000;
#define REP(var, count) for (int var = 0; var < count; var++)

int vert[100010];
vector<pair<int, int>> edge[500010];

int main(void) {
  int V, E, r;
  scanf("%d", &V);
  scanf("%d", &E);
  scanf("%d", &r);

  REP(v, V) { vert[v] = INF; }
  vert[r] = 0;

  REP(i, E) {
    int s, t, d;
    scanf("%d", &s);
    scanf("%d", &t);
    scanf("%d", &d);

    edge[s].push_back(make_pair(t, d));
  }

  reverse_queue<pair<int, int>> que;
  que.push(make_pair(0, r));

  while (que.size()) {
    auto nx = que.top();
    que.pop();

    int s = nx.second;

    for (auto e : edge[s]) {
      int t = e.first;
      int d = e.second;
      if (vert[s] + d < vert[t]) {
        vert[t] = vert[s] + d;
        que.push(make_pair(vert[t], t));
      }
    }
  }

  REP(v, V) {
    if (vert[v] == INF) {
      cout << "INF" << endl;
    } else {
      cout << vert[v] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long int llg;

const int INF = 1000001000;
#define REP(var, count) for (int var = 0; var < count; var++)

int vert[100010];
vector<pair<int, int>> edge[500010];

int main(void) {
  int V, E, r;
  scanf("%d", &V);
  scanf("%d", &E);
  scanf("%d", &r);

  REP(v, V) { vert[v] = INF; }
  vert[r] = 0;

  REP(i, E) {
    int s, t, d;
    scanf("%d", &s);
    scanf("%d", &t);
    scanf("%d", &d);

    edge[s].push_back(make_pair(t, d));
  }

  priority_queue<pair<int, int>> que;
  que.push(make_pair(0, r));

  while (que.size()) {
    auto nx = que.top();
    que.pop();

    int s = nx.second;

    for (auto e : edge[s]) {
      int t = e.first;
      int d = e.second;
      if (vert[s] + d < vert[t]) {
        vert[t] = vert[s] + d;
        que.push(make_pair(vert[t], t));
      }
    }
  }

  REP(v, V) {
    if (vert[v] == INF) {
      cout << "INF" << endl;
    } else {
      cout << vert[v] << endl;
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int V, E, R, S[500010], T[500010], D[500010];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    W;
int C[100010];
const int Inf = 10000 * 100000 + 100;

int main() {
  cin >> V >> E >> R;
  for (int i = 0; i < E; ++i) {
    cin >> S[i] >> T[i] >> D[i];
  }

  vector<vector<int>> edges(V);
  for (int i = 0; i < E; ++i) {
    edges[S[i]].push_back(i);
  }

  for (int i = 0; i < V; ++i) {
    C[i] = Inf;
  }
  C[R] = 0;
  for (int i = 0; i < V; ++i) {
    W.push(make_pair(C[i], i));
  }

  unordered_set<int> q, doubled;
  for (int i = 0; i < V; ++i)
    q.insert(i);

  while (!q.empty()) {
    int u = W.top().second;
    W.pop();
    if (doubled.find(u) != doubled.end())
      continue;
    doubled.insert(u);
    q.erase(u);
    for (const auto &e : edges[u]) {
      int v = T[e];
      if (C[u] + D[e] < C[v] && q.find(v) != q.end()) {
        C[v] = C[u] + D[e];
        W.push(make_pair(C[v], v));
      }
    }
  }

  for (int t = 0; t < V; ++t) {
    if (C[t] == Inf)
      cout << "INF" << endl;
    else
      cout << C[t] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
const int inf = 100000 * 100000 + 10;
int s[500000 + 10];
int t[500000 + 10];
int d[500000 + 10];
long int dis[100000 + 10];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    pq;
vector<int> q;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  for (int i = 0; i < e; i++) {
    cin >> s[i] >> t[i] >> d[i];
  }

  for (int i = 0; i < v; i++) {
    dis[i] = inf;
    q.push_back(i);
  }
  dis[r] = 0;

  while (!q.empty()) {

    int tempv = 0, tempd = inf, tempi = 0;
    for (int i = 0; i < q.size(); i++) {
      if (dis[q[i]] < tempd || dis[q[i]] == 0) {
        tempd = dis[q[i]];
        tempv = q[i];
        tempi = i;
      }
    }

    q.erase(q.begin() + tempi);

    for (int i = 0; i < e; i++) {
      if (s[i] == tempv)
        pq.push(make_pair(d[i], t[i]));
    }

    while (!pq.empty()) {
      pair<int, int> temp = pq.top();
      pq.pop();
      dis[temp.second] = min(dis[temp.second], dis[tempv] + temp.first);
    }
  }

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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

struct edge {
  int to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void dijkstra(int s) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + V + 10, llmax);
  d[s] = 0;
  que.push(pii(0, s));

  while (!que.empty()) {
    pii 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(pii(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int E, r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    edge e;
    e.to = t;
    e.cost = d;
    G[s].push_back(e);
  }
  dijkstra(r);
  for (int i = 0; i < V; i++) {
    if (d[i] == llmax) {
      cout << "INF" << endl;
    } else {
      cout << d[i] << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

struct edge {
  int to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

void bellmanFord(int s) {
  fill(d, d + V + 10, intmax);
  d[s] = 0;
  for (int i = 0; i < V - 1; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < G[j].size(); k++) {
        edge e = G[j][k];
        d[e.to] = min(d[e.to], d[j] + e.cost);
      }
    }
  }
}

int main() {
  int E, r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    edge e;
    e.to = t;
    e.cost = d;
    G[s].push_back(e);
  }
  bellmanFord(r);
  for (int i = 0; i < V; i++) {
    if (d[i] == intmax) {
      cout << "INF" << endl;
    } else {
      cout << d[i] << endl;
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int iv, ie, r;
  cin >> iv >> ie >> r;
  int d[100010];
  for (int i = 0; i < iv; i++) {
    d[i] = 1000000010;
  }
  d[r] = 0;
  vector<pair<int, int>> w(ie);
  vector<int> cost(ie);
  for (int i = 0; i < ie; i++) {
    int s0, e0, d0;
    cin >> s0 >> e0 >> d0;
    w[i] = make_pair(s0, e0);
    cost[i] = d0;
  }
  for (int i = 0; i < iv - 1; i++) {
    int k = 0;
    for (int j = 0; j < ie; j++) {
      int s1, e1, d1;
      s1 = w[j].first;
      e1 = w[j].second;
      d1 = cost[j];
      if (d[e1] > d[s1] + d1)
        k += 1;
      d[e1] = min(d[e1], d[s1] + d1);
    }
    if (k == 0)
      break;
  }

  for (int i = 0; i < iv; i++) {
    if (d[i] == 1000000010) {
      cout << "INF" << endl;
    } else {
      cout << d[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int iv, ie, r;
  cin >> iv >> ie >> r;
  int d[100010];
  for (int i = 0; i < iv; i++) {
    d[i] = 100000000;
  }
  d[r] = 0;
  vector<pair<int, int>> w(ie);
  vector<int> cost(ie);
  for (int i = 0; i < ie; i++) {
    int s0, e0, d0;
    cin >> s0 >> e0 >> d0;
    w[i] = make_pair(s0, e0);
    cost[i] = d0;
  }
  for (int i = 0; i < iv - 1; i++) {
    for (int j = 0; j < ie; j++) {
      int s1, e1, d1;
      s1 = w[j].first;
      e1 = w[j].second;
      d1 = cost[j];
      d[e1] = min(d[e1], d[s1] + d1);
    }
  }

  for (int i = 0; i < iv; i++) {
    if (d[i] == 100000000) {
      cout << "INF" << endl;
    } else {
      cout << d[i] << endl;
    }
  }
}
Problem p02361 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long lint;

#define MAXN 100010
// d,edge,g,P,q
lint d[MAXN];
struct edge {
  int to;
  lint cost;
};
vector<edge> g[MAXN];
typedef pair<lint, int> P;
priority_queue<P, vector<P>, greater<P>> q;

void pfs(int s) {
  d[s] = 0;
  q.push({0, s});
  lint cd, v;
  while (q.size()) {
    cd = q.top().first;
    v = q.top().second;
    q.pop();
    if (cd > d[v])
      continue;
    for (edge &e : g[v])
      if (d[e.to] > cd + e.cost) {
        d[e.to] = cd + e.cost;
        q.push({d[e.to], e.to});
      }
  }
}

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

int main() {
  lint v, e, r;
  cin >> v >> e >> r;
  rep(i, v) d[i] = INT_MAX;
  int a, b;
  lint c;
  rep(i, e) {
    cin >> a >> b >> c;
    g[a].push_back({b, c});
  }
  pfs(r);
  rep(i, v) {
    if (d[i] == INT_MAX)
      cout << "INF" << endl;
    else
      cout << d[i] << endl;
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long lint;

#define MAXN 100010
// d,edge,g,P,q
lint d[MAXN];
struct edge {
  int to;
  lint cost;
};
vector<edge> g[MAXN];
typedef pair<lint, int> P;
priority_queue<P, vector<P>, greater<P>> q;

void pfs(int s) {
  d[s] = 0;
  q.push({0, s});
  lint cd, v;
  while (q.size()) {
    cd = q.top().first;
    v = q.top().second;
    q.pop();
    if (cd > d[v])
      continue;
    for (edge &e : g[v])
      if (d[e.to] > cd + e.cost) {
        d[e.to] = cd + e.cost;
        q.push({d[e.to], e.to});
      }
  }
}

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

int main() {
  lint v, e, r;
  cin >> v >> e >> r;
  rep(i, v) d[i] = INT_MAX;
  int a, b;
  lint c;
  rep(i, e) {
    cin >> a >> b >> c;
    g[a].push_back({b, c});
  }
  pfs(r);
  rep(i, v) {
    if (d[i] == INT_MAX)
      cout << "INF¥n";
    else
      cout << d[i] << endl;
  }
}
Problem p02361 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

template <typename T> struct Edge {
  T dst, cost;
  Edge(int dst, int cost) : dst(dst), cost(cost) {}
  bool operator>(const Edge<T> &rhs) const { return cost > rhs.cost; }
};

const int INF = 1 << 30;

template <typename T> vector<T> dijkstra(vector<vector<Edge<T>>> tree, T src) {
  size_t V = tree.size();
  vector<T> dist = vector<T>(V, INF);
  priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> queue;

  dist[src] = 0;
  queue.push(Edge<T>(src, 0));
  while (!queue.empty()) {
    Edge<T> t = queue.top();
    queue.pop();
    int v = t.dst, cost = t.cost;

    if (dist[v] < cost) {
      continue;
    }
    for (size_t i = 0; i < tree[v].size(); i++) {
      Edge<T> e = tree[v][i];
      if (dist[v] + e.cost < dist[e.dst]) {
        dist[e.dst] = dist[v] + e.cost;
        queue.push(Edge<T>(e.dst, dist[e.dst]));
      }
    }
  }

  return dist;
}

int main(int argc, char const *argv[]) {
  int V, E, r;
  cin >> V >> E >> r;

  vector<vector<Edge<int>>> tree(V);
  for (size_t i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    tree[s].push_back(Edge<int>(t, d));
  }

  vector<int> ret = dijkstra(tree, r);
  for (size_t i = 0; i < V; i++) {
    int d = ret[i];
    if (d >= INF) {
      cout << "INF" << endl;
    } else {
      cout << d << endl;
    }
  }

  return 0;
}


Error: -11

Fixed submission:

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

using namespace std;

template <typename T> struct Edge {
  T dst, cost;
  Edge(int dst, int cost) : dst(dst), cost(cost) {}
  bool operator>(const Edge<T> &rhs) const { return cost > rhs.cost; }
};

const int INF = 1 << 30;

template <typename T> vector<T> dijkstra(vector<vector<Edge<T>>> tree, T src) {
  size_t V = tree.size();
  vector<T> dist = vector<T>(V, INF);
  priority_queue<Edge<T>, vector<Edge<T>>, greater<Edge<T>>> queue;

  dist[src] = 0;
  queue.push(Edge<T>(src, 0));
  while (!queue.empty()) {
    Edge<T> t = queue.top();
    queue.pop();
    int v = t.dst, cost = t.cost;

    if (dist[v] < cost) {
      continue;
    }
    for (size_t i = 0; i < V; i++) {
      Edge<T> e = tree[v][i];
      if (dist[v] + e.cost < dist[e.dst]) {
        dist[e.dst] = dist[v] + e.cost;
        queue.push(Edge<T>(e.dst, dist[e.dst]));
      }
    }
  }

  return dist;
}

int main(int argc, char const *argv[]) {
  int V, E, r;
  cin >> V >> E >> r;

  vector<vector<Edge<int>>> tree =
      vector<vector<Edge<int>>>(V, vector<Edge<int>>(0, Edge<int>(0, 0)));

  for (size_t i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    tree[s].push_back(Edge<int>(t, d));
  }

  vector<int> ret = dijkstra(tree, r);
  for (size_t i = 0; i < V; i++) {
    int d = ret[i];
    if (d >= INF) {
      cout << "INF" << endl;
    } else {
      cout << d << endl;
    }
  }

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

#include <iostream>
#include <math.h>

using namespace std;

//= AOJ GRL_1_A

const int V_constraint = 100000, E_constraint = 500000, D_constraint = 10000;
int V, E, R, S[E_constraint + 10], T[E_constraint + 10], D[E_constraint + 10],
    C[V_constraint + 10];
const int inf = V_constraint * D_constraint + 100;
// int parent[100000];

int main() {

  //= prompt user for input
  cin >> V >> E >> R;
  // if (E>E_constraint)
  // 	cout<< "E>E_constraint";
  for (int i = 0; i < E; ++i) {
    cin >> S[i] >> T[i] >> D[i];
    // if (D[i] >D_constraint)
    // cout<< "D>D_constraint";
  }
  // set distance from source to source as zero, other as infinite
  for (int i = 0; i < V; i++)
    C[i] = inf;
  C[R] = 0;
  // parent[R]=R;

  // for (int i=0; i<E; ++i)
  // 	cout<<C[i];
  // cout<<endl;

  //= bellman-ford algo
  for (int v = 0; v < V - 1; v++) {
    bool update = false;

    for (int i = 0; i < E; ++i) {
      if (C[S[i]] + D[i] < C[T[i]] && C[S[i]] < inf) {
        C[T[i]] = C[S[i]] + D[i];
        update = true;
        // parent[T[i]]=S[i];
      }
    }
    if (!update)
      break;
  }

  //= output
  // cout<<V<<endl;

  for (int i = 0; i < V; i++)
    if (C[i] != inf)
      cout << C[i] << endl;
    else
      cout << "INF" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>

using namespace std;

//= AOJ GRL_1_A

const int V_constraint = 100000, E_constraint = 500000, D_constraint = 10000;
int V, E, R, S[E_constraint + 10], T[E_constraint + 10], D[E_constraint + 10],
    C[V_constraint + 10];
const int inf = V_constraint * D_constraint + 100;
// int parent[100000];

int main() {

  //= prompt user for input
  cin >> V >> E >> R;
  // if (E>E_constraint)
  // 	cout<< "E>E_constraint";
  for (int i = 0; i < E; ++i) {
    cin >> S[i] >> T[i] >> D[i];
    // if (D[i] >D_constraint)
    // cout<< "D>D_constraint";
  }
  // set distance from source to source as zero, other as infinite
  for (int i = 0; i < V; i++)
    C[i] = inf;
  C[R] = 0;
  // parent[R]=R;

  // for (int i=0; i<E; ++i)
  // 	cout<<C[i];
  // cout<<endl;

  //= bellman-ford algo
  for (int v = 0; v < V - 1; v++)
    for (int i = 0; i < E; ++i) {
      if (C[S[i]] + D[i] < C[T[i]] && C[S[i]] < inf) {
        C[T[i]] = C[S[i]] + D[i];
        // parent[T[i]]=S[i];
      }
    }

  //= output
  // cout<<V<<endl;

  for (int i = 0; i < V; i++)
    if (C[i] != inf)
      cout << C[i] << endl;
    else
      cout << "INF" << endl;

  return 0;
}