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

#include <algorithm>
#include <cstdio>
#include <queue>

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

using namespace std;

typedef pair<int, pair<int, int>> P;

struct E {
  int t, c;
};

int n, m, p;
vector<E> g[100];
int d[2][100];
double c[2][100];

int main() {
  while (1) {
    scanf("%d%d%d", &n, &m, &p);
    if (n == 0 && m == 0 && p == 0) {
      break;
    }

    rep(i, m) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      g[a].push_back(E{b, c});
      g[b].push_back(E{a, c});
    }

    rep(i, 2) {
      fill(d[i], d[i] + n, 2e8);
      fill(c[i], c[i] + n, 0);

      priority_queue<P, vector<P>, greater<P>> q;
      int s = i == 0 ? 0 : n - 1;
      d[i][s] = 0;
      q.push(P(0, {s, -1}));
      while (!q.empty()) {
        P v = q.top();
        q.pop();
        if (v.first > d[i][v.second.first]) {
          continue;
        }
        bool f = c[i][v.second.first] != 0;
        c[i][v.second.first] +=
            v.second.second != -1 ? c[i][v.second.second] : 1;
        if (f) {
          continue;
        }
        for (auto &x : g[v.second.first]) {
          if (d[i][x.t] >= d[i][v.second.first] + x.c) {
            d[i][x.t] = d[i][v.second.first] + x.c;
            q.push(P(d[i][x.t], {x.t, v.second.first}));
          }
        }
      }
    }

    /*
    rep(i, 2){
        printf("\\n");
        rep(j, n){
            printf("%.8f, %d\\n", c[i][j], d[i][j]);
        }
        printf("\\n");
    }
    */

    rep(i, p) {
      int z;
      scanf("%d", &z);

      if (d[0][z] + d[1][z] == d[0][n - 1]) {
        printf("%.8f\\n", c[0][z] * c[1][z] / c[0][n - 1]);
      } else {
        printf("%.8f\\n", 0.0);
      }
    }

    rep(i, n) { g[i].clear(); }
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>

const int inf = 1000000000;

struct edge {
  int to, cost;
};
typedef std::pair<int, int> P;

int n, m, p;
int c[100];
std::vector<edge> g[100];
int d[100];

int count[100];

void back(int cur) {
  ++count[cur];
  if (cur == 0)
    return;
  for (int i = 0; i < g[cur].size(); ++i) {
    edge e = g[cur][i];
    if (d[cur] == d[e.to] + e.cost)
      back(e.to);
  }
}

int main() {
  while (1) {
    for (int i = 0; i < 100; ++i)
      g[i].clear();
    scanf("%d%d%d", &n, &m, &p);
    if (n == 0 && m == 0 && p == 0)
      break;
    for (int i = 0; i < m; ++i) {
      int e1, e2, dist;
      scanf("%d%d%d", &e1, &e2, &dist);
      g[e1].push_back({e2, dist});
      g[e2].push_back({e1, dist});
    }
    for (int i = 0; i < p; ++i) {
      scanf("%d", c + i);
    }

    std::fill(d, d + n, inf);

    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    d[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P v = que.top();
      que.pop();
      if (v.first > d[v.second])
        continue;
      for (int i = 0; i < g[v.second].size(); ++i) {
        edge e = g[v.second][i];
        if (d[e.to] > d[v.second] + e.cost) {
          d[e.to] = d[v.second] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      }
    }

    for (int i = 0; i < 100; ++i)
      count[i] = 0;
    back(n - 1);
    count[n - 1] = count[0];
    for (int i = 0; i < p; ++i) {
      printf("%.8f\\n", (double)count[c[i]] / count[0]);
    }
    printf("\\n");
  }
  return 0;
}