alexjercan/bugnet huggingface dataset:
Problem p00212 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
using namespace std;
#define INF 1000000000
int C, N, M, S, D, g1[101][101], s, t, c, d[11][101];
typedef struct P {
  int p, c, t;
} P;
struct Order {
  bool operator()(P const &a, P const &b) const { return a.c > b.c; }
};

P tmp, tmp2;
priority_queue<P, vector<P>, Order> Q;
int main() {
  for (; scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), N;) {
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++)
        g1[i][j] = INF;
    for (int i = 0; i <= C; i++)
      for (int j = 0; j <= N; j++)
        d[i][j] = INF;
    for (int i = 0; i < M; i++) {
      scanf("%d%d%d", &s, &t, &c);
      if (g1[s][t] > c)
        g1[s][t] = g1[t][s] = c;
    }
    d[C][S] = 0;
    tmp.c = 0;
    tmp.p = S;
    tmp.t = C;
    Q.push(tmp);
    for (; !Q.empty();) {
      tmp = Q.top(), Q.pop();
      for (int i = 0; i <= N; i++) {
        int nc = tmp.c + g1[tmp.p][i];
        if (d[tmp.t][i] > nc) {
          tmp2.c = d[tmp.t][i] = nc;
          tmp2.p = i;
          tmp2.t = tmp.t;
          Q.push(tmp2);
        }
        nc = tmp.c + g1[tmp.p][i] / 2;
        if (tmp.t > 0 && d[tmp.t - 1][i] > nc) {
          tmp2.c = d[tmp.t - 1][i] = min(d[tmp.t][i], nc);
          tmp2.p = i;
          tmp2.t = tmp.t - 1;
          Q.push(tmp2);
        }
      }
    }
    printf("%d\\n", d[0][D]);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
using namespace std;
#define INF 100000000
int C, N, M, S, D, g1[101][101], g2[101][101], s, t, c, d[101], u[101];
typedef struct P {
  int p, c, t;
} P;
struct Order {
  bool operator()(P const &a, P const &b) const {
    return a.c == b.c ? a.t < b.t : a.c > b.c;
  }
};

P tmp, tmp2;
priority_queue<P, vector<P>, Order> Q;
int main() {
  for (; scanf("%d%d%d%d%d", &C, &N, &M, &S, &D), C;) {
    for (int i = 0; i <= N; i++) {
      d[i] = INF;
      for (int j = 0; j <= N; j++)
        g1[i][j] = g2[i][j] = INF;
    }
    for (int i = 0; i < N; i++) {
      scanf("%d%d%d", &s, &t, &c);
      g1[s][t] = g1[t][s] = c;
      g2[s][t] = g2[t][s] = c / 2;
    }
    d[S] = 0;
    tmp.c = 0;
    tmp.p = S;
    tmp.t = C;
    Q.push(tmp);
    for (; !Q.empty();) {
      tmp = Q.top(), Q.pop();
      for (int i = 1; i <= N; i++) {
        int nc = d[tmp.p] + g1[tmp.p][i];
        if ((d[i] == nc && u[i] > tmp.p) || d[i] > nc) {
          tmp2.c = d[i] = d[tmp.p] + g1[tmp.p][i];
          tmp2.p = i;
          tmp2.t = tmp.t;
          Q.push(tmp2);
        }
        nc = d[tmp.p] + g2[tmp.p][i];
        if (tmp.t > 0 && ((d[i] == nc && u[i] > tmp.p - 1) || d[i] > nc)) {
          tmp2.c = d[i] = d[tmp.p] + g2[tmp.p][i];
          tmp2.p = i;
          tmp2.t = tmp.t - 1;
          Q.push(tmp2);
        }
      }
    }
    printf("%d\\n", d[D]);
  }
}
Problem p00212 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
int cost[101][11];
int main() {
  int c, N, m, s, d, i, j, k, x;
  for (; scanf("%d%d%d%d%d", &c, &N, &m, &s, &d), N;) {
    vector<map<int, int>> dist(N + 1);
    memset(cost, 99, sizeof(cost));
    for (k = 0; k < m; k++) {
      scanf("%d%d%d", &i, &j, &x);
      dist[i][j] = dist[j][i] = x;
    }
    for (i = 0; i <= c; i++)
      cost[s][i] = 0;
    for (; ~c; c--) {
      vector<bool> used(N + 1);
      for (;;) { // dijkstra
        int m = 9999999;
        for (i = 1; i <= N; i++)
          if (!used[i] && m > cost[i][c])
            m = cost[i][c];
        if (m == 9999999)
          break;
        for (j = 1; j <= N; j++)
          if (m == cost[j][c]) {
            map<int, int>::iterator it = dist[j].begin();
            for (used[j] = 1; it != dist[j].end(); it++) {
              if (cost[it->first][c] > it->second + cost[j][c])
                cost[it->first][c] = it->second + cost[j][c];
              if (c && cost[it->first][c - 1] > it->second / 2 + cost[j][c])
                cost[it->first][c - 1] = it->second / 2 + cost[j][c];
            }
          }
      }
    }
    printf("%d\\n", cost[d][0]);
  }
}

Error: 0
1 3 3 1 3
1 3 2000
1 2 1000
2 3 1000
2 3 3 1 3
1 3 2300
1 2 1000
2 3 1200
2 6 6 5 1
1 2 1500
1 3 4500
2 6 2000
5 4 1000
6 4 2200
3 5 3000


Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
int cost[101][11];
int main() {
  int c, N, m, s, d, i, j, k, x;
  for (; scanf("%d%d%d%d%d", &c, &N, &m, &s, &d), N;) {
    fprintf(stderr, "%d %d %d %d %d\\n", c, N, m, s, d);
    vector<map<int, int>> dist(N + 1);
    memset(cost, 99, sizeof(cost));
    for (k = 0; k < m; k++) {
      scanf("%d%d%d", &i, &j, &x);
      dist[i][j] = dist[j][i] = x;
      fprintf(stderr, "%d %d %d\\n", i, j, x);
    }
    for (i = 0; i <= c; i++)
      cost[s][i] = 0;
    for (; ~c; c--) {
      vector<bool> used(N + 1);
      for (;;) { // dijkstra
        int m = 9999999;
        for (i = 0; i < N; i++)
          if (!used[i] && m > cost[i][c])
            m = cost[i][c];
        if (m == 9999999)
          break;
        for (j = 0; j < N; j++)
          if (m == cost[j][c]) {
            map<int, int>::iterator it = dist[j].begin();
            for (used[j] = 1; it != dist[j].end(); it++) {
              if (cost[it->first][c] > it->second + cost[j][c])
                cost[it->first][c] = it->second + cost[j][c];
              if (c && cost[it->first][c - 1] > it->second / 2 + cost[j][c])
                cost[it->first][c - 1] = it->second / 2 + cost[j][c];
            }
          }
      }
    }
    printf("%d\\n", cost[d][0]);
  }
}
Problem p00212 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

struct town {
  int n, tn[500], m[500], a[11];
};

town to[100];

int c, n, m, s, d;

void ans() {
  int i, j, k, f;
  for (i = 0; i < n; i++)
    for (j = 0; j <= c; j++)
      to[i].a[j] = 9999999;
  for (i = 0; i <= c; i++)
    to[s].a[i] = 0;
  while (true) {
    f = 0;
    for (i = 0; i < n; i++)
      for (j = 0; j < to[i].n; j++) {
        for (k = 0; k <= c; k++) {
          if (to[i].a[k] + (to[i].m[j]) / 2 < to[to[i].tn[j]].a[k + 1]) {
            to[to[i].tn[j]].a[k + 1] = to[i].a[k] + (to[i].m[j]) / 2;
            f = 1;
          }
          if (to[i].a[k] + to[i].m[j] < to[to[i].tn[j]].a[k]) {
            to[to[i].tn[j]].a[k] = to[i].a[k] + to[i].m[j];
            f = 1;
          }
        }
      }
    if (f == 0)
      break;
  }
}
int main() {
  int i, a, b, x;
  while (cin >> c >> n >> m >> s >> d && n > 0) {
    s--;
    d--;
    for (i = 0; i < n; i++)
      to[i].n = 0;
    for (i = 0; i < m; i++) {
      cin >> a >> b >> x;
      a--;
      b--;
      to[a].tn[to[a].n] = b;
      to[b].tn[to[b].n] = a;
      to[a].m[to[a].n] = to[b].m[to[b].n] = x;
      to[a].n++;
      to[b].n++;
    }
    ans();
    cout << to[d].a[c] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

struct town {
  int n, tn[500], m[500], mn, mm;
};

town to[100];

int mm, c, n, m, s, d, p[100], p2[100];

void ans(int k, int h, int g) {
  int i, a = 0;
  if (g >= mm * 2)
    return;
  if (h == d) {
    for (i = 0; i < k; i++)
      p2[i] = p[i];
    sort(p2, p2 + k, greater<int>());
    for (i = 0; i < k; i++)
      if (i < c)
        a += p2[i] / 2;
      else
        a += p2[i];
    if (a < mm)
      mm = a;
    return;
  }
  for (i = 0; i < to[h].n; i++) {
    p[k] = to[h].m[i];
    if (to[to[h].tn[i]].mn == 1)
      continue;
    to[to[h].tn[i]].mn = 1;
    ans(k + 1, to[h].tn[i], g + p[k]);
    to[to[h].tn[i]].mn = 0;
  }
}

int main() {
  int i, a, b, x;
  while (cin >> c >> n >> m >> s >> d && n > 0) {
    s--;
    d--;
    for (i = 0; i < n; i++) {
      to[i].n = 0;
      to[i].mn = 0;
    }
    for (i = 0; i < m; i++) {
      cin >> a >> b >> x;
      a--;
      b--;
      to[a].tn[to[a].n] = b;
      to[b].tn[to[b].n] = a;
      to[a].m[to[a].n] = to[b].m[to[b].n] = x;
      to[a].n++;
      to[b].n++;
    }
    mm = 99999999;
    to[s].mn = 1;
    ans(0, s, 0);
    cout << mm << endl;
  }
  return 0;
}
Problem p00212 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+8
class Data {
public:
  int to, cost, num;
  Data() {}
  Data(int to, int cost, int num) : to(to), cost(cost), num(num) {}
  bool operator<(const Data &s) const {
    if (cost != s.cost)
      return cost > s.cost;
    return to < s.to;
  }
};

int main() {
  int c, n, m, s, g;
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &g), (c | n | m | s | g)) {
    vector<Data> G[128];
    for (int i = 0; i < m; i++) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      G[a].push_back(Data(b, f, 0));
      G[b].push_back(Data(a, f, 0));
    }
    bool memo[11][101] = {{}};
    priority_queue<Data> Q;
    Q.push(Data(s, 0, c));
    int ans = -1;
    while (!Q.empty()) {
      Data e = Q.top();
      Q.pop();
      int v = e.to;
      if (memo[e.num][e.to])
        continue;
      memo[e.num][e.to] = true;
      if (v == g) {
        ans = e.cost;
        break;
      }
      for (int i = 0; i < G[v].size(); i++) {
        Data E = G[v][i];
        if (e.num != 0) {
          Q.push(Data(E.to, E.cost / 2 + e.cost, e.num - 1));
        }
        Q.push(Data(E.to, E.cost + e.cost, e.num));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1e+8
class Data {
public:
  int to, cost, num;
  Data() {}
  Data(int to, int cost, int num) : to(to), cost(cost), num(num) {}
  bool operator<(const Data &s) const {
    if (cost != s.cost)
      return cost > s.cost;
    return to < s.to;
  }
};

int main() {
  int c, n, m, s, g;
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &g), (c | n | m | s | g)) {
    vector<Data> G[128];
    for (int i = 0; i < m; i++) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      G[a].push_back(Data(b, f, 0));
      G[b].push_back(Data(a, f, 0));
    }
    priority_queue<Data> Q;
    Q.push(Data(s, 0, c));
    int ans = -1;
    while (!Q.empty()) {
      Data e = Q.top();
      Q.pop();
      int v = e.to;
      if (v == g) {
        ans = e.cost;
        break;
      }
      for (int i = 0; i < G[v].size(); i++) {
        Data E = G[v][i];
        if (e.num != 0) {
          Q.push(Data(E.to, E.cost / 2 + e.cost, e.num - 1));
        }
        Q.push(Data(E.to, E.cost + e.cost, e.num));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00215 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
struct P {
  int x, y;
  P() {}
  P(int a, int b) { x = a, y = b; }
};
int operator-(P a, P b) { return abs(a.x - b.x) + abs(a.y - b.y); }
int dp[1 << 5][5][1000];
#define INF (1 << 21)
int main() {
  int W, H;
  while (cin >> W >> H, W) {
    P s, g;
    vector<P> v[6];
    rep(i, H) rep(j, W) {
      char c;
      cin >> c;
      if (c == 'S')
        s = P(j, i);
      else if (c == 'G')
        g = P(j, i);
      else if (c != '.')
        v[c - '1'].push_back(P(j, i));
    }
    rep(i, 1 << 5) rep(k, 5) rep(j, 1000) dp[i][k][j] = INF;
    rep(i, 5) rep(j, v[(i + 1) % 5].size()) {
      dp[(1 << i) | (1 << ((i + 1) % 5))][(i + 1) % 5][j] =
          s - v[(i + 1) % 5][j];
    }
    rep(bit, (1 << 5) - 1) rep(x, 5) {
      int y = (x + 1) % 5;
      rep(i, v[x].size()) {
        if (dp[bit][x][i] == INF)
          continue;
        rep(j, v[y].size()) {
          dp[bit | (1 << y)][y][j] = min(dp[bit | (1 << y)][y][j],
                                         dp[bit][x][i] + (v[x][i] - v[y][j]));
        }
      }
    }
    int ans = INF, cho = -1;
    rep(i, 5) rep(j, v[i].size()) {
      int cst = dp[(1 << 5) - 1][i][j] + (g - v[i][j]);
      int w = (i + 1) % 5;
      if (ans >= cst) {
        if (ans == cst) {
          cho = min(cho, w);
        } else {
          cho = w;
          ans = cst;
        }
      }
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << cho + 1 << " " << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
struct P {
  int x, y;
  P() {}
  P(int a, int b) { x = a, y = b; }
};
int operator-(P a, P b) { return abs(a.x - b.x) + abs(a.y - b.y); }
#define INF (1 << 21)
int main() {
  int W, H;
  while (cin >> W >> H, W) {
    P s, g;
    vector<P> v[6];
    rep(i, H) rep(j, W) {
      char c;
      cin >> c;
      if (c == 'S')
        s = P(j, i);
      else if (c == 'G')
        g = P(j, i);
      else if (c != '.')
        v[c - '1'].push_back(P(j, i));
    }
    int dp[1 << 5][5][1000];
    rep(i, 1 << 5) rep(k, 5) rep(j, 1000) dp[i][k][j] = INF;
    rep(i, 5) rep(j, v[(i + 1) % 5].size()) {
      dp[(1 << i) | (1 << ((i + 1) % 5))][(i + 1) % 5][j] =
          s - v[(i + 1) % 5][j];
    }
    rep(bit, (1 << 5) - 1) rep(x, 5) {
      int y = (x + 1) % 5;
      rep(i, v[x].size()) rep(j, v[y].size()) {
        dp[bit | (1 << y)][y][j] =
            min(dp[bit | (1 << y)][y][j], dp[bit][x][i] + (v[x][i] - v[y][j]));
      }
    }
    int ans = INF, cho = -1;
    rep(i, 5) rep(j, v[i].size()) {
      int cst = dp[(1 << 5) - 1][i][j] + (g - v[i][j]);
      int w = (i + 1) % 5;
      if (ans >= cst) {
        if (ans == cst) {
          cho = min(cho, w);
        } else {
          cho = w;
          ans = cst;
        }
      }
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << cho + 1 << " " << ans << endl;
  }
}
Problem p00215 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define INF 1 << 28
#define F first
#define S second
using namespace std;

int sx, sy, gx, gy;
typedef pair<int, int> P;
vector<P> p[5];
int memo[5][5][1111];

int dist(int k, P a, P b) {
  if (!k)
    return abs(sx - p[b.F][b.S].F) + abs(sy - p[b.F][b.S].S);
  return abs(p[a.F][a.S].F - p[b.F][b.S].F) +
         abs(p[a.F][a.S].S - p[b.F][b.S].S);
}

int solve(int k, P q) {

  if (k == 4)
    return abs(p[q.F][q.S].F - gx) + abs(p[q.F][q.S].S - gy);
  if (memo[k][q.F][q.S])
    return memo[k][q.F][q.S];

  int ans = INF, d = (q.F + 1) % 5;
  for (int i = 0; i < p[d].size(); i++)
    ans = min(ans, solve(k + 1, P(d, i)) + dist(k, P(q.F, q.S), P(d, i)));

  return memo[k][q.F][q.S] = ans;
}

int main() {

  int w, h;
  char c;

  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;

    for (int i = 0; i < 5; i++)
      p[i].clear();

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c;
        if (c == 'S')
          sx = j, sy = i;
        if (c == 'G')
          gx = j, gy = i;
        else if (c != '.')
          p[c - '1'].push_back(P(j, i));
      }
    }

    P q = P(0, INF);
    for (int i = 0; i < 5; i++) {
      memset(memo, 0, sizeof(memo));
      if (q.S > solve(0, P(i, 0)))
        q.F = i + 1, q.S = solve(0, P(i, 0));
    }

    if (q.S == INF)
      cout << "NA\\n";
    else
      cout << q.F << " " << q.S << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define INF 1 << 28
using namespace std;

int w, h;
char c;
int sx, sy, gx, gy;
typedef pair<int, int> P;
vector<P> d[9];
map<P, map<int, int>> mp;
int memo[9][1111][9];

int cost(P now, P will, int cnt) {

  if (!cnt)
    now.F = sx, now.S = sy;
  else if (cnt == 4)
    will.F = gx, will.S = gy;

  return abs(will.F - now.F) + abs(will.S - now.S);
}

int solve(P p, int cnt) {

  if (memo[p.F][p.S][cnt])
    return memo[p.F][p.S][cnt];
  if (mp[p][cnt])
    return mp[p][cnt];
  if (cnt == 5)
    return 0;

  int ret = INF, k = (p.F + 1) % 5;
  for (int i = 0; i < d[k].size(); i++)
    ret = min(solve(P(k, i), cnt + 1) + cost(d[p.F][p.S], d[k][i], cnt), ret);

  return memo[p.F][p.S][cnt] = mp[p][cnt] = ret;
}

int main() {

  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;

    for (int i = 0; i < 9; i++)
      d[i].clear();

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> c;
        if (c == 'S')
          sx = j, sy = i;
        else if (c == 'G')
          gx = j, gy = i;
        else if (c != '.')
          d[c - '1'].push_back(P(j, i));
      }
    }

    mp.clear();
    memset(memo, 0, sizeof(memo));

    P p(INF, INF);
    for (int i = 0; i < 5; i++)
      if (p.S > solve(P(i, 0), 0))
        p.F = i + 1, p.S = solve(P(i, 0), 0);

    if (p.F == INF)
      cout << "NA" << endl;
    else
      cout << p.F << " " << p.S << endl;
  }
}
Problem p00215 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(a, b) (a = min(a, b))
using namespace std;
typedef pair<int, int> pii;

int id[1010][1010];

inline int dis(pii a, pii b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> g(h);
    rep(i, h) cin >> g[i];

    pii s, t;
    vector<vector<pii>> c(5);

    int cnt = 2;
    rep(i, h) rep(j, w) {
      if (g[i][j] == 'S') {
        s = pii(i, j);
        id[i][j] = 0;
      } else if (g[i][j] == 'G') {
        t = pii(i, j);
        id[i][j] = 1;
      } else if (g[i][j] != '.') {
        c[g[i][j] - '1'].push_back(pii(i, j));
        id[i][j] = cnt++;
      }
    }

    pii ans = pii(1e9, 1e9);
    rep(i, 5) {
      vector<vector<pii>> seq(6);
      seq[0].push_back(s);
      rep(j, 4) seq[j + 1] = c[(i + 1 + j) % 5];
      seq[5].push_back(t);

      vector<int> d(cnt, 1e8);
      d[id[s.first][s.second]] = 0;

      rep(j, 5) {
        for (pii a : seq[j]) {
          for (pii b : seq[j + 1]) {
            int cost = d[id[a.first][a.second]] + dis(a, b);
            chmin(d[id[b.first][b.second]], cost);
          }
        }
      }

      ans = min(ans, pii(d[id[t.first][t.second]], i + 1));
    }

    if (ans.first < 1e8) {
      cout << ans.second << " " << ans.first << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef pair<int, int> pii;

inline int dis(pii a, pii b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vector<string> g(h);
    rep(i, h) cin >> g[i];

    pii s, t;
    vector<vector<pii>> c(5);
    rep(i, h) rep(j, w) {
      if (g[i][j] == 'S')
        s = pii(i, j);
      else if (g[i][j] == 'G')
        t = pii(i, j);
      else if (g[i][j] != '.')
        c[g[i][j] - '1'].push_back(pii(i, j));
    }

    pii ans = pii(1e9, 1e9);
    rep(i, 5) {
      vector<vector<pii>> seq(6);
      seq[0].push_back(s);
      rep(j, 4) seq[j + 1] = c[(i + 1 + j) % 5];
      seq[5].push_back(t);

      map<pii, int> d;
      d[s] = 0;

      rep(j, 5) {
        for (pii a : seq[j]) {
          for (pii b : seq[j + 1]) {
            if (d.count(a) == 0)
              continue;

            int cost = d[a] + dis(a, b);
            if (d.count(b) == 0 || d[b] > cost)
              d[b] = cost;
          }
        }
      }

      if (d.count(t)) {
        ans = min(ans, pii(d[t], i + 1));
      }
    }

    if (ans.first < 1e9) {
      cout << ans.second << " " << ans.first << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
Problem p00215 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1 << 24;
const int N = 5;

vector<P> V[N + 2];
vector<int> T[N + 2];

int dist(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }

int calc(int st) {
  for (int i = 0; i < N + 2; ++i)
    T[i].clear();
  T[0].push_back(0);

  int bp = 0;
  for (int i = 1; i <= N; ++i) {
    int p;
    if (i == N)
      p = N + 1;
    else
      p = (st - 1 + i) % N + 1;
    for (int j = 0; j < V[p].size(); ++j) {
      int minCost = INF;
      for (int k = 0; k < V[bp].size(); ++k) {
        minCost = min(minCost, T[bp][k] + dist(V[bp][k], V[p][j]));
      }
      T[p].push_back(minCost);
    }
    bp = p;
  }
  return T[N + 1][0];
}

void solve() {
  int ans = INF;
  int ansi = -1;

  for (int i = 1; i <= N; ++i) {
    int newValue = calc(i);
    if (newValue < ans) {
      ans = newValue;
      ansi = i;
    }
  }
  if (ansi == -1)
    cout << "NA" << endl;
  else
    cout << ansi << " " << ans << endl;
}

int main() {
  int W, H;
  while (cin >> W >> H && (W || H)) {
    for (int i = 0; i < N + 2; ++i)
      V[i].clear();
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        char c;
        cin >> c;
        if (c == 'S')
          V[0].push_back(P(j, i));
        else if (c == 'G')
          V[N + 1].push_back(P(j, i));
        else if (c != '.')
          V[c - '0'].push_back(P(j, i));
      }
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1 << 24;
int W, H, N;
vector<P> V[1004];
vector<int> T[1004];

int dist(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }

int calc(int st) {
  for (int i = 0; i < N + 2; ++i)
    T[i].clear();
  T[0].push_back(0);

  int bp = 0;
  for (int i = 1; i <= N; ++i) {
    int p;
    if (i == N)
      p = N + 1;
    else
      p = (st - 1 + i) % N + 1;

    for (int j = 0; j < V[p].size(); ++j) {
      int minCost = INF;
      for (int k = 0; k < V[bp].size(); ++k) {
        minCost = min(minCost, T[bp][k] + dist(V[bp][k], V[p][j]));
      }
      T[p].push_back(minCost);
    }
    bp = p;
  }
  return T[N + 1][0];
}

void solve() {
  int ans = INF;
  int ansi = -1;
  for (int i = 1; i <= N; ++i) {
    int newCost = calc(i);

    if (newCost < ans) {
      ans = newCost;
      ansi = i;
    }
  }
  if (ansi == -1)
    cout << "NA" << endl;
  else
    cout << ansi << " " << ans << endl;
}

int main() {
  while (cin >> W >> H && (W || H)) {
    P goal;
    N = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        char c;
        cin >> c;
        if (c == 'S')
          V[0].push_back(P(j, i));
        else if (c == 'G')
          goal = P(j, i);
        else if (c != '.') {
          V[c - '0'].push_back(P(j, i));
          N = max(N, c - '0');
        }
      }
    }
    V[N + 1].push_back(goal);
    solve();
  }
  return 0;
}