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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class data {
public:
  int p, q, c;
  data(int p0, int q0, int c0) {
    p = p0;
    q = q0;
    c = c0;
  }
};

void solve(int n, int start, int goal, vector<vector<int>> distance,
           vector<vector<int>> limit) {
  vector<bool> easyCheck(n, false);
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int a = q.front();
    if (!easyCheck[a]) {
      easyCheck[a] = true;
      for (int i = 0; i < n; ++i) {
        if (distance[a][i] > -1)
          q.push(i);
      }
    }
    q.pop();
  }
  if (!easyCheck[goal]) {
    cout << "unreachable" << endl;
    return;
  }

  vector<vector<vector<bool>>> check(
      n, vector<vector<bool>>(n, vector<bool>(31, false)));
  multimap<double, data> m;
  for (int i = 0; i < n; ++i) {
    if (distance[start][i] > -1)
      m.insert(make_pair(distance[start][i], data(i, start, 1)));
  }

  for (;;) {
    multimap<double, data>::iterator it = m.begin();
    double t = it->first;
    int p = it->second.p;
    int q = it->second.q;
    int c = it->second.c;
    if (p == goal && c == 1) {
      printf("%.10f\\n", t);
      return;
    }
    m.erase(it);

    if (!check[p][q][c]) {
      check[p][q][c] = true;
      for (int i = 0; i < n; ++i) {
        if (i == p || i == q || distance[i][p] == -1)
          continue;
        for (int j = -1; j <= 1; ++j) {
          if (0 < c + j && c + j <= limit[p][i] && !check[i][p][c + j])
            m.insert(make_pair(t + distance[p][i] / static_cast<double>(c + j),
                               data(i, p, c + j)));
        }
      }
    }
  }
}

int main() {
  for (;;) {
    int n, m, s, g;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cin >> s >> g;
    --s;
    --g;

    vector<vector<int>> distance(n, vector<int>(n, -1));
    vector<vector<int>> limit(n, vector<int>(n, -1));
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      cin >> distance[x][y] >> limit[x][y];
      distance[y][x] = distance[x][y];
      limit[y][x] = limit[x][y];
    }

    solve(n, s, g, distance, limit);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class data {
public:
  int p, q, c;
  data(int p0, int q0, int c0) {
    p = p0;
    q = q0;
    c = c0;
  }
};

void solve(int n, int start, int goal, vector<vector<int>> distance,
           vector<vector<int>> limit) {
  vector<bool> easyCheck(n, false);
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int a = q.front();
    if (!easyCheck[a]) {
      easyCheck[a] = true;
      for (int i = 0; i < n; ++i) {
        if (distance[a][i] > -1)
          q.push(i);
      }
    }
    q.pop();
  }
  if (!easyCheck[goal]) {
    cout << "unreachable" << endl;
    return;
  }

  vector<vector<vector<bool>>> check(
      n, vector<vector<bool>>(n, vector<bool>(31, false)));
  multimap<double, data> m;
  for (int i = 0; i < n; ++i) {
    if (distance[start][i] > -1)
      m.insert(make_pair(distance[start][i], data(i, start, 1)));
  }

  for (;;) {
    multimap<double, data>::iterator it = m.begin();
    double t = it->first;
    int p = it->second.p;
    int q = it->second.q;
    int c = it->second.c;
    if (p == goal && c == 1) {
      printf("%.10f\\n", t);
      return;
    }
    m.erase(it);

    if (!check[p][q][c]) {
      check[p][q][c] = true;
      for (int i = 0; i < n; ++i) {
        for (int j = -1; j <= 1; ++j) {
          if (i != p && i != q && distance[i][p] > -1 && 0 < c + j &&
              c + j <= limit[p][i])
            m.insert(make_pair(t + distance[p][i] / static_cast<double>(c + j),
                               data(i, p, c + j)));
        }
      }
    }
  }
}

int main() {
  for (;;) {
    int n, m, s, g;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cin >> s >> g;
    --s;
    --g;

    vector<vector<int>> distance(n, vector<int>(n, -1));
    vector<vector<int>> limit(n, vector<int>(n, -1));
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      --x;
      --y;
      cin >> distance[x][y] >> limit[x][y];
      distance[y][x] = distance[x][y];
      limit[y][x] = limit[x][y];
    }

    solve(n, s, g, distance, limit);
  }

  return 0;
}