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

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e15;

struct Edge {
  int to, d, c;
};

int s, g;
int n, m;
vector<Edge> G[31];

typedef pair<int, int> P;
typedef pair<double, int> E;
typedef pair<E, P> PE;

double dij() {
  double ret = INF;
  bool used[n + 1][n + 1][101];
  fill_n(**used, (n + 1) * (n + 1) * 101, false);
  priority_queue<PE, vector<PE>, greater<PE>> q;
  q.push(PE(E(0, n), P(s, 1)));
  while (!q.empty()) {
    PE e = q.top();
    q.pop();
    double c = e.first.first;
    int pre = e.first.second;
    int u = e.second.first, v = e.second.second;
    // cout << u << " " << v << endl;
    if (u == g && v == 1) {
    }

    if (used[u][pre][v])
      continue;
    used[u][pre][v] = true;
    for (Edge &f : G[u]) {
      if (pre == f.to)
        continue;
      if (f.c < v)
        continue;
      if (f.to == g && v == 1) {
        ret = min(ret, c + (double)f.d);
      }
      q.push(PE(E(c + (double)f.d / v, u), P(f.to, v)));
      q.push(PE(E(c + (double)f.d / v, u), P(f.to, v + 1)));
      if (v >= 2)
        q.push(PE(E(c + (double)f.d / v, u), P(f.to, v - 1)));
    }
  }

  return ret;
}

signed main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < 31; i++)
      G[i].clear();
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < m; i++) {
      int a, b, d, c;
      cin >> a >> b >> d >> c;
      a--;
      b--;
      G[a].emplace_back(Edge{b, d, c});
      G[b].emplace_back(Edge{a, d, c});
    }

    double ans = dij();
    if (ans >= INF - 100)
      cout << "unreachable" << endl;
    else
      printf("%.10lf\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e15;

struct Edge {
  int to, d, c;
};

int s, g;
int n, m;
vector<Edge> G[31];

typedef pair<int, int> P;
typedef pair<double, int> E;
typedef pair<E, P> PE;

double dij() {
  double ret = INF;
  int used[n + 1][n + 1][101];
  fill_n(**used, (n + 1) * (n + 1) * 101, 0);
  priority_queue<PE, vector<PE>, greater<PE>> q;
  q.push(PE(E(0, n), P(s, 1)));
  while (!q.empty()) {
    PE e = q.top();
    q.pop();
    double c = e.first.first;
    int pre = e.first.second;
    int u = e.second.first, v = e.second.second;
    // cout << u << " " << v << endl;
    if (u == g && v == 1) {
    }

    if (used[u][pre][v] >= 2)
      continue;
    used[u][pre][v]++;
    for (Edge &f : G[u]) {
      if (pre == f.to)
        continue;
      if (f.c < v)
        continue;
      if (f.to == g && v == 1) {
        ret = min(ret, c + (double)f.d);
      }
      q.push(PE(E(c + (double)f.d / v, u), P(f.to, v)));
      q.push(PE(E(c + (double)f.d / v, u), P(f.to, v + 1)));
      if (v >= 2)
        q.push(PE(E(c + (double)f.d / v, u), P(f.to, v - 1)));
    }
  }

  return ret;
}

signed main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < 31; i++)
      G[i].clear();
    cin >> s >> g;
    s--;
    g--;
    for (int i = 0; i < m; i++) {
      int a, b, d, c;
      cin >> a >> b >> d >> c;
      a--;
      b--;
      G[a].emplace_back(Edge{b, d, c});
      G[b].emplace_back(Edge{a, d, c});
    }

    double ans = dij();
    if (ans >= INF - 100)
      cout << "unreachable" << endl;
    else
      printf("%.10lf\\n", ans);
  }

  return 0;
}