Problem p00743 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int MAX = 35;
const int INF = (1 << 25);
int n, m;

struct Edge {
  int to;
  double dis, lim;
  Edge(int a, double b, double c) : to(a), dis(b), lim(c) {}
};

struct State {
  int pos, from;
  double speed, t;
  State(int a, double b, double c, int f) : pos(a), speed(b), t(c), from(f) {}
  bool operator<(const State &s) const { return t > s.t; }
};

vector<Edge> E[MAX];
double T[MAX][MAX][MAX];
// T[i][j][k] := shortest time from i to j with speed k.

void init() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++)
      for (int k = 0; k < MAX; k++)
        T[i][j][k] = INF;
    E[i].clear();
  }
}

void input() {
  for (int i = 0; i < m; i++) {
    int x, y;
    double d, c;
    cin >> x >> y >> d >> c;
    //   cout << x << " " << y << " " << d << " " << c << endl;
    E[x].push_back(Edge(y, d, c));
    E[y].push_back(Edge(x, d, c));
  }
}

void solve(int s, int g) {

  priority_queue<State> Q;
  T[0][s][0] = 0;
  Q.push(State(s, 0, 0, 0));

  while (!Q.empty()) {
    State sta = Q.top();
    Q.pop();

    if (sta.t > T[sta.from][sta.pos][(int)sta.speed])
      continue;

    for (int i = 0; i < E[sta.pos].size(); i++) {
      int np = E[sta.pos][i].to;

      if (np == sta.from)
        continue;

      double sp[] = {-1, 0, 1};

      for (int j = 0; j < 3; j++) {
        double ns = sta.speed + sp[j];
        if (ns <= 0 || ns > E[sta.pos][i].lim)
          continue;
        double nt = sta.t + E[sta.pos][i].dis / ns;
        if (T[sta.pos][np][(int)ns] > nt) {
          T[sta.pos][np][(int)ns] = nt;
          Q.push(State(np, ns, nt, sta.pos));
        }
      }
    }
  }

  double ans = INF;

  for (int i = 0; i < MAX; i++)
    ans = min(ans, T[i][g][1]);

  if (ans == INF)
    cout << "unreachable" << endl;
  else
    printf("%.5f\\n", ans);
}

int main() {

  while (cin >> n >> m && n + m) {
    init();
    int s, g;
    cin >> s >> g;
    input();
    solve(s, g);
  }
  return 0;
}

Error: 0
0
0
0
1
2
1
1
0
0
1
1
2
4
4
3
3
2
1
2
2
4
3
5
5
4
4
3
2
1
0
0
0
0
0
1
2
5
8
10
12
14
13
12
11
10
14
16
15
14
16
19
18
17
16
15
14
13
15
17
16
15
14
13
12
11
15
17
16
15
14
13
15
18
17
16
15
14
16
18
17
16
15
14
18
20
19
18
17
16
19
21
20
19
18
17
19
21
20
19
18
17
19
23
22
21
20
19
21
24
23
22
21
20
22
24
23
22
21
20
24
26
25
24
23
22
25
27
26
25
24
23
25
27
26
25
24
23
25
29
28
27
26
25
27
30
29
28
27
26
28
30
29
28
27
26
28
32
31
30
29
28
30
33
32
31
30
29
31
33
32
31
30
29
33
35
34
33
32
31
34
36
35
34
33
32
34
36
35
34
33
32
36
38
37
36
35
34
37
39
38
37
36
35
36
37
36
35
34
33
33
33
32
31
30
29
32
33
34
34
34
35
36
36
36
36
36
37
38
37
37
36
37
37
38
38
38
38
38
38
38
38
38
38
38
38
38
38
39
39
38
39
39
40
40
40
40
40
40
39
39
39
39
39
39
39
39
39
40
40
41
41
41
41
42
42
41
41
41
41
41
41
41
41
41
42
42
42
42
43
43
43
43
42
43
43
43
43
43
43
43
43
42
42
42
43
43
43
43
44
44
44
44
45
45
45
45
44
44
44
44
44
44
44
45
45
45
45
46
46
46
46
45
46
46
46
46
46
46
46
46
45
45
45
46
46
46
46
47
47
47
47
48
48
47
47
47
47
47
47
47
47
47
48
48
48
48
47
48
48
48
48
49
49
48
48
48
48
48
48
48
48
48
49
49
48
48
48
49
49
49
49
48
49
49
49
49
49
49
48
48
48
48
48
49
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int MAX = 35;
const int INF = (1 << 25);
int n, m;

struct Edge {
  int to;
  double dis, lim;
  Edge(int a, double b, double c) : to(a), dis(b), lim(c) {}
};

struct State {
  int pos, from;
  double speed, t;
  State(int a, double b, double c, int f) : pos(a), speed(b), t(c), from(f) {}
  bool operator<(const State &s) const { return t > s.t; }
};

vector<Edge> E[MAX];
double T[MAX][MAX][MAX];
// T[i][j][k] := shortest time from i to j with speed k.

void init() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX; j++)
      for (int k = 0; k < MAX; k++)
        T[i][j][k] = INF;
    E[i].clear();
  }
}

void input() {
  for (int i = 0; i < m; i++) {
    int x, y;
    double d, c;
    cin >> x >> y >> d >> c;
    //   cout << x << " " << y << " " << d << " " << c << endl;
    E[x].push_back(Edge(y, d, c));
    E[y].push_back(Edge(x, d, c));
  }
}

void solve(int s, int g) {

  priority_queue<State> Q;
  T[0][s][0] = 0;
  Q.push(State(s, 0, 0, -1));

  while (!Q.empty()) {
    State sta = Q.top();
    Q.pop();

    cerr << Q.size() << endl;

    if (sta.t > T[sta.from][sta.pos][(int)sta.speed])
      continue;

    for (int i = 0; i < E[sta.pos].size(); i++) {
      int np = E[sta.pos][i].to;

      if (np == sta.from)
        continue;

      double sp[] = {-1, 0, 1};

      for (int j = 0; j < 3; j++) {
        double ns = sta.speed + sp[j];
        if (ns <= 0 || ns > E[sta.pos][i].lim)
          continue;
        double nt = sta.t + E[sta.pos][i].dis / ns;
        if (T[sta.pos][np][(int)ns] > nt) {
          T[sta.pos][np][(int)ns] = nt;
          Q.push(State(np, ns, nt, sta.pos));
        }
      }
    }
  }

  // cerr << "come " << endl;

  double ans = INF;

  for (int i = 0; i < MAX; i++)
    ans = min(ans, T[i][g][1]);

  if (ans == INF)
    cout << "unreachable" << endl;
  else
    printf("%.5f\\n", ans);
}

int main() {

  while (cin >> n >> m && n + m) {
    init();
    int s, g;
    cin >> s >> g;
    input();
    solve(s, g);
  }
  return 0;
}