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

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

class Node {
public:
  vector<int> e_to;
  vector<int> e_cost;

  bool def;
  int min_cost;

  Node() {
    def = false;
    min_cost = -1;
  }
};
bool operator==(const Node &ot1, const Node &ot2) {
  return ot1.min_cost == ot2.min_cost;
}
bool operator!=(const Node &ot1, const Node &ot2) { return !(ot1 == ot2); }
bool operator<(const Node &ot1, const Node &ot2) {
  return ot1.min_cost < ot2.min_cost;
}
bool operator>(const Node &ot1, const Node &ot2) {
  return ot1.min_cost > ot2.min_cost;
}
bool operator<=(const Node &ot1, const Node &ot2) { return !(ot1 > ot2); }
bool operator>=(const Node &ot1, const Node &ot2) { return !(ot1 < ot2); }

int main() {
  int v, e, r, s, t, d;
  vector<Node> nodes;
  priority_queue<Node, vector<Node>, greater<Node>> que;

  cin >> v >> e >> r;
  nodes.resize(v);

  for (int i = 0; i < v; ++i) {
    nodes[i] = Node();
  }

  for (int i = 0; i < e; ++i) {
    cin >> s >> t >> d;
    nodes[s].e_to.push_back(t);
    nodes[s].e_cost.push_back(d);
  }

  nodes[r].min_cost = 0;

  que.push(nodes[r]);

  while (!que.empty()) {
    Node def_node = que.top();
    que.pop();

    if (def_node.def)
      continue;

    def_node.def = true;

    for (int i = 0; i < def_node.e_to.size(); ++i) {
      int to = def_node.e_to[i];
      int cost = def_node.min_cost + def_node.e_cost[i];

      if (nodes[to].min_cost == -1 || cost < nodes[to].min_cost) {
        nodes[to].min_cost = cost;
        que.push(nodes[to]);
      }
    }
  }

  for (int i = 0; i < v; ++i) {
    if (nodes[i].min_cost == -1)
      cout << "INF" << endl;
    else
      cout << nodes[i].min_cost << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int **matrix;
void func(int cur, int sum, int i, int *mark, int *min, int V) {
  mark[cur] = 1;

  if (matrix[cur][i] != -1) {
    if (*min == -1) {
      *min = sum + matrix[cur][i];
    } else if (sum + matrix[cur][i] <= *min) {
      *min = sum + matrix[cur][i];
    }
  }
  for (int j = 0; j < V; ++j) {
    if (j != i) {
      if (matrix[cur][j] != -1) {
        if (mark[j] == 0) {
          if (*min == -1 || sum + matrix[cur][j] <= *min)
            func(j, sum + matrix[cur][j], i, mark, min, V);
        }
      }
    }
  }
  mark[cur] = 0;
}

int main() {

  int V, E, r;

  cin >> V >> E >> r;
  matrix = new int *[V];

  for (int i = 0; i < V; i++) {
    matrix[i] = new int[V];
    for (int j = 0; j < V; ++j) {
      matrix[i][j] = -1;
    }
  }

  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;

    matrix[s][t] = d;
  }

  for (int i = 0; i < V; ++i) {
    int *mark = new int[V];
    int min = -1;

    if (i == r) {
      cout << 0 << endl;
      continue;
    }

    func(r, 0, i, mark, &min, V);
    if (min != -1)
      cout << min << endl;
    else
      cout << "INF" << endl;

    delete[] mark;
  }

  for (int i = 0; i < V; ++i) {
    delete[] matrix[i];
  }
  delete[] matrix;

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

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;

struct edge {
  int to, cost;
};

int n, m, s, cnt[maxn];
int d[maxn];
int qh, qt, q[maxn * maxn];
bool ok = 1, inq[maxn];
vector<edge> G[maxn];

int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].pb(edge{v, w});
  }
  memset(d, 0x3f, sizeof(d));
  d[s] = 0;
  q[qt++] = s;
  inq[s] = 1;
  while (qh < qt) {
    int u = q[qh++];
    inq[u] = 0;
    for (edge e : G[u]) {
      if (d[e.to] > d[u] + e.cost) {
        d[e.to] = d[u] + e.cost;
        if (!inq[e.to]) {
          q[qt++] = e.to;
          inq[e.to] = 1;
          ++cnt[e.to];
          if (cnt[u] > m + 5) {
            ok = 0;
            break;
          }
        }
      }
    }
    if (cnt[u] > m + 5) {
      ok = 0;
      break;
    }
  }
  if (!ok) {
    cout << "NEGATIVE CYCLE" << '\\n';
    return 0;
  } else
    for (int i = 0; i < n; i++)
      if (d[i] == INF)
        cout << "INF" << '\\n';
      else
        cout << d[i] << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1005;

struct edge {
  int to, cost;
};

int n, m, s, cnt[maxn];
int d[maxn];
int qh, qt, q[maxn * maxn];
bool ok = 1, inq[maxn];
vector<edge> G[maxn];

int main() {
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].pb(edge{v, w});
  }
  memset(d, 0x3f, sizeof(d));
  d[s] = 0;
  q[qt++] = s;
  inq[s] = 1;
  while (qh < qt) {
    int u = q[qh++];
    inq[u] = 0;
    for (edge e : G[u]) {
      if (d[e.to] > d[u] + e.cost) {
        d[e.to] = d[u] + e.cost;
        if (!inq[e.to]) {
          q[qt++] = e.to;
          inq[e.to] = 1;
          ++cnt[e.to];
          if (cnt[u] > m + 5) {
            ok = 0;
            break;
          }
        }
      }
    }
  }
  if (!ok) {
    cout << "NEGATIVE CYCLE" << '\\n';
    return 0;
  } else
    for (int i = 0; i < n; i++)
      if (d[i] == INF)
        cout << "INF" << '\\n';
      else
        cout << d[i] << '\\n';
}
Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct Node {
  int to, from, cost;
};

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> dist(n, inf);
  vector<Node> node(m);
  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    node[i].to = t;
    node[i].from = s;
    node[i].cost = d;
  }

  dist[r] = 0;
  int l = 0;
  while (true) {
    if (l > 100000) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    bool f = false;
    for (int i = 0; i < node.size(); i++) {
      Node d = node[i];
      if (dist[d.from] != inf && dist[d.to] > dist[d.from] + d.cost) {
        dist[d.to] = dist[d.from] + d.cost;
        f = true;
      }
    }
    if (!f)
      break;
    l++;
  }

  for (int i = 0; i < n; i++) {
    if (dist[i] == inf)
      cout << "INF" << endl;
    else
      cout << dist[i] << endl;
  }

  return 0;
}

// EOF

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int inf = 1000000000;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct Node {
  int to, from, cost;
};

int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<int> dist(n, inf);
  vector<Node> node(m);
  for (int i = 0; i < m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    node[i].to = t;
    node[i].from = s;
    node[i].cost = d;
  }

  dist[r] = 0;
  int l = 0;
  while (true) {
    if (l > 1000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    bool f = false;
    for (int i = 0; i < node.size(); i++) {
      Node d = node[i];
      if (dist[d.from] != inf && dist[d.to] > dist[d.from] + d.cost) {
        dist[d.to] = dist[d.from] + d.cost;
        f = true;
      }
    }
    if (!f)
      break;
    l++;
  }

  for (int i = 0; i < n; i++) {
    if (dist[i] == inf)
      cout << "INF" << endl;
    else
      cout << dist[i] << endl;
  }

  return 0;
}

// EOF
Problem p02362 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1e9
#define MAX_E 500000
#define MAX_V 100000

struct edge {
  int u, v, cost;
};
edge es[MAX_E];

int dis[MAX_V];
int V, E, r;

void bellman_ford(int s) {
  for (int i = 0; i < V; i++)
    dis[i] = INF;
  dis[s] = 0;
  int count = 0;
  while (true) {
    if (++count == V && count > 1) {
      cout << "NEGATIVE CYCLE" << endl;
      exit(0);
    }
    bool update = false;
    for (int i = 0; i < E; i++) {
      edge e = es[i];
      if (dis[e.u] != INF && dis[e.v] > dis[e.u] + e.cost) {
        dis[e.v] = dis[e.u] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main(void) {
  int s, t, d;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> d;
    edge e;
    e.u = s;
    e.v = t;
    e.cost = d;
    es[i] = e;
  }
  bellman_ford(r);
  for (int i = 0; i < V; i++) {
    if (dis[i] == INF)
      cout << "INF" << endl;
    else
      cout << dis[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1e9
#define MAX_E 500000
#define MAX_V 100000

struct edge {
  int u, v, cost;
};
edge es[MAX_E];

int dis[MAX_V];
int V, E, r;

void bellman_ford(int s) {
  for (int i = 0; i < V; i++)
    dis[i] = INF;
  dis[s] = 0;
  int count = 0;
  while (true) {
    if (++count == V) {
      cout << "NEGATIVE CYCLE" << endl;
      exit(1);
    }
    bool update = false;
    for (int i = 0; i < E; i++) {
      edge e = es[i];
      if (dis[e.u] != INF && dis[e.v] > dis[e.u] + e.cost) {
        dis[e.v] = dis[e.u] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main(void) {
  int s, t, d;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> d;
    edge e;
    e.u = s;
    e.v = t;
    e.cost = d;
    es[i] = e;
  }
  bellman_ford(r);
  for (int i = 0; i < V; i++) {
    if (dis[i] == INF)
      cout << "INF" << endl;
    else
      cout << dis[i] << endl;
  }
  return 0;
}
Problem p02362 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

#define INF (1 << 30)

struct Node {
  int id;
  int cost;
  vector<int> next_node;
  vector<int> next_cost;
} node[1000];

int main() {
  int v, e, r, s, t, d;

  cin >> v >> e >> r;
  for (int i = 0; i < v; i++) {
    node[i].id = i;
    node[i].cost = INF;
  }
  for (int i = 0; i < e; i++) {
    cin >> s >> t >> d;
    node[s].next_node.push_back(t);
    node[s].next_cost.push_back(d);
  }

  int count = 0;
  bool update, next[v] = {false};
  next[r] = true;
  node[r].cost = 0;
  while (1) {
    if (count >= v) {
      cout << "NEGATIVE CYCLE\\n";
      return 0;
    }
    update = false;
    for (int i = 0; i < v; i++) {
      if (next[i] == false) {
        continue;
      }
      int size = node[i].next_node.size();
      for (int j = 0; j < size; j++) {
        int to = node[i].next_node[j];
        next[to] = true;
        if (node[to].cost > node[i].cost + node[i].next_cost[j]) {
          node[to].cost = node[i].cost + node[i].next_cost[j];
          update = true;
        }
      }
    }
    if (update == false) {
      break;
    }
    count++;
  }
  for (int i = 0; i < v; i++) {
    if (next[i] == false) {
      cout << "INF\\n";
    } else {
      cout << node[i].cost << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define INF (1 << 30)

struct Node {
  int id;
  vector<int> next_node;
  vector<int> next_cost;
  bool done;
  int cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[100000];

int main() {
  int v, e, r, s, t, d;

  cin >> v >> e >> r;
  for (int i = 0; i < v; i++) {
    node[i].id = i;
    node[i].done = false;
    node[i].cost = INF;
  }
  for (int i = 0; i < e; i++) {
    cin >> s >> t >> d;
    node[s].next_node.push_back(t);
    node[s].next_cost.push_back(d);
  }

  Node temp;
  int size, to, cost;
  priority_queue<Node> que;
  node[r].cost = 0;
  que.push(node[r]);
  while (!que.empty()) {
    temp = que.top();
    que.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    size = temp.next_node.size();
    for (int i = 0; i < size; i++) {
      to = temp.next_node[i];
      cost = temp.cost + temp.next_cost[i];
      if (cost < node[to].cost) {
        if (node[to].done == true) {
          cout << "NEGATIVE CYCLE\\n";
          return 1;
        }
        node[to].cost = cost;
        que.push(node[to]);
      }
    }
  }

  for (int i = 0; i < v; i++) {
    if (node[i].done == false) {
      cout << "INF\\n";
    } else {
      cout << node[i].cost << endl;
    }
  }

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

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

signed main() {
  using pii = pair<int, int>;
  using piii = pair<pii, int>;
  int v, e, s;
  cin >> v >> e >> s;
  vector<int> d(v, LLONG_MAX);
  vector<piii> edge;
  rep(i, e) {
    int from, to, cost;
    cin >> from >> to >> cost;
    edge.push_back(piii(pii(from, to), cost));
  }
  d[s] = 0;
  rep(i, v) {
    bool update = false;
    rep(j, edge.size()) {
      int to = edge[j].first.second;
      int from = edge[j].first.first;
      int cost = edge[j].second;
      if (d[from] == LLONG_MAX)
        continue;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        update = true;
      }
    }
    if (i == v - 1 && update)
      return puts("NEGATIVE CYCLE") * 0;
  }
  rep(i, v) if (d[i] != LLONG_MAX) cout << d[i] << endl;
  else cout << "INF" << endl;
}

Error: 0

Fixed submission:

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

signed main() {
  using pii = pair<int, int>;
  using piii = pair<pii, int>;
  int v, e, s;
  cin >> v >> e >> s;
  vector<int> d(v, LLONG_MAX);
  vector<piii> edge;
  rep(i, e) {
    int from, to, cost;
    cin >> from >> to >> cost;
    edge.push_back(piii(pii(from, to), cost));
  }
  d[s] = 0;
  rep(i, v) {
    bool update = false;
    rep(j, edge.size()) {
      int to = edge[j].first.second;
      int from = edge[j].first.first;
      int cost = edge[j].second;
      if (d[from] == LLONG_MAX)
        continue;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        update = true;
      }
    }
    if (i == v - 1 && update)
      return puts("NEGATIVE CYCLE");
  }
  rep(i, v) if (d[i] != LLONG_MAX) cout << d[i] << endl;
  else cout << "INF" << endl;
}
Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip> //小数精度 	cout << fixed << setprecision(5) << tmp << endl;
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define All(X) (X).begin(), (X).end()
const ll int INF = 1000000001;
int v, e, r;

struct edge {
  ll int from, to, cost;
};

vector<edge> es(e);
vector<ll int> d(v);

bool SearchMinCost(int s) {
  REP(i, v) d[i] = INF;
  d[s] = 0;
  int cnt = 0;
  while (true) {
    cnt++;
    bool update = false;
    REP(i, e) {
      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;
    if (cnt == v)
      return true;
  }
  return false;
}
int main() {
  cin >> v >> e >> r;
  es.resize(e);
  REP(i, e) { cin >> es[i].from >> es[i].to >> es[i].cost; }
  d.resize(v);

  int flag = 1;

  if (SearchMinCost(r) == true)
    flag = 0;
  if (flag == 0) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    SearchMinCost(r);
    REP(i, v) {
      if (d[i] >= INF)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip> //小数精度 	cout << fixed << setprecision(5) << tmp << endl;
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define All(X) (X).begin(), (X).end()
const ll int INF = 1000000001;
int v, e, r;

struct edge {
  ll int from, to, cost;
};

vector<edge> es(e);
vector<ll int> d(v);

bool find_negative_loop() {
  // memset(d,0,sizeof(d));
  fill(All(d), 0);
  REP(i, v) {
    REP(j, e) {
      edge e = es[j];
      if (d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        if (i == v - 1)
          return true;
      }
    }
  }
  return false;
}

void SearchMinCost(int s) {
  REP(i, v) d[i] = INF;
  d[s] = 0;
  while (true) {
    bool update = false;
    REP(i, e) {
      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() {
  cin >> v >> e >> r;
  es.resize(e);
  REP(i, e) { cin >> es[i].from >> es[i].to >> es[i].cost; }
  d.resize(v);
  SearchMinCost(r);

  if (find_negative_loop()) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    SearchMinCost(r);
    REP(i, v) {
      if (d[i] >= INF)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
    }
  }
  return 0;
}
Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
const int INF = (1 << 30);
int V, E, r;
int dp[100000];
struct edge {
  int to, cost;
  edge(int to, int cost = 1) : to(to), cost(cost) {}
};
vector<edge> G[100000];

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  cin >> V >> E >> r;
  int s, t, d, update = 1, limit = V * E + 1;
  for (int i = 0; i < E; ++i) {
    cin >> s >> t >> d;
    edge e(t, d);
    G[s].emplace_back(e);
  }
  fill(dp, dp + V, INF);
  dp[r] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, r));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (dp[p.second] < p.first)
      continue;
    int v = p.second;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (dp[e.to] > dp[v] + e.cost) {
        dp[e.to] = dp[v] + e.cost;
        ++update;
        que.push(P(dp[e.to], e.to));
      }
    }
    if (update > limit) {
      cout << "NEGATIVE CYCLE\\n";
      return 0;
    }
  }
  for (int i = 0; i < V; ++i) {
    if (dp[i] == INF)
      cout << "INF\\n";
    else
      cout << dp[i] << "\\n";
  }
  cout << flush;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> P;
const int INF = (1 << 30);
int V, E, r;
int dp[100000];
struct edge {
  int to, cost;
  edge(int to, int cost = 1) : to(to), cost(cost) {}
};
vector<edge> G[100000];

int main() {
  cin.tie(0);
  ios::ios_base::sync_with_stdio(false);
  cin >> V >> E >> r;
  int s, t, d;
  for (int i = 0; i < E; ++i) {
    cin >> s >> t >> d;
    edge e(t, d);
    G[s].emplace_back(e);
  }
  fill(dp, dp + V, INF);
  dp[r] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, r));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (dp[p.second] < p.first)
      continue;
    int v = p.second;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (dp[e.to] > dp[v] + e.cost) {
        dp[e.to] = dp[v] + e.cost;
        que.push(P(dp[e.to], e.to));
      }
    }
  }
  if (dp[r] < 0)
    cout << "NEGATIVE CYCLE\\n";
  else
    for (int i = 0; i < V; ++i) {
      if (dp[i] == INF)
        cout << "INF\\n";
      else
        cout << dp[i] << "\\n";
    }
  cout << flush;
  return 0;
}
Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <vector>

#define INF 999999999
#define ll long long

using namespace std;

const int MAX_N = 1000;
const int MAX_M = 2000;

struct edge {
  int from; //?????????
  int to;   //??°??????
  int cost; //??????
};

int d[MAX_N];
edge es[MAX_M];
bool flag; //???????????????????????????

void shortest_path(
    int n, int m,
    int s) // s???????????????????????????????????????????????¢????±???????
{
  int cnt = 0;
  fill(d, d + n, INF);
  d[s] = 0;
  while (true && cnt < n) {
    bool update = false;
    for (int i = 0; i < m; 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;
    cnt++;
  }
  if (cnt == n) {
    flag = true;
  }
}

int main() {
  int i, n, m, s;
  cin >> n; //????????°
  cin >> m; //?????°
  cin >> s; //???????????????
  for (int i = 0; i < m; i++) {
    scanf("%d", &es[i].from);
    scanf("%d", &es[i].to);
    scanf("%d", &es[i].cost);
  }
  flag = false;
  shortest_path(n, m, s);
  if (!flag) {
    for (int i = 0; i < n; i++) {
      if (d[i] == INF) {
        cout << "INF\\n";
      } else {
        cout << d[i] << "\\n";
      }
    }
  } else {
    cout << "NEGATIVE CYCLE\\n";
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>

#define INF 999999999
#define ll long long

using namespace std;

const int MAX_N = 1000;
const int MAX_M = 2000;

struct edge {
  int from; //?????????
  int to;   //??°??????
  int cost; //??????
};

int d[MAX_N];
edge es[MAX_M];
bool flag; //???????????????????????????

void shortest_path(
    int n, int m,
    int s) // s???????????????????????????????????????????????¢????±???????
{
  int cnt = 0;
  fill(d, d + n, INF);
  d[s] = 0;
  while (true && cnt < n) {
    bool update = false;
    for (int i = 0; i < m; 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;
  }
  if (cnt == n) {
    flag = true;
  }
}

int main() {
  int i, n, m, s;
  cin >> n; //????????°
  cin >> m; //?????°
  cin >> s; //???????????????
  for (int i = 0; i < m; i++) {
    scanf("%d", &es[i].from);
    scanf("%d", &es[i].to);
    scanf("%d", &es[i].cost);
  }
  flag = false;
  shortest_path(n, m, s);
  if (!flag) {
    for (int i = 0; i < n; i++) {
      if (d[i] == INF) {
        cout << "INF\\n";
      } else {
        cout << d[i] << "\\n";
      }
    }
  } else {
    cout << "NEGATIVE CYCLE\\n";
  }
}
Problem p02362 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

const int MAXN = 2005, INF = 1e9;
int d[MAXN], x[MAXN], y[MAXN], w[MAXN];
int n, m, r;

bool bellman_ford(int r) {
  fill(d, d + n, INF);
  d[r] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[y[j]] = min(d[y[j]], d[x[j]] + w[j]);
  for (int j = 0; j < m; j++)
    if (d[y[j]] > d[x[j]] + w[j] && d[y[j]] < INF / 10)
      return true;
  return false;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++)
    cin >> x[i] >> y[i] >> w[i];
  if (bellman_ford(r))
    cout << "NEGATIVE CYCLE" << endl;
  else
    for (int i = 0; i < n; i++)
      if (d[i] > INF / 10)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

const int MAXN = 1005, INF = 1e9;
int d[MAXN], x[MAXN], y[MAXN], w[MAXN];
int n, m, r;

bool bellman_ford(int r) {
  fill(d, d + n, INF);
  d[r] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      d[y[j]] = min(d[y[j]], d[x[j]] + w[j]);
  for (int j = 0; j < m; j++)
    if (d[y[j]] > d[x[j]] + w[j] && d[y[j]] < INF / 10)
      return true;
  return false;
}

int main() {
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++)
    cin >> x[i] >> y[i] >> w[i];
  if (bellman_ford(r))
    cout << "NEGATIVE CYCLE" << endl;
  else
    for (int i = 0; i < n; i++)
      if (d[i] > INF / 10)
        cout << "INF" << endl;
      else
        cout << d[i] << endl;
  return 0;
}