Problem p03837 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long u64;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int from[2 * m], to[2 * m], c[2 * m];
  bool visited[2 * m];
  for (int i = 0; i < n; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    from[2 * i] = a - 1;
    to[2 * i] = b - 1;
    c[2 * i] = d;
    from[2 * i + 1] = b - 1;
    to[2 * i + 1] = a - 1;
    c[2 * i + 1] = d;
    visited[2 * i] = false;
    visited[2 * i + 1] = false;
  }
  for (int i = 0; i < n; ++i) {
    int cost[n];
    int n_visited = 1;
    for (int j = 0; j < n; ++j) {
      cost[j] = -1;
    }
    cost[i] = 0;
    while (n_visited < n) {
      vector<int> path;
      vector<int> bnode;
      int best = 100001;
      for (int j = 0; j < 2 * m; ++j) {
        if (cost[from[j]] >= 0 && cost[to[j]] < 0) {
          if (best > cost[from[j]] + c[j]) {
            best = cost[from[j]] + c[j];
            path.clear();
            path.push_back(j);
            bnode.clear();
            bnode.push_back(to[j]);
          } else if (best == cost[from[j]] + c[j]) {
            path.push_back(j);
            bnode.push_back(to[j]);
          }
        }
      }
      for (int j = 0; j < path.size(); ++j) {
        if (cost[bnode[j]] == -1) {
          cost[bnode[j]] = best;
          n_visited++;
        }
        visited[path[j]] = true;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < m; ++i) {
    if (!visited[2 * i] && !visited[2 * i + 1]) {
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long u64;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int from[2 * m], to[2 * m], c[2 * m];
  bool visited[2 * m];
  for (int i = 0; i < m; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    from[2 * i] = a - 1;
    to[2 * i] = b - 1;
    c[2 * i] = d;
    from[2 * i + 1] = b - 1;
    to[2 * i + 1] = a - 1;
    c[2 * i + 1] = d;
    visited[2 * i] = false;
    visited[2 * i + 1] = false;
  }
  for (int i = 0; i < n; ++i) {
    int cost[n];
    int n_visited = 1;
    for (int j = 0; j < n; ++j) {
      cost[j] = -1;
    }
    cost[i] = 0;
    while (n_visited < n) {
      vector<int> path;
      vector<int> bnode;
      int best = 100001;
      for (int j = 0; j < 2 * m; ++j) {
        if (cost[from[j]] >= 0 && cost[to[j]] < 0) {
          if (best > cost[from[j]] + c[j]) {
            best = cost[from[j]] + c[j];
            path.clear();
            path.push_back(j);
            bnode.clear();
            bnode.push_back(to[j]);
          } else if (best == cost[from[j]] + c[j]) {
            path.push_back(j);
            bnode.push_back(to[j]);
          }
        }
      }
      for (int j = 0; j < path.size(); ++j) {
        if (cost[bnode[j]] == -1) {
          cost[bnode[j]] = best;
          n_visited++;
        }
        visited[path[j]] = true;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < m; ++i) {
    if (!visited[2 * i] && !visited[2 * i + 1]) {
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}