Problem p00644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair

int n, m, p, C[100];
vector<vector<pair<int, int>>> e;
int d[100];
bool v[100], u[100];
double togoal[100], prob[100];

int main() {
  while (cin >> n >> m >> p, n) {
    e.clear();
    e.resize(n);
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      e[a].pb(mp(b, c));
      e[b].pb(mp(a, c));
    }
    rep(i, p) cin >> C[i];

    rep(i, n) d[i] = 1 << 28;
    d[0] = 0;
    priority_queue<pair<int, int>> Q;
    Q.push(mp(0, 0));
    while (!Q.empty()) {
      int c = Q.top().second, cc = Q.top().first;
      Q.pop();
      if (c == n - 1)
        break;
      if (d[c] < -cc)
        continue;
      fr(i, e[c]) if (d[i->first] > -cc + i->second)
          d[i->first] = -cc + i->second,
          Q.push(mp(cc - i->second, i->first));
    }

    rep(i, n) u[i] = v[i] = 0;
    rep(i, n) prob[i] = 0, togoal[i] = 0;
    togoal[n - 1] = 1;
    v[n - 1] = 1;
    Q = priority_queue<pair<int, int>>();
    Q.push(mp(d[n - 1], n - 1));
    while (!Q.empty()) {
      int c = Q.top().second;
      Q.pop();
      fr(i, e[c]) if (d[i->first] + i->second == d[c]) {
        togoal[i->first] += togoal[c];
        if (!v[i->first])
          v[i->first] = 1, Q.push(mp(d[i->first], i->first));
      }
    }
    Q = priority_queue<pair<int, int>>();
    Q.push(mp(0, 0));
    prob[0] = 1;
    u[0] = 1;
    while (!Q.empty()) {
      int c = Q.top().second;
      Q.pop();
      fr(i, e[c]) if (v[i->first] && d[i->first] == d[c] + i->second) {
        prob[i->first] += prob[c] * togoal[i->first] / togoal[c];
        if (!u[i->first])
          u[i->first] = 1, Q.push(mp(-d[i->first], i->first));
      }
    }
    rep(i, p) printf("%.7f\\n", prob[C[i]]);
    puts("");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair

int n, m, p, C[100];
vector<vector<pair<int, int>>> e;
int d[100];
bool v[100];
double pb[100];

int main() {
  while (cin >> n >> m >> p, n) {
    e.clear();
    e.resize(n);
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      e[a].pb(mp(b, c));
      e[b].pb(mp(a, c));
    }
    rep(i, p) cin >> C[i];

    rep(i, n) d[i] = 1 << 28;
    d[0] = 0;
    priority_queue<pair<int, int>> Q;
    Q.push(mp(0, 0));
    while (!Q.empty()) {
      int c = Q.top().second, cc = Q.top().first;
      Q.pop();
      if (c == n - 1)
        break;
      if (d[c] < -cc)
        continue;
      fr(i, e[c]) if (d[i->first] > -cc + i->second)
          d[i->first] = -cc + i->second,
          Q.push(mp(cc - i->second, i->first));
    }

    rep(i, n) v[i] = 0, pb[i] = 0;
    queue<int> q;
    q.push(n - 1);
    while (!q.empty()) {
      int c = q.front();
      q.pop();
      v[c] = 1;
      fr(i, e[c]) if (!v[i->first] && d[i->first] + i->second == d[c])
          q.push(i->first);
    }
    q = queue<int>();
    q.push(0);
    pb[0] = 1;
    while (!q.empty()) {
      int c = q.front(), cnt = 0;
      q.pop();
      fr(i, e[c]) if (v[i->first] && d[i->first] == d[c] + i->second) cnt++;
      fr(i, e[c]) if (v[i->first] && d[i->first] == d[c] + i->second)
          q.push(i->first),
          pb[i->first] += pb[c] / cnt;
    }

    rep(i, p) printf("%.7f\\n", pb[C[i]]);
    puts("");
  }
  return 0;
}