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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define D first
#define C second

using namespace std;

const double INF = 1000000000.0;

// test
vector<string> sv;

double d[35][35][35];
bool visited[35][35][35];
int N, M, S, G;
pair<int, int> p[35][35];

struct ST {
  double ti;
  int bn;
  int cn;
  int sp;
  ST(double _ti, int _bn, int _cn, int _sp) {
    ti = _ti;
    bn = _bn;
    cn = _cn;
    sp = _sp;
  }
  bool operator>(const ST &e) const { return ti > e.ti; }
};

int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    cin >> S >> G;
    S--;
    G--;

    REP(i, 0, N) REP(j, 0, N) REP(k, 0, 35) d[i][j][k] = INF;
    REP(i, 0, N) REP(j, 0, N) REP(k, 0, 35) visited[i][j][k] = false;
    REP(i, 0, N) REP(j, 0, N) p[i][j].D = p[i][j].C = -1;

    bool flag = true;

    REP(i, 0, M) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      p[x - 1][y - 1].D = p[y - 1][x - 1].D = d;
      p[x - 1][y - 1].C = p[y - 1][x - 1].C = c;
    }

    d[S][S][0] = 0.0;

    priority_queue<ST, vector<ST>, greater<ST>> PQ;
    ST st(0.0, S, S, 0);
    PQ.push(st);

    while (!PQ.empty()) {
      ST u = PQ.top();
      PQ.pop();
      double mtime = u.ti;
      int ubn = u.bn, ucn = u.cn, usp = u.sp;
      // visited[ubn][ucn][usp] = true;
      if (d[ubn][ucn][usp] < mtime)
        continue;
      // cout<<"POP! "<<"u.ti: "<<u.ti<<"; u.bn: "<<u.bn<<"; u.cn: "<<u.cn<<";
      // u.sp: "<<u.sp<<endl;

      if (ucn == G && usp == 1) {
        // printf("%.5lf\\n",d[ubn][ucn][usp]);
        sv.push_back(to_string(mtime));
        flag = false;
        break;
      }

      visited[ubn][ucn][usp] = true;
      for (int v = usp - 1; v <= usp + 1; v++) {
        if (v <= 0)
          continue;
        REP(next, 0, N) {
          if (/*visited[ucn][next][v] ||*/ p[ucn][next].D == -1 ||
              p[ucn][next].C < v || ucn == next)
            continue;
          if (ubn == next)
            continue; // Uturn
          if (d[ucn][next][v] > mtime + (double)p[ucn][next].D / (double)v) {
            d[ucn][next][v] = mtime + (double)p[ucn][next].D / (double)v;
            ST tst(d[ucn][next][v], ucn, next, v);
            PQ.push(tst);
            // cout<<"PUSH! "<<"u.ti: "<<d[ucn][next][v]<<"; u.bn: "<<ucn<<";
            // u.cn: "<<next<<"; u.sp: "<<v<<endl;
          }
        }
      }
    }
    if (flag) {
      // cout<<"unreachable"<<endl;
      sv.push_back("unreachable");
    }
  }

  REP(i, 0, sv.size()) cout << sv[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define D first
#define C second

using namespace std;

const double INF = 1000000000.0;

double d
    [31][31]
    [31]; //?????????????????????????????¨?????????????????¨??????????????°?????????????????????
bool visited[31][31][31];
int N, M, S, G;
pair<int, int> p[31][31];

int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    cin >> S >> G;
    S--;
    G--;

    REP(i, 0, N) REP(j, 0, N) REP(k, 0, 31) d[i][j][k] = INF;
    REP(i, 0, N) REP(j, 0, N) REP(k, 0, 31) visited[i][j][k] = false;
    REP(i, 0, N) REP(j, 0, N) p[i][j].D = p[i][j].C = -1;

    REP(i, 0, M) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      p[x - 1][y - 1].D = p[y - 1][x - 1].D = d;
      p[x - 1][y - 1].C = p[y - 1][x - 1].C = c;
    }

    d[S][S][0] = 0.0;
    // visited[S][S][1] = 0.0;

    while (1) {
      int ubn = -1, ucn = -1, usp = -1;
      double mtime = INF;
      REP(i, 0, N) {
        REP(j, 0, N) {
          REP(k, 0, 31) {
            if (visited[i][j][k])
              continue;
            if (mtime > d[i][j][k]) {
              ubn = i;
              ucn = j;
              usp = k;
              mtime = d[i][j][k];
            }
          }
        }
      }
      // test
      // cout<<"ubn: "<<ubn<<", ucn: "<<ucn<<", usp: "<<usp<<endl;
      if (ucn == G && usp == 1) {
        // cout<<d[ubn][ucn][usp]<<endl;
        printf("%.5lf\\n", d[ubn][ucn][usp]);
        break;
      }
      if (ucn == -1) {
        cout << "unreachable" << endl;
        break;
      }

      visited[ubn][ucn][usp] = true;
      for (int v = usp - 1; v <= usp + 1; v++) {
        if (v <= 0)
          continue;
        REP(next, 0, N) {
          // cout<<"p[ucn][next].D: "<<p[ucn][next].D<<"; p[ucn][next].C:
          // "<<p[ucn][next].C<<endl;
          if (p[ucn][next].D == -1 || p[ucn][next].C < v || ucn == next)
            continue;
          // test
          // cout<<"next: "<<next<<endl;
          d[ucn][next][v] =
              min(d[ucn][next][v], d[ubn][ucn][usp] /*mtime*/ +
                                       (double)p[ucn][next].D / (double)v);
        }
      }
    }
  }

  return 0;
}