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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int N, M, C;

typedef long long ll;

struct Edge {
  int to;
  ll cost;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

Graph g;

double cntpath[110][110];

struct state {
  int curr;
  ll cost;
  bool operator<(state const &s) const { return cost > s.cost; }
};

struct ST {
  int curr;
  double prob;
};

ll const INF = 1LL << 40;

int main() {

  while (cin >> N >> M >> C && (N | M | C)) {
    g.clear();
    g.resize(N);

    rep(i, M) {
      int a, b;
      ll c;
      cin >> a >> b >> c;
      g[a].push_back((Edge){b, c});
      g[b].push_back((Edge){a, c});
    }

    ll dist[N][N];
    fill(dist[0], dist[0] + N * N, INF);

    rep(S, N) {
      dist[S][S] = 0;
      cntpath[S][S] = 1;
      priority_queue<state> pq;
      pq.push((state){S, dist[S][S]});
      while (!pq.empty()) {
        int curr = pq.top().curr;
        ll cost = pq.top().cost;
        pq.pop();

        rep(i, g[curr].size()) {
          Edge &e = g[curr][i];
          if (dist[S][e.to] == cost + e.cost) {
            cntpath[S][e.to] += cntpath[S][curr];
          }
          if (dist[S][e.to] <= cost + e.cost) {
            continue;
          }
          cntpath[S][e.to] = cntpath[S][curr];
          dist[S][e.to] = cost + e.cost;
          pq.push((state){e.to, dist[S][e.to]});
        }
      }
    }

    rep(i, C) {
      int q;
      cin >> q;
      if (dist[0][q] + dist[q][N - 1] == dist[0][N - 1]) {
        printf("%.10f\\n",
               cntpath[0][q] / cntpath[0][N - 1] * cntpath[q][N - 1]);
      } else {
        printf("%.10f\\n", 0.);
      }
    }
    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int N, M, C;

typedef long long ll;

struct Edge {
  int to;
  ll cost;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

Graph g;
vector<int> cs;

// vector<vector<int> > prev_;
int prev_[110][110];
int pcnt[110];
double ans[110];

struct state {
  int curr;
  ll cost;
  bool operator<(state const &s) const { return cost > s.cost; }
};

struct ST {
  int curr;
  double prob;
};

ll const INF = 1LL << 40;

int main() {

  while (cin >> N >> M >> C && (N | M | C)) {
    g.clear();
    g.resize(N);
    cs.clear();
    cs.resize(C);
    rep(i, M) {
      int a, b;
      ll c;
      cin >> a >> b >> c;
      g[a].push_back((Edge){b, c});
      g[b].push_back((Edge){a, c});
    }
    rep(i, C) cin >> cs[i];

    ll dist[N];
    fill(dist, dist + N, INF);
    dist[0] = 0;

    rep(i, N) pcnt[i] = 0;

    priority_queue<state> pq;
    pq.push((state){0, dist[0]});
    while (!pq.empty()) {
      int curr = pq.top().curr;
      ll cost = pq.top().cost;
      pq.pop();

      rep(i, g[curr].size()) {
        Edge &e = g[curr][i];
        if (dist[e.to] == cost + e.cost) {
          prev_[e.to][pcnt[e.to]++] = curr;
        }
        if (dist[e.to] <= cost + e.cost) {
          continue;
        }
        pcnt[e.to] = 0;
        prev_[e.to][pcnt[e.to]++] = curr;
        dist[e.to] = cost + e.cost;
        pq.push((state){e.to, dist[e.to]});
      }
    }

    rep(i, N) ans[i] = 0.;

    queue<ST> q;
    q.push((ST){N - 1, 1.0});
    while (!q.empty()) {
      int curr = q.front().curr;
      double prob = q.front().prob;
      q.pop();

      ans[curr] += prob;

      rep(i, pcnt[curr]) { q.push((ST){prev_[curr][i], prob / pcnt[curr]}); }
    }

    rep(i, C) { printf("%.10f\\n", ans[cs[i]]); }
    cout << endl;
  }

  return 0;
}