Problem p03837 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

const int INF = (int)1e8;
const int MAX_V = 101;

struct edge {
  int to;
  int cost;
  bool flag;
};

using P = pair<int, int>; // first: shortest distance, second: vertex number

int d[MAX_V];
int previous[MAX_V];
bool used[MAX_V][MAX_V];
vector<edge> G[MAX_V];

void dijkstra(int s, int N, int M) {
  // cout << "start s= " << s << endl;
  fill(d, d + MAX_V, INF);
  fill(previous, previous + MAX_V, -1);
  d[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) {
      // cout << "continue" << endl;
      continue;
    }
    for (auto e : G[v]) {
      // cout << "v, e.to, e.cost  " << v << " " << e.to << " " << e.cost <<
      // endl;
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        previous[e.to] = v;
        // cout << " making route now, previous= " << e.to << " " <<
        // previous[e.to] << endl;
        que.push(P(d[e.to], e.to));
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    if (i == s)
      continue;
    vector<int> route;
    for (int ii = i; ii != -1; ii = previous[ii]) {
      route.push_back(ii);
    }
    for (int ii = (int)route.size() - 1; ii > 0; ii--) {
      // cout << "now prev = " << route[ii] << " " << route[ii-1] << endl;
      used[route[ii]][route[ii - 1]] = true;
      used[route[ii - 1]][route[ii]] = true;
    }
  }

  for (int i = 1; i <= N; i++) {
    for (auto &e : G[i]) {
      if (used[i][e.to]) {
        e.flag = true;
        // cout << "i to flag " << i << " " << e.to << " " << e.flag << endl;
      }
    }
  }
}

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int a, b, c;
  int ans;

  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    edge e = {b, c, false};
    G[a].push_back(e);
    e = {a, c, false};
    G[b].push_back(e);
  }

  for (int i = 0; i < N; i++) {
    dijkstra(i + 1, N, M);
  }

  ans = 0;
  for (int i = 1; i <= M; i++) {
    for (auto e : G[i]) {
      // cout << "i to flag " << i << " " << e.to << " " << e.flag << endl;
      if (e.flag == false) {
        // cout << "a b =  " << i << " " << e.to << endl;
        ans++;
      }
    }
  }

  ans = ans / 2;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

const int INF = (int)1e8;
const int MAX_V = 101;

struct edge {
  int to;
  int cost;
  bool flag;
};

using P = pair<int, int>; // first: shortest distance, second: vertex number

int d[MAX_V];
int previous[MAX_V];
bool used[MAX_V][MAX_V];
vector<edge> G[MAX_V];

void dijkstra(int s, int N, int M) {
  // cout << "start s= " << s << endl;
  fill(d, d + MAX_V, INF);
  fill(previous, previous + MAX_V, -1);
  d[s] = 0;

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) {
      // cout << "continue" << endl;
      continue;
    }
    for (auto e : G[v]) {
      // cout << "v, e.to, e.cost  " << v << " " << e.to << " " << e.cost <<
      // endl;
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        previous[e.to] = v;
        // cout << " making route now, previous= " << e.to << " " <<
        // previous[e.to] << endl;
        que.push(P(d[e.to], e.to));
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    if (i == s)
      continue;
    vector<int> route;
    for (int ii = i; ii != -1; ii = previous[ii]) {
      route.push_back(ii);
    }
    for (int ii = (int)route.size() - 1; ii > 0; ii--) {
      // cout << "now prev = " << route[ii] << " " << route[ii-1] << endl;
      used[route[ii]][route[ii - 1]] = true;
      used[route[ii - 1]][route[ii]] = true;
    }
  }

  for (int i = 1; i <= N; i++) {
    for (auto &e : G[i]) {
      if (used[i][e.to]) {
        e.flag = true;
        // cout << "i to flag " << i << " " << e.to << " " << e.flag << endl;
      }
    }
  }
}

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int a, b, c;
  int ans;

  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    edge e = {b, c, false};
    G[a].push_back(e);
    e = {a, c, false};
    G[b].push_back(e);
  }

  for (int i = 0; i < N; i++) {
    dijkstra(i + 1, N, M);
  }

  ans = 0;
  for (int i = 1; i <= N; i++) {
    for (auto e : G[i]) {
      // cout << "i to flag " << i << " " << e.to << " " << e.flag << endl;
      if (e.flag == false) {
        // cout << "a b =  " << i << " " << e.to << endl;
        ans++;
      }
    }
  }

  ans = ans / 2;
  cout << ans << endl;

  return 0;
}