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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#define INF 100000.0
using namespace std;
struct S {
  int town;
  int from;
  int v;
  double cost;
  void set(int a, int b, int c, double d) {
    town = a;
    from = b;
    v = c;
    cost = d;
  }
  inline bool operator<(const S &a) const { return this->cost < a.cost; }
};
double dp[30][30][31];
int d[30][30];
int c[30][30];
multiset<S> Set;
multiset<S>::iterator it;
int main() {
  int n, m;
  int s, g;
  int x, y, t, u;
  double min;
  bool start;
  S a, b;
  while (cin >> n >> m, n || m) {
    memset(d, -1, sizeof(d));
    memset(c, -1, sizeof(c));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 30; j++)
        for (int k = 1; k < 31; k++)
          dp[i][j][k] = INF;
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      x--;
      y--;
      cin >> t;
      d[y][x] = d[x][y] = t;
      cin >> t;
      c[y][x] = c[x][y] = t;
    }
    a.set(s, -1, 1, 0.0);
    Set.insert(a);
    start = true;
    while (!Set.empty()) {
      it = Set.begin();
      a = *it;
      Set.erase(it);
      if (dp[a.town][a.from][a.v] < a.cost)
        continue;
      for (int i = 0; i < n; i++) {
        t = d[a.town][i];
        if (a.from == i || t == -1)
          continue;
        for (int j = -1; j <= 1; j++) {
          u = c[a.town][i];
          if (a.v + j < 1 || a.v + j > u)
            continue;
          if (start && j != 0)
            continue;
          b.set(i, a.town, a.v + j, a.cost + (double)t / (a.v + j));
          if (b.cost < dp[b.town][b.from][b.v]) {
            dp[b.town][b.from][b.v] = b.cost;
            Set.insert(b);
          }
        }
      }
      start = false;
    }
    min = INF;
    for (int i = 0; i < n; i++)
      if (dp[g][i][1] < min)
        min = dp[g][i][1];
    if (min != INF)
      printf("%.5f\\n", min); // cout<<min<<endl;
    else
      cout << "unreachable\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define INF (1e+8)

double dp[30][30][30];
int n, m;
int s, g;
int d[30][30], c[30][30];

double dijkstra() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < 30; k++)
        dp[i][j][k] = INF;
  bool used[30][30][30] = {false};
  dp[s][s][0] = 0;
  double res = INF;
  while (1) {
    int pos = -1, from, speed;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < 30; k++) {
          if (!used[i][j][k] && dp[i][j][k] != INF &&
              (pos == -1 || dp[i][j][k] < dp[pos][from][speed])) {
            pos = i;
            from = j;
            speed = k;
          }
        }
      }
    }
    if (pos == -1)
      break;
    used[pos][from][speed] = true;
    for (int i = 0; i < n; i++) {
      if (d[pos][i] == -1 || i == from)
        continue;
      for (int j = -1; j <= 1; j++) {
        if (pos == from && j != 0)
          continue;
        if (speed + j + 1 <= 0 || c[pos][i] < speed + j + 1)
          continue;
        double t = dp[pos][from][speed] + (double)d[pos][i] / (speed + j + 1);
        if (t < dp[i][pos][speed + j]) {
          dp[i][pos][speed + j] = t;
          if (i == g && speed + j == 0)
            res = min(res, t);
        }
      }
    }
  }
  return res;
}
int main() {
  while (cin >> n >> m, n || m) {
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = -1;
    for (int i = 0; i < m; i++) {
      int x, y, a, b;
      cin >> x >> y >> a >> b;
      x--;
      y--;
      d[x][y] = d[y][x] = a;
      c[x][y] = c[y][x] = b;
    }
    double ans = dijkstra();
    if (ans == INF)
      puts("unreachable");
    else
      printf("%.5f\\n", ans);
  }
  return 0;
}