Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
using namespace std;
int n, m;
vector<pii> vec[100005];
ll d[100005];
bool visit[100005];

void print() {
  for (int i = 0; i < n; i++) {
    cout << d[i] << ' ';
  }
}

void dfs(int x) {
  visit[x] = true;
  if (d[x] == -1)
    d[x] = 0;
  for (int i = 0; i < vec[x].size(); i++) {
    if (d[vec[x][i].first] == 1LL * inf * inf) {
      d[vec[x][i].first] = d[x] + vec[x][i].second;
      dfs(vec[x][i].first);
    } else {
      if (d[vec[x][i].first] != d[x] + vec[x][i].second)
        over("No");
    }
  }
}

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> m;
  int x, y, z;
  for (int i = 0; i < 100005; i++) {
    d[i] = 1LL * inf * inf;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &z);
    x--;
    y--;
    vec[x].push_back(mp(y, z));
    vec[y].push_back(mp(x, -z));
  }
  for (int i = 0; i < n; i++) {
    if (!visit[i])
      dfs(i);
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
using namespace std;
int n, m;
vector<pii> vec[100005];
ll d[100005];
bool visit[100005];

void print() {
  for (int i = 0; i < n; i++) {
    cout << d[i] << ' ';
  }
}

void dfs(int x) {
  visit[x] = true;
  if (d[x] == 1LL * inf * inf)
    d[x] = 0;
  for (int i = 0; i < vec[x].size(); i++) {
    if (d[vec[x][i].first] == 1LL * inf * inf) {
      d[vec[x][i].first] = d[x] + vec[x][i].second;
      dfs(vec[x][i].first);
    } else {
      if (d[vec[x][i].first] != d[x] + vec[x][i].second)
        over("No");
    }
  }
}

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> m;
  int x, y, z;
  for (int i = 0; i < 100005; i++) {
    d[i] = 1LL * inf * inf;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &z);
    x--;
    y--;
    vec[x].push_back(mp(y, z));
    vec[y].push_back(mp(x, -z));
  }
  for (int i = 0; i < n; i++) {
    if (!visit[i])
      dfs(i);
  }
  cout << "Yes" << endl;
  return 0;
}