Problem p03837 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

template <typename T>
vector<int> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  const T INF = numeric_limits<T>::max();
  int n = G.size();
  using P = pair<T, int>;
  vector<T> ds(n, INF); // ds[i] is the least cost from s to i
  vector<int> pre(n, -1);
  priority_queue<P, vector<P>, greater<P>> pq; // the least element is top.
  ds[s] = 0;
  pq.emplace(ds[s], s);
  while (!pq.empty()) {
    P p = pq.top(); // the least element
    pq.pop();
    int v = p.second;
    if (ds[v] < p.first) {
      continue;
    }
    for (auto &e : G[v]) {
      int u = e.first;
      T cost = e.second;
      if (ds[u] > ds[v] + cost) {
        ds[u] = ds[v] + cost;
        pre[u] = v;
        pq.emplace(ds[u], u);
      }
    }
  }
  return pre;
  // if you want to know the least cost path from s, change ds to pre.
  // pre[i] is the pre node of the least cost path from s to i
}

int ansG[104][104];

int main() {
  int N, M;
  if (scanf("%d %d", &N, &M) == 1) {
    return 1;
  }

  using P = pair<int, long long>;
  vector<vector<P>> G(N); // adjacency list
  REP(i, N) {
    REP(j, N) { ansG[i][j] = -1; }
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    if (scanf("%d %d %d", &a, &b, &c) == 1) {
      return 1;
    }
    a--;
    b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    ansG[a][b] = 1;
    ansG[b][a] = 1;
  }
  REP(i, N) {
    auto pre = dijkstra(i, G);
    REP(j, N) {
      // printf("i:%d j:%d pre[j]:%d\n", i, j, pre[j]);
      ansG[pre[j]][j] = -1;
      ansG[j][pre[j]] = -1;
    }
  }

  int ans = 0;
  REP(i, N) {
    REP(j, N) {
      if (ansG[i][j] != -1) {
        ans++;
      }
    }
  }

  printf("%d\n", ans / 2);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

template <typename T>
vector<int> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  const T INF = numeric_limits<T>::max();
  int n = G.size();
  using P = pair<T, int>;
  vector<T> ds(n, INF); // ds[i] is the least cost from s to i
  vector<int> pre(n, -1);
  priority_queue<P, vector<P>, greater<P>> pq; // the least element is top.
  ds[s] = 0;
  pq.emplace(ds[s], s);
  while (!pq.empty()) {
    P p = pq.top(); // the least element
    pq.pop();
    int v = p.second;
    if (ds[v] < p.first) {
      continue;
    }
    for (auto &e : G[v]) {
      int u = e.first;
      T cost = e.second;
      if (ds[u] > ds[v] + cost) {
        ds[u] = ds[v] + cost;
        pre[u] = v;
        pq.emplace(ds[u], u);
      }
    }
  }
  return pre;
  // if you want to know the least cost path from s, change ds to pre.
  // pre[i] is the pre node of the least cost path from s to i
}

int ansG[104][104];

int main() {
  int N, M;
  if (scanf("%d %d", &N, &M) == 1) {
    return 1;
  }

  using P = pair<int, long long>;
  vector<vector<P>> G(N); // adjacency list
  REP(i, N) {
    REP(j, N) { ansG[i][j] = -1; }
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    if (scanf("%d %d %d", &a, &b, &c) == 1) {
      return 1;
    }
    a--;
    b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    ansG[a][b] = 1;
    ansG[b][a] = 1;
  }
  REP(i, N) {
    auto pre = dijkstra(i, G);
    REP(j, N) {
      // printf("i:%d j:%d pre[j]:%d\n", i, j, pre[j]);
      if (pre[j] != -1) {
        ansG[pre[j]][j] = -1;
        ansG[j][pre[j]] = -1;
      }
    }
  }

  int ans = 0;
  REP(i, N) {
    REP(j, N) {
      if (ansG[i][j] != -1) {
        ans++;
      }
    }
  }

  printf("%d\n", ans / 2);
  return 0;
}