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

#include <cstdio>
#include <iostream>
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> pp;
typedef pair<double, pp> P;
struct edge {
  int to, d, c;
};
int main() {
  double dp[31][31][31];
  int n, m, s, g, x, y, d, c, ds[] = {1, -1, 0};
  while (cin >> n >> m, n || m) {
    vector<edge> G[31];
    cin >> s >> g;
    s--;
    g--;
    rep(i, m) {
      cin >> x >> y >> d >> c;
      x--;
      y--;
      edge e;
      e.d = d;
      e.c = c;
      e.to = y;
      G[x].push_back(e);
      e.to = x;
      G[y].push_back(e);
    }
    rep(i, 31) rep(j, 31) rep(k, 31) dp[i][j][k] = INF;
    dp[0][s][s] = 0.0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0.0, pp(-1, p(0, s))));
    double ans = INF;
    while (!que.empty()) {
      P q = que.top();
      que.pop();
      if (q.second.second.second == g && q.second.second.first == 1) {
        ans = min(
            ans,
            dp[q.second.second.first][q.second.second.second][q.second.first]);
        //	cout<<ans<<endl;
        break;
      }
      // cout<<q.second.second<<' '<<q.second.second.first<<' '<<q.first<<endl;
      if (dp[q.second.second.first][q.second.second.second][q.second.first] <
          q.first)
        continue;
      rep(i, G[q.second.second.second].size()) {
        edge e = G[q.second.second.second][i];
        if (e.to == q.second.first)
          continue;
        rep(j, 3) {
          int pre = q.second.second.first + ds[j];
          if (pre < 1)
            continue;
          double tes = (double)e.d / pre + q.first;
          if (pre <= e.c && dp[pre][e.to][q.second.second.second] > tes) {
            que.push(P(tes, pp(q.second.second.second, p(pre, e.to))));
            dp[pre][e.to][q.second.second.second] = tes;
          }
        }
      }
    }
    if (ans == INF)
      cout << "unreachable" << endl;
    else
      printf("%.5lf\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define INF 100000000
#define rep(i, n) for (int i = 0; i < n; i++)
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<double, int> pp;
typedef pair<pp, p> P;
struct edge {
  int to, d, c;
};
int main() {
  double dp[31][31][31];
  int n, m, s, g, x, y, d, c, ds[] = {1, -1, 0};
  while (cin >> n >> m, n || m) {
    vector<edge> G[31];
    cin >> s >> g;
    s--;
    g--;
    rep(i, m) {
      cin >> x >> y >> d >> c;
      x--;
      y--;
      edge e;
      e.d = d;
      e.c = c;
      e.to = y;
      G[x].push_back(e);
      e.to = x;
      G[y].push_back(e);
    }
    rep(i, 31) rep(j, 31) rep(k, 31) dp[i][j][k] = INF;
    dp[0][s][s] = 0.0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(pp(0.0, -1), p(0, s)));
    double ans = INF;
    while (!que.empty()) {
      P q = que.top();
      que.pop();
      if (q.second.second == g && q.second.first == 1) {
        ans = min(ans, dp[q.second.first][q.second.second][q.first.second]);
        //	cout<<ans<<endl;
        break;
      }
      // cout<<q.second.second<<' '<<q.second.first<<' '<<q.first<<endl;
      if (dp[q.second.first][q.second.second][q.first.second] < q.first.first)
        continue;
      rep(i, G[q.second.second].size()) {
        edge e = G[q.second.second][i];
        if (e.to == q.first.second)
          continue;
        rep(j, 3) {
          int pre = q.second.first + ds[j];
          if (pre < 1)
            continue;
          double tes = (double)e.d / pre + q.first.first;
          if (pre <= e.c && dp[pre][e.to][q.second.second] > tes) {
            que.push(P(pp(tes, q.second.second), p(pre, e.to)));
            dp[pre][e.to][q.second.second] = tes;
          }
        }
      }
    }
    if (ans == INF)
      cout << "unreachable" << endl;
    else
      printf("%.5lf\\n", ans);
  }
  return 0;
}