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

#include <algorithm>
#include <map>
#include <stdio.h>
#include <utility>
using namespace std;

double len[40][40];
int lim[40][40];

double mini[40][40][40];

int main() {
  for (;;) {
    int n, m;
    int s, g;
    int i, j, k, l;

    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    scanf("%d%d", &s, &g);
    s--;
    g--;
    for (i = 0; i < n; i++)
      for (j = 0; j < n; j++)
        lim[i][j] = -1;
    for (i = 0; i < m; i++) {
      int p, q;
      double d;
      int c;
      scanf("%d%d", &p, &q);
      scanf("%lf%d", &d, &c);
      p--;
      q--;
      len[p][q] = len[q][p] = d;
      lim[p][q] = lim[q][p] = c;
    }
    for (k = 0; k <= 30; k++)
      for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
          mini[k][i][j] = 1e9;
    multimap<double, pair<int, pair<int, int>>> que;
    que.insert(make_pair(0.0, make_pair(0, make_pair(s, s))));
    mini[0][s][s] = 0;
    double ans = -1;
    int count = 0;
    for (; !que.empty();) {
      count++;
      pair<double, pair<int, pair<int, int>>> h = *(que.begin());
      que.erase(que.begin());
      double cost = h.first;
      int v = h.second.first;
      int prev = h.second.second.first;
      int now = h.second.second.second;

      if (v == 1 && now == g) {
        ans = cost;
        break;
      }

      for (i = 0; i < n; i++) {
        if (i != prev && len[now][i] >= 0) {
          int newv;
          for (newv = v - 1; newv <= v + 1; newv++) {
            if (newv <= 0 || newv > lim[now][i])
              continue;
            // que ni ireru
            double ncost = cost + (double)len[now][i] / newv;
            int nprev = now;
            int nnow = i;
            if (mini[newv][nprev][nnow] > ncost) {
              mini[newv][nprev][nnow] = ncost;
              que.insert(
                  make_pair(ncost, make_pair(newv, make_pair(nprev, nnow))));
            }
          }
        }
      }
    } // printf("count: %d\\n",count);
    if (ans >= 0)
      printf("%.10f\\n", ans);
    else
      printf("unreachable\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) rep2(i, 0, n)
#define ll long long
#define P pair<double, int>
#define pb push_back

int n, m, s, g, D[30][30], C[30][30];
double d[30][30][31];

int main() {
  while (cin >> n >> m && n) {
    cin >> s >> g;
    s--;
    g--;
    rep(i, n) {
      fill(D[i], D[i] + n, INF);
      fill(C[i], C[i] + n, 0);
      rep(j, n) fill(d[i][j], d[i][j] + 31, INF);
    }
    rep(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      x--;
      y--;
      D[x][y] = D[y][x] = d;
      C[x][y] = C[y][x] = c;
    }
    double ans = INF;
    priority_queue<P, vector<P>, greater<P>> Q;
    d[s][s][0] = 0.0;
    Q.push(P(0.0, s * 930 + s * 31 + 0));
    while (Q.size()) {
      P p = Q.top();
      Q.pop();
      double t = p.first;
      int cur = p.second / 930, prev = (p.second / 31) % 30, v = p.second % 31;
      if (t > d[cur][prev][v])
        continue;
      if (cur == g && v == 1) {
        ans = d[cur][prev][v];
        break;
      }
      rep(i, n) {
        if (i != prev && D[cur][i] != INF) {
          rep2(j, -1, 2) {
            int nv = v + j;
            if (0 < nv && nv <= C[cur][i]) {
              double nt = t + D[cur][i] * 1.0 / nv;
              if (nt < d[i][cur][nv]) {
                d[i][cur][nv] = nt;
                Q.push(P(nt, i * 930 + cur * 31 + nv));
              }
            }
          }
        }
      }
    }
    if (ans != INF)
      cout << setprecision(10) << ans << endl;
    else
      cout << "unreachable" << endl;
  }
}