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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef list<int> LI;
typedef pair<int, int> PII;
typedef long long LL;

#define d(x) cout << #x << " = " << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EMP empty()
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REACH(i, c)                                                            \\
  for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c, n) sort(c, c + n)
#define VSORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define DFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define DREP(i, n) DFOR(i, n, 0)
#define INF 1000000000

const double EPS = 1e-10;
const double PI = acos(-1.0);
////////////////////////////////////////////////
static const int MAX = 100000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
int d[MAX];
vector<pair<int, int>> adj[MAX];

void dik(int s) {
  priority_queue<PII> PQ;
  int col[MAX];
  REP(i, n) {
    d[i] = INF;
    col[i] = WHITE;
  }

  d[s] = 0;
  PQ.push(MP(0, s));
  col[s] = GRAY;

  while (!PQ.empty()) {
    PII f = PQ.top();
    PQ.pop();
    int u = f.second;
    col[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    REP(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (col[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(MP(d[v] * (-1), v));
        col[v] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n;
  FOR(i, 1, n) {
    int s, t, w;
    cin >> s >> t >> w;
    adj[s].PB(MP(t, w));
    adj[t].PB(MP(s, w));
  }
  dik(0);
  int maxcost = -1;
  int u = 0;
  REP(i, n) {
    if (d[i] > maxcost && d[i] != INF) {
      maxcost = d[i];
      u = i;
    }
  }
  dik(u);
  maxcost = -1;
  REP(i, n) {
    if (d[i] > maxcost && d[i] != INF) {
      maxcost = d[i];
    }
  }
  cout << maxcost << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef list<int> LI;
typedef pair<int, int> PII;
typedef long long LL;

#define d(x) cout << #x << " = " << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define EMP empty()
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REACH(i, c)                                                            \\
  for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c, n) sort(c, c + n)
#define VSORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define DFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define DREP(i, n) DFOR(i, n, 0)
#define INF 1000000000

const double EPS = 1e-10;
const double PI = acos(-1.0);
////////////////////////////////////////////////
static const int MAX = 10000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
int d[MAX];
vector<pair<int, int>> adj[MAX];

void dik(int s) {
  priority_queue<PII> PQ;
  int col[MAX];
  REP(i, n) {
    d[i] = INF;
    col[i] = WHITE;
  }

  d[s] = 0;
  PQ.push(MP(0, s));
  col[s] = GRAY;

  while (!PQ.empty()) {
    PII f = PQ.top();
    PQ.pop();
    int u = f.second;
    col[u] = BLACK;

    if (d[u] < f.first * (-1))
      continue;

    REP(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (col[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(MP(d[v] * (-1), v));
        col[v] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n;
  FOR(i, 1, n) {
    int s, t, w;
    cin >> s >> t >> w;
    adj[s].PB(MP(t, w));
    adj[t].PB(MP(s, w));
  }
  dik(0);
  int maxcost = -1;
  int u = 0;
  REP(i, n) {
    if (d[i] > maxcost && d[i] != INF) {
      maxcost = d[i];
      u = i;
    }
  }
  dik(u);
  maxcost = -1;
  REP(i, n) {
    if (d[i] > maxcost && d[i] != INF) {
      maxcost = d[i];
    }
  }
  cout << maxcost << endl;
  return 0;
}