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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))

// repetition//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int n, m, s, g;
map<int, vector<TIII>> r;
map<TIII, double> v;

int main() {
  while (1) {
    double ret = -1.0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    cin >> s >> g;
    r.clear();
    v.clear();
    REP(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      r[x].PB(MT(y, d, c));
      r[y].PB(MT(x, d, c));
    }
    priority_queue<pair<double, TIII>, vector<pair<double, TIII>>,
                   greater<pair<double, TIII>>>
        q;
    q.push(MP(0.0, MP(s, MP(-1, 0))));
    while (!q.empty()) {
      pair<double, TIII> tmp = q.top();
      q.pop();
      if (EXIST(v, tmp.second))
        continue;
      if (tmp.second.first == g && tmp.second.second.second == 1) {
        ret = tmp.first;
        break;
      }
      v[tmp.second] = tmp.first;
      int city = tmp.second.first;
      int speed = tmp.second.second.second;
      int pre = tmp.second.second.first;
      double now = tmp.first;
      REP(i, r[city].size()) {
        TIII f = r[city][i];
        if (f.first == pre)
          continue;
        FOR(sp, speed - 1, speed + 2) {
          if (sp < 1)
            continue;
          if (f.second.second >= sp && sp > 0) {
            if (!EXIST(v, MP(f.first, MP(city, sp)))) {
              q.push(MP(now + (f.second.first * 1.0) / (sp * 1.0),
                        MP(f.first, MP(city, sp))));
            }
          }
        }
      }
    }
    if (ret < 0.0)
      cout << "unreachable" << endl;
    else
      printf("%.6f\\n", ret);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))

// repetition//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int n, m, s, g;
map<int, vector<TIII>> r;
map<TIII, double> v;

int main() {
  while (1) {
    double ret = -1.0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    cin >> s >> g;
    r.clear();
    v.clear();
    REP(i, m) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      r[x].PB(MT(y, d, c));
      r[y].PB(MT(x, d, c));
    }
    priority_queue<pair<double, TIII>, vector<pair<double, TIII>>,
                   greater<pair<double, TIII>>>
        q;
    q.push(MP(0.0, MP(s, MP(-1, 0))));
    while (!q.empty()) {
      pair<double, TIII> tmp = q.top();
      q.pop();
      if (EXIST(v, tmp.second))
        continue;
      if (tmp.second.first == g && tmp.second.second.second == 1) {
        ret = tmp.first;
        break;
      }
      v[tmp.second] = tmp.first;
      int city = tmp.second.first;
      int speed = tmp.second.second.second;
      int pre = tmp.second.second.first;
      double now = tmp.first;
      REP(i, r[city].size()) {
        FOR(sp, speed - 1, speed + 2) {
          if (sp < 1)
            continue;
          TIII f = r[city][i];
          if (f.second.second >= sp && sp > 0 && f.first != pre)
            q.push(MP(now + (f.second.first * 1.0) / (sp * 1.0),
                      MP(f.first, MP(city, sp))));
        }
      }
    }
    if (ret < 0.0)
      cout << "unreachable" << endl;
    else
      printf("%.6f\\n", ret);
  }
  return 0;
}