Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<int, int>;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

const int INF = 1e9 + 7;

int n, m;
struct lendata {
  int next;
  int dist;
};

int main() {
  cin >> n >> m;
  vector<int> lvec(n, 0);
  vector<int> rvec(n, 0);
  vector<int> dvec(n, 0);
  vector<vector<lendata>> graph(n, vector<lendata>());
  vector<int> xvec(n, INF);
  for (int i = 0; i < m; ++i) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    lvec.at(i) = l;
    rvec.at(i) = r;
    dvec.at(i) = d;
    lendata nd;
    nd.next = r;
    nd.dist = d;
    graph.at(l).emplace_back(nd);
    nd.next = l;
    nd.dist = -d;
    graph.at(r).emplace_back(nd);
  }

  for (int i = 0; i < n; ++i) {
    if (xvec.at(i) == INF) {
      queue<lendata> que;
      lendata first;
      first.next = i;
      first.dist = 0;
      xvec.at(i) = 0;
      que.push(first);
      while (!que.empty()) {
        lendata now = que.front();
        que.pop();
        int glen = graph.at(now.next).size();
        for (int j = 0; j < glen; ++j) {
          int nx = graph.at(now.next).at(j).next;
          if (xvec.at(nx) == INF) {
            xvec.at(nx) = now.dist + graph.at(now.next).at(j).dist;
            lendata nxxx;
            nxxx.next = nx;
            nxxx.dist = xvec.at(nx);
            que.push(nxxx);
          }
        }
      }
    }
  }

  for (int i = 0; i < m; ++i) {
    if (xvec.at(rvec.at(i)) - xvec.at(lvec.at(i)) != dvec.at(i)) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<int, int>;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

const int INF = 1e9 + 7;

int n, m;
struct lendata {
  int next;
  int dist;
};

int main() {
  cin >> n >> m;
  vector<int> lvec(m, 0);
  vector<int> rvec(m, 0);
  vector<int> dvec(m, 0);
  vector<vector<lendata>> graph(n, vector<lendata>());
  vector<int> xvec(n, INF);
  for (int i = 0; i < m; ++i) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    lvec.at(i) = l;
    rvec.at(i) = r;
    dvec.at(i) = d;
    lendata nd;
    nd.next = r;
    nd.dist = d;
    graph.at(l).emplace_back(nd);
    nd.next = l;
    nd.dist = -d;
    graph.at(r).emplace_back(nd);
  }

  for (int i = 0; i < n; ++i) {
    if (xvec.at(i) == INF) {
      queue<lendata> que;
      lendata first;
      first.next = i;
      first.dist = 0;
      xvec.at(i) = 0;
      que.push(first);
      while (!que.empty()) {
        lendata now = que.front();
        que.pop();
        int glen = graph.at(now.next).size();
        for (int j = 0; j < glen; ++j) {
          int nx = graph.at(now.next).at(j).next;
          if (xvec.at(nx) == INF) {
            xvec.at(nx) = now.dist + graph.at(now.next).at(j).dist;
            lendata nxxx;
            nxxx.next = nx;
            nxxx.dist = xvec.at(nx);
            que.push(nxxx);
          }
        }
      }
    }
  }

  for (int i = 0; i < m; ++i) {
    if (xvec.at(rvec.at(i)) - xvec.at(lvec.at(i)) != dvec.at(i)) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}