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

#include <bits/stdc++.h>

using namespace std;

#define MAX 110
#define INF (1 << 29)
typedef pair<int, int> pii;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

int main() {
  int N, M, P;
  while (cin >> N >> M >> P, N) {
    int d[MAX][MAX];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        d[i][j] = INF;
      }
      d[i][i] = 0;
    }

    vector<Edge> G[MAX];

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

    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }

    priority_queue<pii> Q;
    Q.push(pii(d[0][N - 1], N - 1));

    vector<bool> visited(MAX, 0);
    visited[N - 1] = 1;

    vector<double> cnt(MAX, 0);
    cnt[N - 1] = 1;

    while (!Q.empty()) {
      pii p = Q.top();
      Q.pop();
      int v = p.second;
      for (int i = 0; i < (int)G[v].size(); i++) {
        Edge &e = G[v][i];
        if (d[0][e.to] + e.cost == d[0][v]) {
          cnt[e.to] += cnt[v];
          if (!visited[e.to]) {
            visited[e.to] = 1;
            Q.push(pii(d[0][e.to], e.to));
          }
        }
      }
    }

    priority_queue<pii, vector<pii>, greater<pii>> Q2;
    Q2.push(pii(0, 0));

    vector<bool> visited2(MAX, 0);
    visited2[0] = 1;

    double dp[MAX] = {};
    dp[0] = 1;

    while (!Q2.empty()) {
      pii p = Q2.top();
      Q2.pop();
      int v = p.second;
      for (int i = 0; i < (int)G[v].size(); i++) {
        Edge &e = G[v][i];
        if (visited[e.to] && d[0][v] + e.cost == d[0][e.to]) {
          dp[e.to] += dp[v] * cnt[e.to] / cnt[v];
          if (!visited2[e.to]) {
            visited2[e.to] = 1;
            Q2.push(pii(d[0][e.to], e.to));
          }
        }
      }
    }

    int child;
    for (int i = 0; i < P; i++) {
      cin >> child;
      printf("%.10f\\n", dp[child]);
    }
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 114
#define INF 1e9
typedef pair<int, int> pii;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

double dp[MAX];
bool visited[MAX];
vector<Edge> G[MAX];

void dfs(int v, vector<int> &d) {
  if (visited[v])
    return;
  visited[v] = 1;
  int cnt = 0;
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (d[v] - d[e.to] == e.cost) {
      cnt++;
    }
  }
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (d[v] - d[e.to] == e.cost) {
      dfs(e.to, d);
      dp[e.to] *= dp[v] * (1. / cnt);
    }
  }
}

int main() {
  int n, m, p, a, b, c;
  while (cin >> n >> m >> p, n) {
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      G[a].push_back(Edge(b, c));
      G[b].push_back(Edge(a, c));
    }
    priority_queue<pii, vector<pii>, greater<pii>> Q;
    Q.push(pii(0, 0));
    vector<int> d(n, INF);
    d[0] = 0;
    while (!Q.empty()) {
      pii p = Q.top();
      Q.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < (int)G[v].size(); i++) {
        Edge e = G[v][i];
        if (d[v] + e.cost < d[e.to]) {
          d[e.to] = d[v] + e.cost;
          Q.push(pii(d[e.to], e.to));
        }
      }
    }
    memset(visited, 0, sizeof(visited));
    fill(dp, dp + MAX, 1);
    dfs(n - 1, d);
    for (int i = 0; i < p; i++) {
      cin >> c;
      printf("%.8f\\n", dp[c]);
    }
    cout << endl;
  }
  return 0;
}