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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  int to, cost;
};

struct Data {
  Data(int arg_node_id, int arg_cost_sum) {
    node_id = arg_node_id;
    cost_sum = arg_cost_sum;
  }
  bool operator<(const struct Data &arg) const {
    return cost_sum > arg.cost_sum;
  };
  int node_id, cost_sum;
};

int main() {

  int N;
  scanf("%d", &N);

  if (N == 1) {
    printf("0\\n");
    return 0;
  }

  int *min_dist = new int[N];

  vector<Info> V[N];

  int from, to, cost;

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &from, &to, &cost);
    V[from].push_back(Info(to, cost));
    V[to].push_back(Info(from, cost));
  }

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

  priority_queue<Data> Q;

  for (int i = 0; i < V[0].size(); i++) {
    min_dist[V[0][i].to] = V[0][i].cost;
    Q.push(Data(V[0][i].to, V[0][i].cost));
  }

  int index;
  while (!Q.empty()) {
    if (Q.top().cost_sum > min_dist[Q.top().node_id]) {
      Q.pop();
    } else {
      index = Q.top().node_id;
      Q.pop();

      for (int i = 0; i < V[index].size(); i++) {
        if (min_dist[V[index][i].to] > min_dist[index] + V[index][i].cost) {
          min_dist[V[index][i].to] = min_dist[index] + V[index][i].cost;
          Q.push(Data(V[index][i].to, min_dist[V[index][i].to]));
        }
      }
    }
  }

  int maximum = -1, max_index;
  for (int i = 1; i < N; i++) {
    if (maximum < min_dist[i]) {
      maximum = min_dist[i];
      max_index = i;
    }
  }

  for (int i = 0; i < N; i++)
    min_dist[i] = BIG_NUM;
  min_dist[max_index] = 0;

  for (int i = 0; i < V[max_index].size(); i++) {
    min_dist[V[max_index][i].to] = V[max_index][i].cost;
    Q.push(Data(V[max_index][i].to, V[max_index][i].cost));
  }

  while (!Q.empty()) {
    if (Q.top().cost_sum > min_dist[Q.top().node_id]) {
      Q.pop();
    } else {
      index = Q.top().node_id;
      Q.pop();

      for (int i = 0; i < V[index].size(); i++) {
        if (min_dist[V[index][i].to] > min_dist[index] + V[index][i].cost) {
          min_dist[V[index][i].to] = min_dist[index] + V[index][i].cost;
          Q.push(Data(V[index][i].to, min_dist[V[index][i].to]));
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N; i++)
    ans = max(ans, min_dist[i]);

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  int to, cost;
};

struct Data {
  Data(int arg_node_id, int arg_cost_sum) {
    node_id = arg_node_id;
    cost_sum = arg_cost_sum;
  }
  bool operator<(const struct Data &arg) const {
    return cost_sum > arg.cost_sum;
  };
  int node_id, cost_sum;
};

int main() {

  int N;
  scanf("%d", &N);

  int *min_dist = new int[N];

  vector<Info> V[N];

  int from, to, cost;

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &from, &to, &cost);
    V[from].push_back(Info(to, cost));
    V[to].push_back(Info(from, cost));
  }

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

  priority_queue<Data> Q;

  for (int i = 0; i < V[0].size(); i++) {
    min_dist[V[0][i].to] = V[0][i].cost;
    Q.push(Data(V[0][i].to, V[0][i].cost));
  }

  int index;
  while (!Q.empty()) {
    if (Q.top().cost_sum > min_dist[Q.top().node_id]) {
      Q.pop();
    } else {
      index = Q.top().node_id;
      Q.pop();

      for (int i = 0; i < V[index].size(); i++) {
        if (min_dist[V[index][i].to] > min_dist[index] + V[index][i].cost) {
          min_dist[V[index][i].to] = min_dist[index] + V[index][i].cost;
          Q.push(Data(V[index][i].to, min_dist[V[index][i].to]));
        }
      }
    }
  }

  int maximum = -1, max_index;
  for (int i = 1; i < N; i++) {
    if (maximum < min_dist[i]) {
      maximum = min_dist[i];
      max_index = i;
    }
  }

  for (int i = 0; i < N; i++)
    min_dist[i] = BIG_NUM;
  min_dist[max_index] = 0;

  for (int i = 0; i < V[max_index].size(); i++) {
    min_dist[V[max_index][i].to] = V[max_index][i].cost;
    Q.push(Data(V[max_index][i].to, V[max_index][i].cost));
  }

  while (!Q.empty()) {
    if (Q.top().cost_sum > min_dist[Q.top().node_id]) {
      Q.pop();
    } else {
      index = Q.top().node_id;
      Q.pop();

      for (int i = 0; i < V[index].size(); i++) {
        if (min_dist[V[index][i].to] > min_dist[index] + V[index][i].cost) {
          min_dist[V[index][i].to] = min_dist[index] + V[index][i].cost;
          Q.push(Data(V[index][i].to, min_dist[V[index][i].to]));
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N; i++)
    ans = max(ans, min_dist[i]);

  printf("%d\\n", ans);

  return 0;
}