Problem p00743 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-7;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }

struct edge {
  int to;
  int limitSpeed;
  int dist;
};
vector<edge> G[101];
void add_edge(int x, int y, int d, int c) {
  edge e;
  int from = x;
  int to = y;
  e.to = y;
  e.limitSpeed = c;
  e.dist = d;
  G[from].push_back(e);
  e.to = x;
  G[to].push_back(e);
}
const double INF = 1e+10;

typedef pair<double, int> pdi;
typedef pair<pdi, pii> Sit;

double d[51][51][51];
double dijkstra(int s, int g, int n) {
  for (int i = 0; i < 51; i++)
    for (int j = 0; j < 51; j++)
      for (int k = 0; k < 51; k++)
        d[i][j][k] = INF;
  priority_queue<Sit> pq;
  pq.push(make_pair(pdi(0, 0), pii(s, 1)));
  d[0][s][1] = 0;
  while (pq.size()) {
    Sit p = pq.top();
    pq.pop();
    double ccost = p.first.first;
    int prvNode = p.first.second;
    int curNode = p.second.first;
    int curSpeed = p.second.second;
    if (!EQ(d[prvNode][curNode][curSpeed], ccost) &&
        d[prvNode][curNode][curSpeed] < ccost)
      continue;
    for (int j = 0; j < G[curNode].size(); j++) {
      edge &e = G[curNode][j];
      int toNode = e.to;
      if (toNode == prvNode)
        continue;
      int limitSpeed = e.limitSpeed;
      int dist = e.dist;
      for (int i = -1; i <= 1; i++) {
        if (prvNode == 0 && i != 0)
          continue;
        int nxtSpeed = curSpeed + i;
        if (nxtSpeed == 0 || nxtSpeed > limitSpeed)
          continue;
        double ncost = ccost + 1.0 * dist / nxtSpeed;
        if (!EQ(ncost, d[curNode][toNode][nxtSpeed]) &&
            ncost < d[curNode][toNode][nxtSpeed]) {
          d[curNode][toNode][nxtSpeed] = ncost;
          pq.push(make_pair(pdi(ncost, curNode), pii(toNode, nxtSpeed)));
        }
      }
    }
  }
  double res = INF;
  for (int i = 1; i <= n; i++)
    res = min(res, d[i][g][1]);
  return res;
}

void solve() {
  int n, m, s, g;
  while (cin >> n >> m && (n | m)) {
    cin >> s >> g;
    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      add_edge(x, y, d, c);
    }
    double res = dijkstra(s, g, n);
    if (EQ(res, INF))
      cout << "unreachable" << endl;
    else
      printf("%.10f\n", res);
    for (int i = 0; i < 101; i++)
      G[i].clear();
  }
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
double EPS = 1e-7;
double EQ(double a, double b) { return abs(a - b) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }

struct edge {
  int to;
  int limitSpeed;
  int dist;
};
vector<edge> G[101];
void add_edge(int x, int y, int d, int c) {
  edge e;
  int from = x;
  int to = y;
  e.to = y;
  e.limitSpeed = c;
  e.dist = d;
  G[from].push_back(e);
  e.to = x;
  G[to].push_back(e);
}
const double INF = 1e+10;

typedef pair<double, int> pdi;
typedef pair<pdi, pii> Sit;

double d[51][51][51];
double dijkstra(int s, int g, int n) {
  for (int i = 0; i < 51; i++)
    for (int j = 0; j < 51; j++)
      for (int k = 0; k < 51; k++)
        d[i][j][k] = INF;
  priority_queue<Sit, vector<Sit>, greater<Sit>> pq;
  pq.push(make_pair(pdi(0, 0), pii(s, 1)));
  d[0][s][1] = 0;
  while (pq.size()) {
    Sit p = pq.top();
    pq.pop();
    double ccost = p.first.first;
    int prvNode = p.first.second;
    int curNode = p.second.first;
    int curSpeed = p.second.second;
    if (!EQ(d[prvNode][curNode][curSpeed], ccost) &&
        d[prvNode][curNode][curSpeed] < ccost)
      continue;
    for (int j = 0; j < G[curNode].size(); j++) {
      edge &e = G[curNode][j];
      int toNode = e.to;
      if (toNode == prvNode)
        continue;
      int limitSpeed = e.limitSpeed;
      int dist = e.dist;
      for (int i = -1; i <= 1; i++) {
        if (prvNode == 0 && i != 0)
          continue;
        int nxtSpeed = curSpeed + i;
        if (nxtSpeed == 0 || nxtSpeed > limitSpeed)
          continue;
        double ncost = ccost + 1.0 * dist / nxtSpeed;
        if (!EQ(ncost, d[curNode][toNode][nxtSpeed]) &&
            ncost < d[curNode][toNode][nxtSpeed]) {
          d[curNode][toNode][nxtSpeed] = ncost;
          pq.push(make_pair(pdi(ncost, curNode), pii(toNode, nxtSpeed)));
        }
      }
    }
  }
  double res = INF;
  for (int i = 1; i <= n; i++)
    res = min(res, d[i][g][1]);
  return res;
}

void solve() {
  int n, m, s, g;
  while (cin >> n >> m && (n | m)) {
    cin >> s >> g;
    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      add_edge(x, y, d, c);
    }
    double res = dijkstra(s, g, n);
    if (EQ(res, INF))
      cout << "unreachable" << endl;
    else
      printf("%.10f\n", res);
    for (int i = 0; i < 101; i++)
      G[i].clear();
  }
}

int main() {
  solve();
  return 0;
}