Problem p00743 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000.0
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<double, int> P;

int n, m;
int s, g;
double dp[40 * 900 + 30 * 30 + 30];
vector<P> e[40 * 900 + 30 * 30 + 30];

void solve() {
  cin >> s >> g;
  s--;
  g--;
  rep(i, 40 * 900 + 30 * 30 + 30) {
    dp[i] = INF;
    e[i].clear();
  }
  rep(i, m) {
    int x, y, c;
    double d;
    cin >> x >> y >> d >> c;
    x--;
    y--;
    for (int j = 1; j <= c; j++)
      rep(k, n) {
        if (k == y)
          continue;
        e[j * 900 + k * 30 + x].push_back(P(d / j, j * 900 + x * 30 + y));
        e[j * 900 + k * 30 + x].push_back(P(d / j, (j + 1) * 900 + x * 30 + y));
        e[j * 900 + k * 30 + x].push_back(P(d / j, (j - 1) * 900 + x * 30 + y));
      }
    for (int j = 1; j <= c; j++)
      rep(k, n) {
        if (k == x)
          continue;
        e[j * 900 + k * 30 + y].push_back(P(d / j, j * 900 + y * 30 + x));
        e[j * 900 + k * 30 + y].push_back(P(d / j, (j + 1) * 900 + y * 30 + x));
        e[j * 900 + k * 30 + y].push_back(P(d / j, (j - 1) * 900 + y * 30 + x));
      }
  }
  priority_queue<P> que;
  que.push(P(0.0, 1 * 900 + s * 30 + s));
  dp[1 * 900 + s * 30 + s] = 0.0;
  while (true) {
    if (que.size() == 0)
      break;
    P q = que.top();
    que.pop();
    if (q.first > dp[q.second])
      continue;
    rep(i, e[q.second].size()) {
      P p = e[q.second][i];
      if (dp[p.second] > q.first + p.first) {
        dp[p.second] = q.first + p.first;
        que.push(P(dp[p.second], p.second));
      }
    }
  }
  double ans = INF;
  rep(i, n) ans = min(ans, dp[0 * 900 + i * 30 + g]);
  if (ans == INF)
    puts("unreachable");
  else
    printf("%.9f\n", ans);
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000.0
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<double, int> P;

int n, m;
int s, g;
double dp[40 * 900 + 30 * 30 + 30];
vector<P> e[40 * 900 + 30 * 30 + 30];

void solve() {
  cin >> s >> g;
  s--;
  g--;
  rep(i, 40 * 900 + 30 * 30 + 30) {
    dp[i] = INF;
    e[i].clear();
  }
  rep(i, m) {
    int x, y, c;
    double d;
    cin >> x >> y >> d >> c;
    x--;
    y--;
    for (int j = 1; j <= c; j++)
      rep(k, n) {
        if (k == y)
          continue;
        e[j * 900 + k * 30 + x].push_back(P(d / j, j * 900 + x * 30 + y));
        e[j * 900 + k * 30 + x].push_back(P(d / j, (j + 1) * 900 + x * 30 + y));
        e[j * 900 + k * 30 + x].push_back(P(d / j, (j - 1) * 900 + x * 30 + y));
      }
    for (int j = 1; j <= c; j++)
      rep(k, n) {
        if (k == x)
          continue;
        e[j * 900 + k * 30 + y].push_back(P(d / j, j * 900 + y * 30 + x));
        e[j * 900 + k * 30 + y].push_back(P(d / j, (j + 1) * 900 + y * 30 + x));
        e[j * 900 + k * 30 + y].push_back(P(d / j, (j - 1) * 900 + y * 30 + x));
      }
  }
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0.0, 1 * 900 + s * 30 + s));
  dp[1 * 900 + s * 30 + s] = 0.0;
  while (true) {
    if (que.size() == 0)
      break;
    P q = que.top();
    que.pop();
    if (q.first > dp[q.second])
      continue;
    rep(i, e[q.second].size()) {
      P p = e[q.second][i];
      if (dp[p.second] > q.first + p.first) {
        dp[p.second] = q.first + p.first;
        que.push(P(dp[p.second], p.second));
      }
    }
  }
  double ans = INF;
  rep(i, n) ans = min(ans, dp[0 * 900 + i * 30 + g]);
  if (ans == INF)
    puts("unreachable");
  else
    printf("%.9f\n", ans);
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    solve();
  }
}