alexjercan/bugnet huggingface dataset:
Problem p00719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
#define INF 2000000000
#define pb push_back
int N, M, P, a, b;
int t[10];
struct edge {
  int to;
  double cost;
  edge(int to, double cost) : to(to), cost(cost) {}
};
vector<edge> g[40];
double dp[40][1 << 8];
double rec(int v, int bit) {
  if (dp[v][bit] != -1)
    return dp[v][bit];
  double res = INF;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i].to;
    double cost = g[v][i].cost;
    for (int j = 0; j < N; j++) {
      if (!((bit >> j) & 1)) {
        res = min(res, rec(u, bit | (1 << j)) + cost / t[j]);
      }
    }
  }
  return dp[v][bit] = res;
}
int main() {
  while (1) {
    for (int i = 0; i < 40; i++)
      g[i].clear();
    scanf("%d %d %d %d %d", &N, &M, &P, &a, &b);
    if (N == 0 && M == 0 && P == 0 && a == 0 && b == 0)
      break;
    a--;
    b--;
    for (int i = 0; i < N; i++)
      scanf("%d", &t[i]);
    for (int i = 0; i < P; i++) {
      int x, y;
      double z;
      scanf("%d %d %lf", &x, &y, &z);
      x--;
      y--;
      g[x].pb(edge(y, z));
      g[y].pb(edge(x, z));
    }
    for (int i = 0; i < M; i++)
      for (int j = 0; j < (1 << N); j++)
        dp[i][j] = -1;
    dp[a][(1 << N) - 1] = 0;
    double ans = INF;
    for (int i = 0; i < (1 << N); i++)
      ans = min(ans, rec(b, i));
    if (ans == INF)
      printf("Impossible\\n");
    else
      printf("%f\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define INF 99999999

using namespace std;

void solve() {
  int n, m, p, a, b;
  cin >> n >> m >> p >> a >> b;
  if (!n && !m && !p && !a && !b)
    return;

  int t[10] = {0};
  int cost[30][30];
  fill(cost[0], cost[29], -1);
  double dp[(1 << 8) + 1][30];
  fill(dp[0], dp[(1 << 8)], INF);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  for (int i = 0; i < p; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    cost[x][y] = cost[y][x] = z;
  }
  dp[(1 << n) - 1][b - 1] = 0;
  double ans = INF;
  for (int s = (1 << n) - 1; s >= 0; s--) {
    ans = min(ans, dp[s][a - 1]);
    for (int v = 0; v < m; v++) {
      for (int i = 0; i < n; i++) {
        if (s >> i & 1) {
          for (int u = 0; u < m; u++) {
            if (cost[v][u] >= 0) {
              dp[s & ~(1 << i)][u] = min(dp[s & ~(1 << i)][u],
                                         dp[s][v] + (double)cost[v][u] / t[i]);
            }
          }
        }
      }
    }
  }
  if (ans == INF)
    cout << "Impossible" << endl;
  else
    cout << ans << endl;
}

int main(void) {
  while (1) {
    solve();
  }
  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
#include <vector>
#define INF 0x3FFFFFFF

using namespace std;

typedef struct eda {
  int x;
  int y;
  int z;
} edge;

int main(void) {
  int n, m, p, a, b, t[8], i, j, k, l;
  double d[30][1 << 8], ans;
  vector<edge> e;
  edge one;
  while (1) {
    scanf("%d%d%d%d%d", &n, &m, &p, &a, &b);
    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;
    a--, b--;
    for (i = 0; i < n; i++)
      scanf("%d", &t[i]);
    for (i = 0; i < p; i++) {
      e.push_back(one);
      scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
      e[i].x--, e[i].y--;
    }
    for (i = 0; i < p; i++) {
      e.push_back(one);
      e[i + p].x = e[i].y, e[i + p].y = e[i].x, e[i + p].z = e[i].z;
    }
    for (i = 0; i < m; i++)
      for (j = 0; j < (1 << n); j++)
        d[i][j] = (double)INF;

    d[a][(1 << n) - 1] = 0.0;
    for (i = 0; i < m; i++) {            // max arith 30
      for (j = 0; j < p * 2; j++) {      // max arith 100
        for (k = 0; k < (1 << n); k++) { // max arith 256
          for (l = 0; l < n; l++) {      // max arith 8
            if (k & (1 << l) &&
                d[e[j].y][k - (1 << l)] > d[e[j].x][k] + (double)e[j].z / t[l])
              d[e[j].y][k - (1 << l)] = d[e[j].x][k] + (double)e[j].z / t[l];
          }
        }
      }
    }
    ans = (double)INF;
    for (i = 0; i < (1 << n); i++)
      if (ans > d[b][i])
        ans = d[b][i];
    if (ans != (double)INF)
      printf("%.3f\\n", ans);
    else
      printf("Impossible\\n");
    while (!e.empty())
      e.pop_back();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
#define INF 0x3FFFFFFF

typedef struct eda {
  int x;
  int y;
  int z;
} edge;

int main(void) {
  int n, m, p, a, b, t[8], i, j, k, l;
  double d[30][1 << 8], ans;
  edge e[200];
  while (1) {
    scanf("%d%d%d%d%d", &n, &m, &p, &a, &b);
    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;
    a--, b--;
    for (i = 0; i < n; i++)
      scanf("%d", &t[i]);
    for (i = 0; i < p; i++) {
      scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
      e[i].x--, e[i].y--;
    }
    for (i = 0; i < p; i++)
      e[i + p].x = e[i].y, e[i + p].y = e[i].x, e[i + p].z = e[i].z;
    for (i = 0; i < m; i++)
      for (j = 0; j < (1 << n); j++)
        d[i][j] = (double)INF;

    d[a][(1 << n) - 1] = 0.0;
    for (i = 0; i < m; i++) {            // max arith 30
      for (j = 0; j < p * 2; j++) {      // max arith 100
        for (k = 0; k < (1 << n); k++) { // max arith 256
          for (l = 0; l < n; l++) {      // max arith 8
            if (k & (1 << l) &&
                d[e[j].y][k - (1 << l)] > d[e[j].x][k] + (double)e[j].z / t[l])
              d[e[j].y][k - (1 << l)] = d[e[j].x][k] + (double)e[j].z / t[l];
          }
        }
      }
    }
    ans = (double)INF;
    for (i = 0; i < (1 << n); i++)
      if (ans > d[b][i])
        ans = d[b][i];
    if (ans != (double)INF)
      printf("%.3f\\n", ans);
    else
      printf("Impossible\\n");
  }
  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, double> P;
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;

    double t[8], tp;
    vector<P> e[31];

    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int j = 0, x, y; j < p; j++) {
      cin >> x >> y >> tp;
      x--, y--;
      e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp));
    }

    double dp[1 << 8][30];
    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        dp[i][j] = 1e4;
    dp[0][a] = 0;

    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        for (int k = 0; k < n; k++)
          if (!(i & 1 << k))
            for (int l = 0; l < e[j].size(); l++)
              dp[i + (1 << k)][e[j][l].first] =
                  min(dp[i + (1 << k)][e[j][l].first],
                      dp[i][j] + e[j][l].second / t[k]);

    double ans = 1e4;
    for (int i = 0; i < 1 << n; i++)
      ans = min(ans, dp[i][b]);
    if (ans == 1e4)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, double> P;
int main() {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b, n) {
    a--, b--;

    double t[8], tp;
    vector<P> e[31];

    for (int i = 0; i < n; i++)
      cin >> t[i];
    for (int j = 0, x, y; j < p; j++) {
      cin >> x >> y >> tp;
      x--, y--;
      e[x].push_back(P(y, tp)), e[y].push_back(P(x, tp));
    }

    double dp[1 << 8][30];
    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        dp[i][j] = 1e4;
    dp[0][a] = 0;

    for (int i = 0; i < 1 << n; i++)
      for (int j = 0; j < m; j++)
        for (int k = 0; k < n; k++)
          if (!(i & 1 << k))
            for (int l = 0; l < e[k].size(); l++)
              dp[i + (1 << k)][e[j][l].first] =
                  min(dp[i + (1 << k)][e[j][l].first],
                      dp[i][j] + e[j][l].second / t[k]);

    double ans = 1e4;
    for (int i = 0; i < 1 << n; i++)
      ans = min(ans, dp[i][b]);
    if (ans == 1e4)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

#define INF 1000000000.0

using namespace std;

int n, m, p, a, b, x, y, z, t[8];

double dp[256][30];

vector<pair<int, int>> G[30];

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &p);
    scanf("%d", &a);
    scanf("%d", &b);

    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;

    for (int i = 0; i < m; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      scanf("%d", &t[i]);

    for (int i = 0; i < p; i++) {
      scanf("%d", &x);
      scanf("%d", &y);
      scanf("%d", &z);

      G[x - 1].push_back(make_pair(y - 1, z));
      G[y - 1].push_back(make_pair(x - 1, z));
    }

    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = INF;
      }
    }

    dp[0][a - 1] = 0.0;

    for (int i = 1; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k = 0; k < m; k++) {
            for (int l = 0; l < G[k].size(); l++) {
              int node = G[k][l].first;

              double dist = G[k][l].second * 1.0 / t[j];

              dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist);
            }
          }
        }
      }
    }

    double ret = INF;

    for (int i = 0; i < (1 << n); i++) {
      ret = min(ret, dp[i][b - 1]);
    }

    if (ret != INF) {
      printf("%.12f\\n", ret);
    } else {
      printf("Impossible\\n");
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

#define INF 1000000000.0

using namespace std;

int n, m, p, a, b, x, y, z, t[8];

double dp[128][30];

vector<pair<int, int>> G[30];

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &p);
    scanf("%d", &a);
    scanf("%d", &b);

    if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
      break;

    for (int i = 0; i < m; i++)
      G[i].clear();

    for (int i = 0; i < n; i++)
      scanf("%d", &t[i]);

    for (int i = 0; i < p; i++) {
      scanf("%d", &x);
      scanf("%d", &y);
      scanf("%d", &z);

      G[x - 1].push_back(make_pair(y - 1, z));
      G[y - 1].push_back(make_pair(x - 1, z));
    }

    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = INF;
      }
    }

    dp[0][a - 1] = 0.0;

    for (int i = 1; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          for (int k = 0; k < m; k++) {
            for (int l = 0; l < G[k].size(); l++) {
              int node = G[k][l].first;

              double dist = G[k][l].second * 1.0 / t[j];

              dp[i][node] = min(dp[i][node], dp[i - (1 << j)][k] + dist);
            }
          }
        }
      }
    }

    double ret = INF;

    for (int i = 0; i < (1 << n); i++) {
      ret = min(ret, dp[i][b - 1]);
    }

    if (ret != INF) {
      printf("%.12f\\n", ret);
    } else {
      printf("Impossible\\n");
    }
  }

  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#define INF 1e9
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
typedef pair<double, P> PP;
int n, m, p, s, g, T[10];
vector<P> x[32];

double dijkstra() {
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  double D[1 << 8][31];
  for (int i = 0; i < (1 << n); i++)
    for (int j = 0; j <= m; j++)
      D[i][j] = INF;
  Q.push(PP(0, P(s, 0)));
  D[0][s] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    double dis = t.f;
    int pos = t.s.f, bit = t.s.s;
    if (pos == g)
      return dis;
    if (D[bit][pos] < dis)
      continue;
    for (int i = 0; i < x[pos].size(); i++)
      for (int j = 0; j < n; j++) {
        int nbit = (bit | (1 << j)), npos = x[pos][i].f;
        double ndis = dis + (1.0 * x[pos][i].s / T[j]);
        if (bit != nbit && D[nbit][npos] > ndis)
          Q.push(PP(ndis, P(npos, nbit))), D[nbit][npos] = ndis;
      }
  }
  return 0;
}

int main() {
  while (1) {
    cin >> n >> m >> p >> s >> g;
    if (!n && !m && !p && !s && !g)
      break;
    for (int i = 0; i <= m; i++)
      x[i].clear();
    for (int i = 0; i < n; i++)
      cin >> T[i];
    for (int i = 0, a, b, c; i < p; i++)
      cin >> a >> b >> c, x[a].push_back(P(b, c)), x[b].push_back(P(a, c));
    double ans = dijkstra();
    if (ans == 0)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1e9
using namespace std;
typedef pair<int, int> P;
typedef pair<double, P> PP;
int n, m, p, s, g, T[10], x[31][31];

double dijkstra() {
  double D[1 << 8][31];
  priority_queue<PP, vector<PP>, greater<PP>> Q;
  for (int i = 0; i < (1 << 8); i++)
    for (int j = 0; j < 31; j++)
      D[i][j] = INF;
  Q.push(PP(0, P(s, 0)));
  D[0][s] = 0;

  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    double dis = t.first;
    int pos = t.second.first, bit = t.second.second;
    if (pos == g)
      return dis;
    if (D[bit][pos] < dis)
      continue;

    for (int i = 1; i <= m; i++) {
      if (!x[pos][i])
        continue;
      for (int j = 0; j < n; j++) {
        int nbit = bit | (1 << j);
        double ndis = dis + (1.0 * x[pos][i] / T[j]);
        if ((bit & (1 << j)) || D[nbit][i] < ndis)
          continue;
        Q.push(PP(ndis, P(i, nbit)));
        D[nbit][i] = ndis;
      }
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> p >> s >> g;
    if (!n && !m && !p && !s && !g)
      break;
    memset(x, 0, sizeof(x));
    for (int i = 0; i < n; i++)
      cin >> T[i];
    for (int i = 0, a, b, c; i < p; i++)
      cin >> a >> b >> c, x[a][b] = x[b][a] = c;
    double ans = dijkstra();
    if (ans == -1)
      cout << "Impossible" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
#define inf 101010
typedef pair<int, int> pii;
typedef pair<double, pii> pp;

int main(void) {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b && n) {
    a--, b--;
    vector<int> t(n);
    for (int i = 0; i < n; i++)
      cin >> t[i];
    vector<vector<pii>> g(m, vector<pii>());
    for (int i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--;
      g[x].push_back(make_pair(y, z));
      g[y].push_back(make_pair(x, z));
    }

    vector<vector<double>> d(m, vector<double>(1 << n, inf));
    priority_queue<pp, vector<pp>, greater<pp>> que;
    que.push(make_pair(0.0, make_pair(a, (1 << n) - 1)));

    double ans = inf;
    while (!que.empty()) {
      pp p = que.top();
      que.pop();
      double cost = p.first;
      int cur = p.second.first, bit = p.second.second;
      if (cur == b) {
        ans = cost;
        break;
      }

      for (int i = 0; i < g[cur].size(); i++) {
        int to = g[cur][i].first, len = g[cur][i].second;
        for (int j = 0; j < n; j++) {
          if (bit & 1 << j) {
            double x = len / (double)t[j];
            int nbit = bit & ~(1 << j);
            if (cost + x >= d[to][nbit])
              continue;
            d[to][nbit] = cost + x;
            que.push(make_pair(cost + x, make_pair(to, nbit)));
          }
        }
      }
    }
    if (ans == inf)
      cout << "Impossible" << endl;
    else
      printf("%.8lf\\n", ans);
  }
}

Error: WA

Fixed submission:

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

using namespace std;
#define inf 101010
typedef pair<int, int> pii;
typedef pair<double, pii> pp;

int main(void) {
  int n, m, p, a, b;
  while (cin >> n >> m >> p >> a >> b && n) {
    a--, b--;
    vector<int> t(n);
    for (int i = 0; i < n; i++)
      cin >> t[i];
    vector<vector<pii>> g(m, vector<pii>());
    for (int i = 0; i < p; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--;
      g[x].push_back(make_pair(y, z));
      g[y].push_back(make_pair(x, z));
    }

    priority_queue<pp, vector<pp>, greater<pp>> que;
    que.push(make_pair(0.0, make_pair(a, (1 << n) - 1)));

    double ans = inf;
    while (!que.empty()) {
      pp p = que.top();
      que.pop();
      double cost = p.first;
      int cur = p.second.first, bit = p.second.second;
      if (cur == b) {
        ans = cost;
        break;
      }

      for (int i = 0; i < g[cur].size(); i++) {
        int to = g[cur][i].first, len = g[cur][i].second;
        for (int j = 0; j < n; j++) {
          if (bit & 1 << j) {
            double x = len / (double)t[j];
            que.push(make_pair(cost + x, make_pair(to, bit & ~(1 << j))));
          }
        }
      }
    }
    if (ans == inf)
      cout << "Impossible" << endl;
    else
      printf("%.8lf\\n", ans);
  }
}
Problem p00719 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * Problem link
 *
 */

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> V;
typedef vector<V> VV;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
template <typename T> inline bool chmin(T &a, T b) {
  bool res = a > b;
  if (res)
    a = b;
  return res;
}
typedef pair<int, int> P;
typedef vector<P> VP;
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  int H, N, A, B, M;
  while (1) {
    cin >> H >> N >> M >> A >> B;
    if (H + N + A + B + M == 0)
      break;
    vector<double> h(H);
    REP(i, H) cin >> h[i];
    vector<int> a(M), b(M);
    vector<double> c(M);
    REP(i, M) cin >> a[i] >> b[i] >> c[i];
    double res = 114514 + 1;
    vector<vector<double>> dp(1 << H, vector<double>(N + 1, res));
    dp[0][A] = 0;
    REP(bit, (1 << H)) {
      chmin(res, dp[bit][B]);
      REP(k, H) if ((bit & (1 << k)) == 0) {
        int nxt = bit | (1 << k);
        REP(j, M) chmin(dp[nxt][b[j]], dp[bit][a[j]] + c[j] / h[k]);
        REP(j, M) chmin(dp[nxt][a[j]], dp[bit][b[j]] + c[j] / h[k]);
      }
    }

    if (res > 114514)
      cout << "Impossible"
           << "\\n";
    else
      cout << res << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

/*
 * Problem link
 *
 */

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> V;
typedef vector<V> VV;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
template <typename T> inline bool chmin(T &a, T b) {
  bool res = a > b;
  if (res)
    a = b;
  return res;
}
typedef pair<int, int> P;
typedef vector<P> VP;
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()

int main() {
#ifdef INPUT_FROM_FILE
  ifstream cin("sample.in");
  ofstream cout("sample.out");
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  int H, N, A, B, M;
  while (1) {
    cin >> H >> N >> M >> A >> B;
    if (H + N + A + B + M == 0)
      break;
    vector<double> h(H);
    REP(i, H) cin >> h[i];
    vector<int> a(M), b(M);
    vector<double> c(M);
    REP(i, M) cin >> a[i] >> b[i] >> c[i];
    double res = 114514 + 1;
    vector<int> order(H);
    iota(ALL(order), 0);
    do {
      vector<double> dp(N + 1, res);
      dp[A] = 0;
      REP(i, H) {
        vector<double> nxt(N + 1, res);
        REP(j, M) chmin(nxt[b[j]], dp[a[j]] + c[j] / h[order[i]]);
        REP(j, M) chmin(nxt[a[j]], dp[b[j]] + c[j] / h[order[i]]);

        swap(dp, nxt);
        chmin(res, dp[B]);
      }

    } while (next_permutation(ALL(order)));

    if (res > 114514)
      cout << "Impossible"
           << "\\n";
    else
      cout << res << "\\n";
  }

  return 0;
}
Problem p00719 (C++) — buggy submission, verdict: Runtime Error

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

#define INF (1 << 29)

using namespace std;

int main(void) {

  int n, m, p, a, b, x, y, z, in;
  double dp[(1 << 8)][31], graph[31][31];
  vector<int> t;

  while (cin >> n >> m >> p >> a >> b) {
    if (!(n | m | p | a | b))
      break;

    t.clear();

    fill(graph[0], graph[31], -1);
    fill(dp[0], dp[(1 << 8)], INF);

    for (int i = 0; i < n; i++) {
      cin >> in;
      t.push_back(in);
    }

    for (int i = 0; i < p; i++) {
      cin >> x >> y >> z;
      graph[x][y] = graph[y][x] = z;
    }

    dp[0][a] = 0;
    double res = INF;
    for (int S = 0; S < (1 << 8); S++) {
      res = min(res, dp[S][b]);
      for (int v = 1; v <= m; v++) {
        for (int i = 0; i < n; i++) {
          if (!(S >> i & 1)) {
            for (int u = 1; u <= m; u++) {
              if (graph[v][u] >= 0) {
                dp[S | (1 << i)][u] =
                    min(dp[S | (1 << i)][u], dp[S][v] + graph[v][u] / t[i]);
              }
            }
          }
        }
      }
    }
    if (res == INF)
      printf("Impossible\\n");
    else
      printf("%.3f\\n", res);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

#define INF 1 << 29

using namespace std;

int main(void) {

  int n, dp[45][4], a, b;
  vector<int> p, t;

  while (cin >> n, n) {
    p.clear();
    t.clear();

    fill(dp[0], dp[42], INF);

    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      p.push_back(a);
      t.push_back(b);
    }

    dp[0][0] = p[0];
    if (p[0] > t[0]) {
      cout << "NG " << 1 << endl;
      continue;
    }
    int cnt = 1;
    for (int i = 1; i < n; i++) {
      for (int j = 1; j < 4; j++) {
        if (dp[i - 1][j - 1] == INF)
          continue;
        cnt = i;
        if (j < 3 && abs(p[i] - p[i - 1]) * (j + 1) <= t[i] - t[i - 1])
          dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + abs(p[i] - p[i - 1]));
        if (p[i] + (p[i - 1] * (j + 1)) <= t[i] - t[i - 1])
          dp[i][0] = min(dp[i][0], dp[i - 1][j - 1] + p[i] + p[i - 1]);
      }
    }

    int ans = INF;
    for (int i = 0; i < 4; i++) {
      ans = min(ans, dp[n - 1][i]);
    }
    if (ans == INF)
      cout << "NG " << cnt + 1 << endl;
    else
      cout << "OK " << ans + p[p.size() - 1] << endl;
  }

  return 0;
}