Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 2505

struct Edge {
  void set(int arg_from, int arg_to, ll arg_value) {
    from = arg_from;
    to = arg_to;
    value = arg_value;
  }
  int from, to;
  ll value;
};

ll N, M, P;
bool visited[SIZE];
ll min_dist[SIZE];
Edge edge[SIZE];
vector<int> rev_G[SIZE];

void dfs(int node_id) {

  for (int i = 0; i < rev_G[node_id].size(); i++) {

    int next = rev_G[node_id][i];
    if (visited[next])
      continue;

    visited[next] = true;

    dfs(next);
  }
}

int main() {

  scanf("%lld %lld %lld", &N, &M, &P);

  int from, to;
  ll value;

  for (int i = 0; i < M; i++) {

    scanf("%d %d %lld", &from, &to, &value);
    from--;
    to--;

    edge[i].set(from, to, P - value);
    rev_G[to].push_back(from);
  }

  for (int i = 0; i < N; i++) {

    visited[i] = false;
  }

  visited[N - 1] = true;
  dfs(N - 1);

  min_dist[0] = 0;
  for (int i = 1; i < N; i++) {

    min_dist[i] = HUGE_NUM;
  }

  bool FLG = true, updated;

  for (int i = 0; i < N; i++) {
    updated = false;
    for (int k = 0; k < M; k++) {
      Edge tmp = edge[k];
      if (!visited[tmp.from] || !visited[tmp.to])
        continue;

      if (min_dist[tmp.from] != HUGE_NUM &&
          min_dist[tmp.to] > min_dist[tmp.from] + tmp.value) {
        min_dist[tmp.to] = min_dist[tmp.from] + tmp.value;

        updated = true;
        if (i == N - 1) {
          FLG = false;
          break;
        }
      }
    }
    if (!updated)
      break;
    if (!FLG)
      break;
  }

  if (!FLG) {
    printf("-1\n");
    return 0;
  }

  printf("%lld\n", max(0LL, min_dist[N - 1] * (-1)));

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define SIZE 2505

struct Edge {
  void set(int arg_from, int arg_to, ll arg_value) {
    from = arg_from;
    to = arg_to;
    value = arg_value;
  }
  int from, to;
  ll value;
};

ll N, M, P;
bool visited[SIZE];
ll min_dist[SIZE];
Edge edge[2 * SIZE];
vector<int> rev_G[SIZE];

void dfs(int node_id) {

  for (int i = 0; i < rev_G[node_id].size(); i++) {

    int next = rev_G[node_id][i];
    if (visited[next])
      continue;

    visited[next] = true;

    dfs(next);
  }
}

int main() {

  scanf("%lld %lld %lld", &N, &M, &P);

  int from, to;
  ll value;

  for (int i = 0; i < M; i++) {

    scanf("%d %d %lld", &from, &to, &value);
    from--;
    to--;

    edge[i].set(from, to, P - value);
    rev_G[to].push_back(from);
  }

  for (int i = 0; i < N; i++) {

    visited[i] = false;
  }

  visited[N - 1] = true;
  dfs(N - 1);

  min_dist[0] = 0;
  for (int i = 1; i < N; i++) {

    min_dist[i] = HUGE_NUM;
  }

  bool FLG = true, updated;

  for (int i = 0; i < N; i++) {
    updated = false;
    for (int k = 0; k < M; k++) {
      Edge tmp = edge[k];
      if (!visited[tmp.from] || !visited[tmp.to])
        continue;

      if (min_dist[tmp.from] != HUGE_NUM &&
          min_dist[tmp.to] > min_dist[tmp.from] + tmp.value) {
        min_dist[tmp.to] = min_dist[tmp.from] + tmp.value;

        updated = true;
        if (i == N - 1) {
          FLG = false;
          break;
        }
      }
    }
    if (!updated)
      break;
    if (!FLG)
      break;
  }

  if (!FLG) {
    printf("-1\n");
    return 0;
  }

  printf("%lld\n", max(0LL, min_dist[N - 1] * (-1)));

  return 0;
}