Problem p02361 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define int long long
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string getStr() {
  char ch[200000];
  scanf("%s", ch);
  return ch;
}
inline char getCh() {
  char ch;
  scanf(" %c", &ch);
  return ch;
}
template <class P, class Q> inline P smin(P &a, Q b) {
  if (b < a)
    a = b;
  return a;
}
template <class P, class Q> inline P smax(P &a, Q b) {
  if (a < b)
    a = b;
  return a;
}
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 10;
const int MA_LG = 21;
const int base = 29;
vector<pair<int, int>> g[MAX_N];
int res[MAX_N];
bool vis[MAX_N];
int mark[MAX_N];
inline bool dfs(int v, int cst = 0) {

  mark[v] = 1;

  for (int pt = 0; pt < g[v].size(); pt++) {
    int u = g[v][pt].second, w = g[v][pt].first;
    if (!mark[u]) {
      if (dfs(u, w + cst))
        return 1;
    } else if (mark[u] == 1 && w + cst < 0)
      return 1;
  }
  mark[v]++;
  return 0;
}
int32_t main() {
  int n = in(), m = in(), source = in();
  for (int i = 0; i < m; i++) {
    int v = in(), u = in(), w = in();
    g[v].push_back({u, w});
  }
  for (int v = 0; v < n; v++)
    res[v] = 1e18;
  res[source] = 0;
  if (dfs(0)) {
    cout << "NEGATIVE CYCLE\\n";
    exit(0);
  }
  priority_queue<pair<int, int>> pq;
  pq.push({0, source});
  res[source] = 0;
  while (!pq.empty()) {
    int v = pq.top().second;
    int di = -pq.top().first;
    pq.pop();
    if (vis[v] && res[v] <= di)
      continue;
    vis[v] = true;
    res[v] = di;
    for (int pt = 0; pt < g[v].size(); pt++) {
      int u = g[v][pt].first, w = g[v][pt].second;
      if (res[u] > w + di) {
        res[u] = w + di;
        pq.push({-w - di, u});
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (res[i] >= 1e18) {
      cout << "INF\\n";
    } else
      cout << res[i] << "\\n";
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define int long long
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string getStr() {
  char ch[200000];
  scanf("%s", ch);
  return ch;
}
inline char getCh() {
  char ch;
  scanf(" %c", &ch);
  return ch;
}
template <class P, class Q> inline P smin(P &a, Q b) {
  if (b < a)
    a = b;
  return a;
}
template <class P, class Q> inline P smax(P &a, Q b) {
  if (a < b)
    a = b;
  return a;
}
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 10;
const int MA_LG = 21;
const int base = 29;
vector<pair<int, int>> g[MAX_N];
int res[MAX_N];
bool vis[MAX_N];
int mark[MAX_N];
inline bool dfs(int v, int cst = 0) {

  mark[v] = 1;

  for (int pt = 0; pt < g[v].size(); pt++) {
    int u = g[v][pt].second, w = g[v][pt].first;
    if (!vis[u]) {
      if (dfs(u, w + cst))
        return 1;
    } else if (vis[u] == 1 && w + cst < 0)
      return 1;
  }
  mark[v]++;
  return 0;
}
int32_t main() {
  int n = in(), m = in(), source = in();
  for (int i = 0; i < m; i++) {
    int v = in(), u = in(), w = in();
    g[v].push_back({u, w});
  }
  for (int v = 0; v < n; v++)
    res[v] = 1e18;
  res[source] = 0;
  dfs(0);
  priority_queue<pair<int, int>> pq;
  pq.push({0, source});
  res[source] = 0;
  while (!pq.empty()) {
    int v = pq.top().second;
    int di = -pq.top().first;
    pq.pop();
    if (vis[v] && res[v] <= di)
      continue;
    vis[v] = true;
    res[v] = di;
    for (int pt = 0; pt < g[v].size(); pt++) {
      int u = g[v][pt].first, w = g[v][pt].second;
      if (res[u] > w + di) {
        if (vis[u] && w + di < 0) {
          cout << "NEGATIVE CYCLE\\n";
          exit(0);
        }
        //                res[u] = w + di;
        pq.push({-w - di, u});
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (res[i] >= 1e18) {
      cout << "INF\\n";
    } else
      cout << res[i] << "\\n";
  }
}