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

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

const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mx = 100005;

ll dist[mx];
vector<pair<ll, int>> adj[mx];
bitset<mx> inq;
int v, e, r;
int q[mx], qh, qt, ct = 0;

int qpop() {
  int v = q[qh++];
  if (qh > mx)
    qh = 0;
  return v;
}

void qpush(int v) {
  ct++;
  q[qt++] = v;
  if (qt > mx)
    qt = 0;
}

int main() {
  qh = qt = 0;
  cin >> v >> e >> r;
  for (int i = 0; i < v; i++)
    dist[i] = inf;
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    adj[s].push_back({d, t});
  }
  dist[r] = 0;
  qpush(r);
  while (qh != qt) {
    if (ct > 2 * v * e + 5) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    int p = qpop();
    inq[p] = false;
    for (auto h : adj[p]) {
      int t = h.second;
      ll d = h.first + dist[p];
      if (d < dist[t]) {
        dist[t] = d;
        if (!inq[t])
          inq[t] = true, qpush(t);
      }
    }
  }
  for (int i = 0; i < v; i++) {
    if (dist[i] < inf / 2)
      cout << dist[i] << endl;
    else
      cout << "INF" << endl;
  }
}


Error: TLE

Fixed submission:

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

const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mx = 100005;

ll dist[mx];
vector<pair<ll, int>> adj[mx];
bitset<mx> inq;
int v, e, r;
int q[mx], qh, qt;

int qpop() {
  int v = q[qh++];
  if (qh > mx)
    qh = 0;
  return v;
}

void qpush(int v) {
  q[qt++] = v;
  if (qt > mx)
    qt = 0;
}

int main() {
  qh = qt = 0;
  cin >> v >> e >> r;
  for (int i = 0; i < v; i++)
    dist[i] = inf;
  for (int i = 0; i < e; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    adj[s].push_back({d, t});
  }
  dist[r] = 0;
  qpush(r);
  while (qh != qt) {
    int p = qpop();
    inq[p] = false;
    for (auto h : adj[p]) {
      int t = h.second;
      ll d = h.first + dist[p];
      if (d < dist[t]) {
        dist[t] = d;
        if (!inq[t])
          inq[t] = true, qpush(t);
      }
    }
  }
  for (int i = 0; i < v; i++) {
    if (dist[i] < inf / 2)
      cout << dist[i] << endl;
    else
      cout << "INF" << endl;
  }
}
Problem p02362 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef pair<long long, int> P;

#define MAX_N 150000
#define INF 100000000000000
priority_queue<P, vector<P>, greater<P>> Q;
vector<pair<int, long long>> X[MAX_N];
int N, M, A, B, C, O;
long long dist[MAX_N];
void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  Q.push(make_pair(0, O));
  dist[O] = 0;
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 8000000 || Q.size() >= 2000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return;
    }
    P pa = Q.top();
    Q.pop();
    long long a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
  return;
}
int main() {
  cin >> N >> M >> O;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  dijkstra();
  return 0;
}

Error: MLE

Fixed submission:

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

typedef pair<long long, int> P;

#define MAX_N 150000
#define INF 100000000000000
priority_queue<P, vector<P>, greater<P>> Q;
vector<pair<int, long long>> X[MAX_N];
int N, M, A, B, C, O;
long long dist[MAX_N];
void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  Q.push(make_pair(0, O));
  dist[O] = 0;
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 8000000 || Q.size() >= 5000000) {
      cout << "NEGATIVE CYCLE" << endl;
      return;
    }
    P pa = Q.top();
    Q.pop();
    long long a1 = pa.first;
    int a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      int to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist[i] == INF) {
      cout << "INF" << endl;
    } else {
      cout << dist[i] << endl;
    }
  }
  return;
}
int main() {
  cin >> N >> M >> O;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  dijkstra();
  return 0;
}
Problem p02362 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 1000
#define MAX_E 2000
#define INF 1e9
struct edge {
  int from, to, cost;
};
int V, E;
int d[MAX_V];
edge es[MAX_E];
bool find_negative_loop(int s) {
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  int count = 0;
  // bool update = false;
  while (true) {
    bool update = false;
    count++;
    for (int i = 0; i < E; i++) {
      if ((d[es[i].from] != INF) &&
          (d[es[i].to] > d[es[i].from] + es[i].cost)) {
        if (count == V) {
          return true;
        }
        d[es[i].to] = d[es[i].from] + es[i].cost;
        update = true;
      }
    }
    if (!update) {
      break;
    }
  }
  return false;
}

int main() {
  int r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> es[i].from >> es[i].to >> es[i].cost;
  }
  if (find_negative_loop(r)) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    for (int i = 0; i < V; i++) {
      if (d[i] == INF) {
        cout << "INF" << endl;
      } else {
        cout << d[i] << endl;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MAX_V 1000
#define MAX_E 2000
#define INF 1e9
struct edge {
  int from, to, cost;
};
int V, E;
int d[MAX_V];
edge es[MAX_E];
bool find_negative_loop(int s) {
  for (int i = 0; i < MAX_V; i++) {
    d[i] = INF;
  }
  d[s] = 0;
  int count = 0;
  // bool update = false;
  while (true) {
    bool update = true;
    count++;
    for (int i = 0; i < E; i++) {
      if ((d[es[i].from] != INF) &&
          (d[es[i].to] > d[es[i].from] + es[i].cost)) {
        if (count == V) {
          return true;
        }
        d[es[i].to] = d[es[i].from] + es[i].cost;
        update = true;
      }
    }
    if (!update) {
      break;
    }
  }
  return false;
}

int main() {
  int r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    cin >> es[i].from >> es[i].to >> es[i].cost;
  }
  if (find_negative_loop(r)) {
    cout << "NEGATIVE CYCLE" << endl;
  } else {
    for (int i = 0; i < V; i++) {
      if (d[i] == INF) {
        cout << "INF" << endl;
      } else {
        cout << d[i] << endl;
      }
    }
  }
  return 0;
}
Problem p02363 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll INF = 10000000000000000;
int n, m;
ll g[105][105];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        g[i][j] = 0;
      } else {
        g[i][j] = INF;
      }
    }
  }
  ll s, t, w;
  for (int i = 0; i < m; i++) {
    cin >> s >> t >> w;
    g[s][t] = min(g[s][t], w);
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (g[i][k] != INF && g[k][j] != INF)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  int flag = 0;
  for (int i = 0; i < n; i++) {
    if (g[i][i] < 0) {
      flag = 1;
    }
  }
  if (flag) {
    printf("NEGATIVE CYCLE\\n");
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (j == 0) {
          if (g[i][j] == INF) {
            printf("INF");
          } else {
            printf("%lld", g[i][j]);
          }
        } else {
          if (g[i][j] == INF) {
            printf(" INF");
          } else {
            printf(" %lld", g[i][j]);
          }
        }
      }
      printf("\\n");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const long long INF = 10000000000;
long long dis[105][105];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m)) {
    // memset(dis,INF,sizeof(INF));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j) {
          dis[i][j] = 0;
        } else
          dis[i][j] = INF;
      }
    }
    for (int i = 0; i < m; i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      dis[x][y] = z;
    }

    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        if (dis[i][k] == INF)
          continue;
        for (int j = 0; j < n; j++) {
          if (dis[k][j] == INF)
            continue;
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (dis[i][i] < 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 1)
      printf("NEGATIVE CYCLE\\n");
    else {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          if (j)
            printf(" ");
          if (dis[i][j] == 10000000000)
            printf("INF");
          else
            printf("%d", dis[i][j]);
        }
        printf("\\n");
      }
    }
  }
}
Problem p02363 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int MAX = 110;
const long long int INF = 1e10;
long long int d[MAX][MAX];
int V;
void warshall_floyd() {
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int main() {
  int E, s, t;
  long long int a, b = 0;
  cin >> V >> E;
  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 110; j++)
      d[i][j] = INF;
  for (int i = 0; i < 110; i++)
    d[i][i] = 0;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> a;
    d[s][t] = a;
  }
  warshall_floyd();
  for (int i = 0; i < V; i++) {
    if (d[i][i] != 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  for (int i = 0; i < V; i++) {

    for (int j = 0; j < V; j++) {
      if (d[i][j] >= 5 * 1e9) {
        if (j != 0)
          cout << " ";
        cout << "INF";
      } else {
        if (j != 0)
          cout << " ";
        cout << d[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

const int MAX = 10000;
const long long int INF = 100000000;
long long int d[MAX][MAX];
int V;
void warshall_floyd() {
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int main() {
  int E, s, t;
  long long int a;
  cin >> V >> E;
  for (int i = 0; i < 10000; i++)
    for (int j = 0; j < 10000; j++)
      d[i][j] = INF;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> a;
    d[s][t] = a;
  }
  warshall_floyd();
  for (int i = 0; i < V; i++) {
    if (d[i][i] != 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      if (d[i][j] >= INF) {
        cout << "INF"
             << " ";
      } else
        cout << d[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p02363 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000000000
struct edge {
  int from, to, cost;
};
int n, m, d[110];
edge es[9910];
bool NEGATIVE_CYCLE;
void bell(int s) {
  fill(d, d + n, INF);
  d[s] = 0;
  for (int i = 0; i <= n; i++) {
    bool update = false;
    for (edge e : es) {
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (i == n && update)
      NEGATIVE_CYCLE = 1;
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    es[i] = {a, b, c};
  }
  rep(i, n) {
    bell(i);
    if (NEGATIVE_CYCLE) {
      puts("NEGATIVE CYCLE");
      return 0;
    }
  }
  rep(i, n) {
    bell(i);
    rep(j, n) {
      if (d[j] == INF)
        cout << "INF";
      else
        cout << d[j];
      if (j != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define INF 1000000000000000
#define fi first
#define se second
#define all(v) v.begin(), v.end()
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
#define pb push_back
// struct edge{int to,cost;};
struct edge {
  int from, to, cost;
};
typedef pair<int, int> P;
int n, m, d[1010];
edge es[2010];
bool NEGATIVE_CYCLE;
void bell(int s) {
  fill(d, d + n, INF);
  d[s] = 0;
  for (int i = 0; i <= n; i++) {
    bool update = false;
    for (edge e : es) {
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (i == n && update)
      NEGATIVE_CYCLE = 1;
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    es[i] = {a, b, c};
  }
  rep(i, n) {
    bell(i);
    if (NEGATIVE_CYCLE) {
      puts("NEGATIVE CYCLE");
      return 0;
    }
  }
  rep(i, n) {
    rep(j, n) {
      bell(i);
      if (d[j] == INF)
        cout << "INF";
      else
        cout << d[j];
      if (j != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
  return 0;
}
Problem p02363 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<vector<lli>> g(n, vector<lli>(n, 1LL << 40));
  int from, to, cost;
  for (int i = 0; i < n; i++) {
    g[i][i] = 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> from >> to >> cost;
    g[from][to] = cost;
  }
  //???????????§?????????
  //???????????????????????£???????????????
  bool neg_cycle = false;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (g[i][j] == 1LL << 40 &&
            (g[i][k] == 1LL << 40 || g[k][j] == 1LL << 40))
          continue;
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (g[i][i] < 0)
      neg_cycle = true;
  }
  if (neg_cycle) {
    std::cout << "NEGATIVE CYCLE" << std::endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (g[i][j] >= 1LL << 40)
        std::cout << "INF ";
      else
        std::cout << g[i][j] << " ";
    }
    if (g[i][n - 1] >= 1LL << 40)
      std::cout << "INF" << std::endl;
    else
      std::cout << g[i][n - 1] << std::endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n, m, r;
  cin >> n >> m;
  vector<vector<int>> g(n, vector<int>(n, 1e9));
  int from, to, cost;
  for (int i = 0; i < n; i++) {
    g[i][i] = 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> from >> to >> cost;
    g[from][to] = cost;
  }
  //???????????§?????????
  //???????????????????????£???????????????
  int cnt[n][n];
  bool neg_cycle = false;
  memset(cnt, 0, sizeof(cnt));
  for (int k = 0; k < 2 * n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (g[i][j] > g[i][k] + g[k][j]) {
          g[i][j] = g[i][k] + g[k][j];
          cnt[i][j]++;
          if (cnt[i][j] >= n - 1)
            neg_cycle = true;
        }
      }
    }
  }
  if (neg_cycle) {
    std::cout << "NEGATIVE CYCLE" << std::endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      if (g[i][j] == 1e9)
        std::cout << "INF ";
      else
        std::cout << g[i][j] << " ";
    }
    if (g[i][n - 1] == 1e9)
      std::cout << "INF" << std::endl;
    else
      std::cout << g[i][n - 1] << std::endl;
  }
}
Problem p02363 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef pair<long long, long long> P;
#define MAX_N 1000
#define INF 1000000000000000LL

vector<P> X[MAX_N];
priority_queue<P, vector<P>, greater<P>> Q;
long long dist[MAX_N];
long long N, M, A, B, C;

int solve(int P) {
  while (!Q.empty()) {
    Q.pop();
  }
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  dist[P] = 0;
  Q.push(make_pair(0, P));
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 500000) {
      return 1;
    }
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      long long to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i >= 1) {
      cout << ' ';
    }
    if (dist[i] == INF) {
      cout << "INF";
    } else {
      cout << dist[i];
    }
  }
  cout << endl;
  return 0;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  for (int i = 0; i < N; i++) {
    int B = solve(i);
    if (B == 1) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef pair<long long, long long> P;
#define MAX_N 1000
#define INF 1000000000000000LL

vector<P> X[MAX_N];
priority_queue<P, vector<P>, greater<P>> Q;
long long dist[MAX_N];
long long N, M, A, B, C;

int solve(int P) {
  while (!Q.empty()) {
    Q.pop();
  }
  for (int i = 0; i < MAX_N; i++) {
    dist[i] = INF;
  }
  dist[P] = 0;
  Q.push(make_pair(0, P));
  int cnt = 0;
  while (!Q.empty()) {
    cnt++;
    if (cnt >= 1000000) {
      return 1;
    }
    pair<long long, long long> pa = Q.top();
    Q.pop();
    long long a1 = pa.first, a2 = pa.second;
    for (int i = 0; i < X[a2].size(); i++) {
      long long to = X[a2][i].first;
      if (dist[to] > a1 + X[a2][i].second) {
        dist[to] = a1 + X[a2][i].second;
        Q.push(make_pair(dist[to], to));
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (i >= 1) {
      cout << ' ';
    }
    if (dist[i] == INF) {
      cout << "INF";
    } else {
      cout << dist[i];
    }
  }
  cout << endl;
  return 0;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B >> C;
    X[A].push_back(make_pair(B, C));
  }
  for (int i = 0; i < N; i++) {
    int B = solve(i);
    if (B == 1) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02363 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define INF 2000000000
using namespace std;
int main() {
  int v, n;
  cin >> v >> n;
  long long a[v][v];
  r(i, v) r(j, v) a[i][j] = i == j ? 0 : INF;
  r(i, n) {
    int x, y, w;
    cin >> x >> y >> w;
    a[x][y] = w;
  }
  r(k, v) r(i, v) r(j, v) if (a[i][k] != INF && a[k][j] != INF) a[i][j] =
      min(a[i][j], a[i][k] + a[k][j]);
  r(i, v) if (a[i][i] < 0) {
    cout << "NEGATIVE CYCLE" << endl;
    return 0;
  }
  r(i, v) {
    r(j, v) {
      if (j)
        cout << ' ';
      if (a[i][j] >= INF)
        cout << "INF";
      else
        cout << a[i][j];
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 28
using namespace std;
int main() {
  int v, n;
  cin >> v >> n;
  long int a[v][v];
  r(i, n) r(j, n) a[i][j] = i == j ? 0 : INF;
  r(i, n) {
    int x, y, w;
    cin >> x >> y >> w;
    a[x][y] = w;
  }
  for (int k = 0; k < v; k++)
    for (int i = 0; i < v; i++)
      for (int j = 0; j < v; j++)
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  r(i, v) if (a[i][i] < 0) {
    cout << "NEGATIVE CYCLE" << endl;
    return 0;
  }
  r(i, v) {
    r(j, v) {
      if (j)
        cout << ' ';
      if (a[i][j] >= INF)
        cout << "INF";
      else
        cout << a[i][j];
    }
    cout << endl;
  }
}
Problem p02363 (C++) — buggy submission, verdict: Runtime Error

// knasiri
#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#define f first
#define s second
#define pb push_back
using namespace std;
pair<int, int> a[20000 + 10];
const long int INF = 9990099999;
long long int dis[100000];
long long int w[100000];
long long int n, m, r;
bool neg_cycle = false;
void belman_ford(int x) {
  for (int i = 0; i < n; i++) {
    dis[i] = INF;
  }
  dis[x] = 0;
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j < m; j++) {
      long long int u = a[j].f;
      long long int v = a[j].s;
      if (i >= (n - 1)) {
        if (((dis[u] + w[j]) < dis[v]) && dis[u] != INF)
          neg_cycle = true;
      } else {
        if (dis[u] != INF) {
          dis[v] = min((dis[u] + w[j]), dis[v]);
        }
      }

      // cout<<v<<" <------> "<<dis[v]<<endl;
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i].f >> a[i].s >> w[i];
  }
  for (int i = 0; i < n; i++) {
    belman_ford(i);
    if (neg_cycle) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
    for (int j = 0; j < n; j++) {
      if (dis[j] == INF) {
        cout << "INF";
      }
      if (dis[j] != INF) {
        cout << dis[j];
      }
      if (j != n - 1)
        cout << ' ';
    }
    cout << endl;
  }

  /// ddddddddd//dd//d//ddddd/dd//d
}

Error: 0

Fixed submission:

// ITNOA
#include <cstdio>
#include <iostream>
using namespace std;
#define F first
#define S second
typedef long long ll;
const ll INF = 1e17, MAX = 200, MOD = 1e9 + 7;
typedef pair<ll, ll> pii;
typedef pair<pii, ll> piii;
piii e[MAX];
ll d[MAX];
ll dp[MAX][MAX];
bool relax(int v) {
  if (d[e[v].F.F] + e[v].S < d[e[v].F.S] && d[e[v].F.F] != INF) {
    d[e[v].F.S] = d[e[v].F.F] + e[v].S;
    return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= n; i++)
    dp[i][i] = 0;
  for (int i = 0; i < m; i++) {
    cin >> e[i].F.F >> e[i].F.S >> e[i].S;
    e[i].F.F++;
    e[i].F.S++;
    dp[e[i].F.F][e[i].F.S] = e[i].S;
  }
  fill(d, d + n, INF);
  d[1] = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < m; j++)
      relax(j);
  }
  bool t = 0;
  for (int i = 0; !t && i < m; i++) {
    if (relax(i))
      t = 1;
  }
  if (t)
    return cout << "NEGATIVE CYCLE\\n", 0;
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dp[i][k] != INF && dp[k][j] != INF)
          dp[i][j] = min(dp[i][k] + dp[k][j], dp[i][j]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dp[i][j] == INF)
        cout << "INF";
      else
        cout << dp[i][j];
      if (j != n)
        cout << " ";
    }
    cout << "\\n";
  }
  return 0;
}