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

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef struct {
  int dis;
  int lim;
} edge;
typedef pair<int, int> pp;
typedef pair<int, pp> node;     // v, (x,y)
typedef pair<double, node> que; // sum, (v, (x,y))

edge input[50][50];
double G[50][50][50]; // x, y, v

int main(void) {
  while (1) {
    int n, m, s, g;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    scanf("%d%d", &s, &g);
    s--, g--;

    memset(input, 0, sizeof(input));
    memset(G, 0, sizeof(G));

    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      scanf("%d%d%d%d", &x, &y, &d, &c);
      x--, y--;
      input[x][y] = input[y][x] = {d, c};
    }

    map<node, double> tbl;
    priority_queue<que> q;
    for (int i = 0; i < n; i++)
      if (input[s][i].dis)
        q.push(que(-0.0, node(1, pp(s, i))));

    double res = 10000000.0;
    while (q.size()) {
      que t = q.top();
      q.pop();

      int v = t.snd.fst;
      int x = t.snd.snd.fst, y = t.snd.snd.snd;

      double sum = -t.fst + 1.0 * input[x][y].dis / v;

      if (tbl.find(t.snd) != tbl.end())
        continue;
      tbl[t.snd] = sum;

      if (y == g && v == 1) {
        res = res < sum ? res : sum;
        continue;
      }

      for (int i = 0; i < n; i++)
        if (i != x && input[y][i].dis) {
          int dv[] = {-1, 0, 1};
          for (int iv = 0; iv < 3; iv++)
            if (0 < v + dv[iv] && v + dv[iv] <= input[y][i].lim &&
                tbl.find(node(v + dv[iv], pp(y, i))) == tbl.end())
              q.push(que(-sum, node(v + dv[iv], pp(y, i))));
        }
    }
    if (res > 1000000.0)
      puts("unreachable");
    else
      printf("%f\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>

#define fst first
#define snd second

using namespace std;

typedef struct {
  int dis;
  int lim;
} edge;
typedef pair<int, int> pp;
typedef pair<int, pp> node;     // v, (x,y)
typedef pair<double, node> que; // sum, (v, (x,y))

edge input[50][50];
double G[50][50][50]; // x, y, v

int main(void) {
  while (1) {
    int n, m, s, g;
    scanf("%d%d", &n, &m);
    if (!n)
      break;
    scanf("%d%d", &s, &g);
    s--, g--;

    memset(input, 0, sizeof(input));
    memset(G, 0, sizeof(G));

    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      scanf("%d%d%d%d", &x, &y, &d, &c);
      x--, y--;
      input[x][y] = input[y][x] = {d, c};
    }

    map<node, double> tbl;
    priority_queue<que> q;
    for (int i = 0; i < n; i++)
      if (input[s][i].dis)
        q.push(que(-0.0, node(1, pp(s, i))));

    double res = 10000000.0;
    while (q.size()) {
      que t = q.top();
      q.pop();

      int v = t.snd.fst;
      int x = t.snd.snd.fst, y = t.snd.snd.snd;

      double sum = -t.fst + 1.0 * input[x][y].dis / v;

      if (tbl.find(t.snd) != tbl.end())
        continue;
      tbl[t.snd] = sum;

      if (y == g && v == 1) {
        res = res < sum ? res : sum;
        continue;
      }

      for (int i = 0; i < n; i++)
        if (i != x && input[y][i].dis) {
          int dv[] = {-1, 0, 1};
          for (int iv = 0; iv < 3; iv++)
            if (0 < v + dv[iv] && v + dv[iv] <= input[y][i].lim)
              q.push(que(-sum, node(v + dv[iv], pp(y, i))));
        }
    }
    if (res > 1000000.0)
      puts("unreachable");
    else
      printf("%f\\n", res);
  }
  return 0;
}