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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> edge;
typedef vector<edge> ve;
typedef vector<ve> graph;

int main() {
  int n, m, p;
  while (cin >> n >> m >> p, n) {
    graph g(n);
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      g[a].push_back(edge(c, b));
      g[b].push_back(edge(c, a));
    }

    vi c(p);
    for (int i = 0; i < p; i++)
      cin >> c[i];

    vi d(n, 1e9), order;
    d[0] = 0;
    priority_queue<edge, vector<edge>, greater<edge>> pq;
    pq.push(edge(0, 0));

    while (pq.size()) {
      int dis = pq.top().first, v = pq.top().second;
      pq.pop();
      if (d[v] < dis)
        continue;
      order.push_back(v);

      for (edge e : g[v]) {
        int nxtd = dis + e.first, u = e.second;
        if (d[u] > nxtd) {
          d[u] = nxtd;
          pq.push(edge(nxtd, u));
        }
      }
    }

    vector<long double> cnt(n, 0);
    cnt[n - 1] = 1;
    for (int i = order.size() - 1; i >= 0; i--) {
      int v = order[i];
      for (edge e : g[v]) {
        int dis = e.first, u = e.second;
        if (d[v] == d[u] + dis)
          cnt[u] += cnt[v];
      }
    }

    vector<long double> prob(n, 0);
    prob[0] = 1;
    for (int i = 0; i < n; i++) {
      int v = order[i];

      for (edge e : g[v]) {
        int nxtd = d[v] + e.first, u = e.second;
        if (d[u] == nxtd) {
          prob[u] += cnt[u] / cnt[v] * prob[v];
        }
      }
    }

    for (int i = 0; i < p; i++) {
      cout << fixed << setprecision(9) << prob[c[i]] << endl;
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> edge;
typedef vector<edge> ve;
typedef vector<ve> graph;

int main() {
  int n, m, p;
  while (cin >> n >> m >> p, n) {
    graph g(n);
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      g[a].push_back(edge(c, b));
      g[b].push_back(edge(c, a));
    }

    vector<int> c(p);
    for (int i = 0; i < p; i++)
      cin >> c[i];

    vector<int> d(n, 1e9);
    d[0] = 0;
    priority_queue<edge, vector<edge>, greater<edge>> pq;
    pq.push(edge(0, 0));

    while (pq.size()) {
      int dis = pq.top().first, v = pq.top().second;
      pq.pop();
      if (d[v] < dis)
        continue;

      for (edge e : g[v]) {
        int nxtd = dis + e.first, u = e.second;
        if (d[u] > nxtd) {
          d[u] = nxtd;
          pq.push(edge(nxtd, u));
        }
      }
    }

    graph dag(n);
    queue<int> q;
    q.push(n - 1);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (edge e : g[v]) {
        int dis = e.first, u = e.second;
        if (d[v] == d[u] + dis) {
          dag[u].push_back(edge(dis, v));
          q.push(u);
        }
      }
    }

    vector<int> deg(n, 0), order;
    for (int v = 0; v < n; v++) {
      for (edge e : dag[v])
        deg[e.second]++;
    }

    for (int v = 0; v < n; v++) {
      if (deg[v] == 0)
        q.push(v);
    }

    while (q.size()) {
      int v = q.front();
      q.pop();
      order.push_back(v);
      for (edge e : dag[v]) {
        int u = e.second;
        deg[u]--;
        if (deg[u] == 0)
          q.push(u);
      }
    }

    vector<long double> prob(n);
    prob[0] = 1;
    for (int i = 0; i < n; i++) {
      int v = order[i];

      for (edge e : dag[v]) {
        int u = e.second;
        prob[u] += prob[v] / dag[v].size();
      }
    }

    for (int i = 0; i < p; i++) {
      cout << fixed << setprecision(9) << prob[c[i]] << endl;
    }
    cout << endl;
  }
}