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

#include <algorithm>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
#define INF (1 << 28)
inline void cmin(int &a, int b) {
  if (a > b)
    a = b;
}

typedef pair<int, int> P;
int diff(const P &a, const P &b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int w, h, f[128][128];
char buf[128];

int main() {
  for (;;) {
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      return 0;
    memset(f, 0, sizeof(f));
    P g;
    rep(i, h) rep(j, w) {
      scanf("%s", buf);
      if (buf[0] == 'S')
        f[i][j] = 1;
      else if (buf[0] == 'G')
        g = mp(i, j);
      else if (isdigit(buf[0]))
        f[i][j] = atoi(buf) + 1;
    }
    int m = 0;
    rep(i, h) rep(j, w) m = max(m, f[i][j]);
    vector<vector<P>> loc(m);
    vector<vector<int>> dp(m);
    rep(i, h) rep(j, w) if (f[i][j]) {
      loc[f[i][j] - 1].push_back(mp(i, j));
      dp[f[i][j] - 1].push_back(INF);
    }
    dp[0][0] = 0;
    rep(k, m - 1) rep(i, dp[k].size()) rep(j, dp[k + 1].size()) {
      cmin(dp[k + 1][j], dp[k][i] + diff(loc[k + 1][j], loc[k][i]));
    }
    int ans = INF;
    rep(i, dp[m - 1].size()) cmin(ans, dp[m - 1][i] + diff(loc[m - 1][i], g));
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair
#define INF (1 << 28)
inline void cmin(int &a, int b) {
  if (a > b)
    a = b;
}

typedef pair<int, int> P;
int diff(const P &a, const P &b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int w, h, f[128][128];
char buf[128];

int main() {
  for (;;) {
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      return 0;
    memset(f, 0, sizeof(f));
    P g;
    rep(i, h) rep(j, h) {
      scanf("%s", buf);
      if (buf[0] == 'S')
        f[i][j] = 1;
      else if (buf[0] == 'G')
        g = mp(i, j);
      else if (isdigit(buf[0]))
        f[i][j] = atoi(buf) + 1;
    }
    int m = 0;
    rep(i, h) rep(j, h) m = max(m, f[i][j]);
    vector<vector<P>> loc(m);
    vector<vector<int>> dp(m);
    rep(i, h) rep(j, h) if (f[i][j]) {
      loc[f[i][j] - 1].push_back(mp(i, j));
      dp[f[i][j] - 1].push_back(INF);
    }
    dp[0][0] = 0;
    rep(k, m - 1) rep(i, dp[k].size()) rep(j, dp[k + 1].size()) {
      cmin(dp[k + 1][j], dp[k][i] + diff(loc[k + 1][j], loc[k][i]));
    }
    int ans = INF;
    rep(i, dp[m - 1].size()) cmin(ans, dp[m - 1][i] + diff(loc[m - 1][i], g));
    printf("%d\\n", ans);
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> x[2502], y[2502];
int d[2502][2502];
int p;

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    rep(i, 2502) {
      x[i].clear();
      y[i].clear();
    }
    p = 1; // s->0,g->p
    int gx, gy;
    rep(i, h) {
      rep(j, w) {
        string s;
        cin >> s;
        if (s == ".")
          ;
        else if (s == "S")
          x[0].push_back(i), y[0].push_back(j);
        else if (s == "G")
          gx = i, gy = j;
        else {
          int d = stoi(s);
          p = max(p, d + 1);
          x[d].push_back(i);
          y[d].push_back(j);
        }
      }
    }
    x[p].push_back(gx);
    y[p].push_back(gy);
    memset(d, 0x3f, sizeof(d));
    d[0][0] = 0;
    for (int i = 1; i <= p; i++) {
      rep(j, x[i].size()) {
        rep(k, x[i - 1].size()) {
          d[i][j] = min(d[i][j], d[i - 1][k] + abs(x[i - 1][k] - x[i][j]) +
                                     abs(y[i - 1][k] - y[i][j]));
        }
      }
    }
    printf("%d\\n", d[p][0]);
  }
}

Error: 0

Fixed submission:

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

vector<int> x[2502], y[2502];
int d[2502][2502];
int p;

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    rep(i, 10002) {
      x[i].clear();
      y[i].clear();
    }
    p = 0; // s->0,g->p
    int gx, gy;
    rep(i, h) {
      rep(j, w) {
        string s;
        cin >> s;
        if (s == ".")
          ;
        else if (s == "S")
          x[0].push_back(i), y[0].push_back(j);
        else if (s == "G")
          gx = i, gy = j;
        else {
          int d = stoi(s);
          p = max(p, d + 1);
          x[d].push_back(i);
          y[d].push_back(j);
        }
      }
    }
    x[p].push_back(gx);
    y[p].push_back(gy);
    memset(d, 0x3f, sizeof(d));
    d[0][0] = 0;
    for (int i = 1; i <= p; i++) {
      rep(j, x[i].size()) {
        rep(k, x[i - 1].size()) {
          d[i][j] = min(d[i][j], d[i - 1][k] + abs(x[i - 1][k] - x[i][j]) +
                                     abs(y[i - 1][k] - y[i][j]));
        }
      }
    }
    printf("%d\\n", d[p][0]);
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

int parse(string a) {
  int b = 0;
  for (char c : a)
    b = b * 10 + c - 48;
  return b;
}
int main() {
  int a, b;
  while (cin >> b >> a, a | b) {
    p G;
    vector<P> d[20000];
    int MAX = 0;
    for (int e = 0; e < a; e++) {
      for (int g = 0; g < b; g++) {
        string o;
        cin >> o;
        if (o == "S")
          d[0].push_back(P(p(e, g), 0));
        else if (o == "G")
          G = p(e, g);
        else if (o != ".") {
          d[parse(o)].push_back(P(p(e, g), 0));
          MAX = max(MAX, parse(o));
        }
      }
    }
    for (int i = 1; i <= MAX; i++) {
      for (int j = 0; j < d[i].size(); j++) {
        int MIN = 1 << 29;
        for (int k = 0; k < d[i - 1].size(); k++) {
          MIN = min(MIN,
                    d[i - 1][k].second +
                        abs(d[i][j].first.first - d[i - 1][k].first.first) +
                        abs(d[i][j].first.second - d[i - 1][k].first.second));
        }
        d[i][j].second = MIN;
      }
    }
    int MINCOST = 1 << 29;
    for (P s : d[MAX]) {
      MINCOST = min(MINCOST, s.second + abs(G.first - s.first.first) +
                                 abs(G.second - s.first.second));
    }
    cout << MINCOST << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define p pair<int, int>
#define P pair<p, int>
using namespace std;

int parse(string a) {
  int b = 0;
  for (char c : a)
    b = b * 10 + c - 48;
  return b;
}
int main() {
  int a, b;
  while (cin >> b >> a, a | b) {
    p G;
    vector<P> d[102];
    int MAX = 0;
    for (int e = 0; e < a; e++) {
      for (int g = 0; g < b; g++) {
        string o;
        cin >> o;
        if (o == "S")
          d[0].push_back(P(p(e, g), 0));
        else if (o == "G")
          G = p(e, g);
        else if (o != ".") {
          d[parse(o)].push_back(P(p(e, g), 0));
          MAX = max(MAX, parse(o));
        }
      }
    }
    for (int i = 1; i <= MAX; i++) {
      for (int j = 0; j < d[i].size(); j++) {
        int MIN = 1 << 29;
        for (int k = 0; k < d[i - 1].size(); k++) {
          MIN = min(MIN,
                    d[i - 1][k].second +
                        abs(d[i][j].first.first - d[i - 1][k].first.first) +
                        abs(d[i][j].first.second - d[i - 1][k].first.second));
        }
        d[i][j].second = MIN;
      }
    }
    int MINCOST = 1 << 29;
    for (P s : d[MAX]) {
      MINCOST = min(MINCOST, s.second + abs(G.first - s.first.first) +
                                 abs(G.second - s.first.second));
    }
    cout << MINCOST << endl;
  }
}
Problem p01401 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
vector<P> v[100001];
vector<int> dp[10001];
int get(string x) {
  int res = 0;
  r(i, x.size()) res = res * 10 + (x[i] - '0');
  return res;
}
int dist(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }
string s;
int e, w, h;
int main() {
  while (cin >> w >> h, h) {
    r(i, 100001) v[i].clear();
    r(i, 10001) dp[i].clear();
    P t;
    r(i, h) r(j, w) {
      cin >> s;
      if (s == "G")
        t = P(i, j);
      if (s == "S")
        v[0].push_back(P(i, j));
      if (isdigit(s[0])) {
        int x = get(s);
        v[x].push_back(P(i, j));
        dp[x].push_back(1e9);
      }
    }
    r(i, 99999) if (v[i].size() == 0) {
      v[i].push_back(t);
      dp[i].push_back(1e9);
      e = i;
      break;
    }
    dp[0].push_back(0);
    r(i, e) r(j, v[i].size()) r(k, v[i + 1].size()) dp[i + 1][k] =
        min(dp[i + 1][k], dp[i][j] + dist(v[i][j], v[i + 1][k]));
    cout << dp[e][0] << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
vector<P> v[100001];
int get(string x) {
  int res = 0;
  r(i, x.size()) res = res * 10 + (x[i] - '0');
  return res;
}
int dist(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }
string s;
int e, dp[10001][2001], w, h;
int main() {
  while (cin >> h >> w, h) {
    r(i, 10001) r(j, 2001) dp[i][j] = 1e9;
    r(i, 100001) v[i].clear();
    P t;
    r(i, h) r(j, w) {
      cin >> s;
      if (s == "G")
        t = P(i, j);
      if (s == "S")
        v[0].push_back(P(i, j));
      if (isdigit(s[0])) {
        int x = get(s);
        v[x].push_back(P(i, j));
      }
    }
    r(i, 99999) if (v[i].size() == 0) {
      v[i].push_back(t);
      e = i;
      break;
    }
    dp[0][0] = 0;
    r(i, e) r(j, v[i].size()) r(k, v[i + 1].size()) dp[i + 1][k] =
        min(dp[i + 1][k], dp[i][j] + dist(v[i][j], v[i + 1][k]));
    cout << dp[e][0] << endl;
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 29;

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

int dp[2][10000];
int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<vector<P>> num(10000);
    P s, g;
    int max_el = 0;
    rep(i, h) rep(j, w) {
      string in;
      cin >> in;

      if (in == ".")
        continue;
      if (in == "S") {
        s = MP(j, i);
        continue;
      }
      if (in == "G") {
        g = MP(j, i);
        continue;
      }
      int x = atoi(in.c_str());
      num[x].push_back(MP(j, i));
      max_el = max(max_el, x);
    }
    num[0].push_back(s);
    num[max_el + 1].push_back(g);
    int *cur = dp[0];
    int *next = dp[1];
    memset(dp[0], 0, sizeof(dp[0]));
    rep(i, max_el + 1) {
      rep(k, num[i + 1].size()) next[k] = INF;
      rep(j, num[i].size()) {
        rep(k, num[i + 1].size()) {
          next[k] = min(next[k], cur[j] + dist(num[i][j], num[i + 1][k]));
        }
      }
      swap(cur, next);
    }
    cout << cur[0] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 29;

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

int dp[2][10000];
int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    vector<vector<P>> num;
    P s, g;
    rep(i, h) rep(j, w) {
      string in;
      cin >> in;

      if (in == ".")
        continue;
      if (in == "S") {
        s = MP(j, i);
        continue;
      }
      if (in == "G") {
        g = MP(j, i);
        continue;
      }
      int x = atoi(in.c_str());
      if (x >= num.size()) {
        num.resize(x + 1);
      }
      num[x].push_back(MP(j, i));
    }
    num[0].push_back(s);
    num.resize(num.size() + 1);
    num[num.size() - 1].push_back(g);
    int *cur = dp[0];
    int *next = dp[1];
    memset(dp[0], 0, sizeof(dp[0]));
    rep(i, num.size() - 1) {
      rep(k, num[i + 1].size()) next[k] = INF;
      rep(j, num[i].size()) {
        rep(k, num[i + 1].size()) {
          next[k] = min(next[k], cur[j] + dist(num[i][j], num[i + 1][k]));
        }
      }
      swap(cur, next);
    }
    cout << cur[0] << endl;
  }

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

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int dp[2505][2505];

int todig(string a) {
  stringstream ss(a);
  int res;
  ss >> res;

  return res;
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    int gx, gy, n = 0;

    vector<vector<pair<int, int>>> pos(W * H);
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        string s;
        cin >> s;
        if (s == "S")
          pos[0].push_back(make_pair(j, i));
        if (s == "G") {
          gx = j;
          gy = i;
        };
        if (isdigit(s[0])) {
          int d = todig(s);
          n = max(n, d);
          pos[d].push_back(make_pair(j, i));
        }
      }

    pos[n + 1].push_back(make_pair(gx, gy));
    ++n;

    for (int i = 1; i <= n; i++) {
      for (int k = 0; k < pos[i].size(); k++)
        dp[k][i] = (1 << 28);

      for (int j = 0; j < pos[i - 1].size(); j++)
        for (int k = 0; k < pos[i].size(); k++) {
          dp[k][i] =
              min(dp[k][i], dp[j][i - 1] +
                                abs(pos[i - 1][j].first - pos[i][k].first) +
                                abs(pos[i - 1][j].second - pos[i][k].second));
        }
    }

    cout << dp[0][n] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int dp[100][100];

int todig(string a) {
  stringstream ss(a);
  int res;
  ss >> res;

  return res;
}

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    int gx, gy, n = 0;

    vector<vector<pair<int, int>>> pos(100);
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        string s;
        cin >> s;
        if (s == "S")
          pos[0].push_back(make_pair(j, i));
        if (s == "G") {
          gx = j;
          gy = i;
        };
        if (isdigit(s[0])) {
          int d = todig(s);
          n = max(n, d);
          pos[d].push_back(make_pair(j, i));
        }
      }

    pos[n + 1].push_back(make_pair(gx, gy));
    ++n;

    for (int i = 1; i <= n; i++) {
      for (int k = 0; k < pos[i].size(); k++)
        dp[k][i] = (1 << 28);

      for (int j = 0; j < pos[i - 1].size(); j++)
        for (int k = 0; k < pos[i].size(); k++) {
          dp[k][i] =
              min(dp[k][i], dp[j][i - 1] +
                                abs(pos[i - 1][j].first - pos[i][k].first) +
                                abs(pos[i - 1][j].second - pos[i][k].second));
        }
    }

    cout << dp[0][n] << endl;
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;
typedef vector<int> vi;

string s;
int perseInt(const string &s) {
  int ret = 0;
  rep(i, s.size()) ret = ret * 10 + (s[i] - '0');
  return ret;
}

struct P {
  int x, y;
  P(){};
  P(int x, int y) : x(x), y(y){};
  int dist(const P &p) { return abs(x - p.x) + abs(y - p.y); }
};
vector<P> ps[2600];

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {
    int m = 0;
    P g;
    rep(i, 2600) ps[i].clear();
    rep(i, h) rep(j, w) {
      cin >> s;
      if (s == ".")
        continue;
      else if (s == "S")
        ps[0].pb({i, j});
      else if (s == "G")
        g = {i, j};
      else {
        int k = perseInt(s);
        ps[k].pb({i, j});
        m = max(m, k);
      }
    }
    ps[++m].pb(g);
    vector<vi> d(2600, vi(2600, 1e9));
    d[0][0] = 0;
    rep(i, m) {
      rep(j, ps[i].size()) {
        rep(k, ps[i + 1].size()) {
          d[i + 1][k] = min(d[i + 1][k], d[i][j] + ps[i][j].dist(ps[i + 1][k]));
        }
      }
    }
    cout << d[m][0] << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second
#define pb push_back
#define mp nake_pair

typedef long long ll;
typedef vector<int> vi;

string s;
int perseInt(const string &s) {
  int ret = 0;
  rep(i, s.size()) ret = ret * 10 + (s[i] - '0');
  return ret;
}

struct P {
  int x, y;
  P(){};
  P(int x, int y) : x(x), y(y){};
  int dist(const P &p) { return abs(x - p.x) + abs(y - p.y); }
};
vector<P> ps[2600];

int main() {
  int w, h;
  while (cin >> w >> h && w && h) {
    int m = 0;
    P g;
    rep(i, 10010) ps[i].clear();
    rep(i, h) rep(j, w) {
      cin >> s;
      if (s == ".")
        continue;
      else if (s == "S")
        ps[0].pb({i, j});
      else if (s == "G")
        g = {i, j};
      else {
        int k = perseInt(s);
        ps[k].pb({i, j});
        m = max(m, k);
      }
    }
    ps[++m].pb(g);
    vector<vi> d(2600, vi(2600, 1e9));
    d[0][0] = 0;
    rep(i, m) {
      rep(j, ps[i].size()) {
        rep(k, ps[i + 1].size()) {
          d[i + 1][k] = min(d[i + 1][k], d[i][j] + ps[i][j].dist(ps[i + 1][k]));
        }
      }
    }
    cout << d[m][0] << endl;
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int F(P a, P b) { return abs(a.first - b.first) + abs(a.second - b.second); }
int main() {
  int w, h, i, j, k, n, c;
  char s[9];
  vector<P> p[2502];
  vector<int> d[2502];
  P g;
  while (scanf("%d%d", &w, &h), w) {
    for (n = i = 0; i < 2502; ++i)
      p[i].clear();
    for (i = 0; i < h; ++i)
      for (j = 0; j < w; ++j) {
        scanf("%s", s);
        if (*s == 'S')
          p[0].push_back(P(j, i));
        else if (*s == 'G')
          g = P(j, i);
        else if (*s != '.') {
          c = atoi(s);
          if (n < c)
            n = c;
          p[c].push_back(P(j, i));
        }
      }
    p[n + 1].push_back(g);
    n += 2;
    for (i = 0; i < n; ++i) {
      d[i].resize(c = p[i].size());
      for (j = 0; j < c; ++j)
        d[i][j] = (1 << 30);
    }
    d[0][0] = 0;
    for (i = 1; i < n; ++i)
      for (j = 0; j < d[i - 1].size(); ++j)
        for (k = 0; k < d[i].size(); ++k)
          d[i][k] = min(d[i][k], d[i - 1][j] + F(p[i - 1][j], p[i][k]));
    printf("%d\\n", d[n - 1][0]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
using namespace std;
#define dist(p1, p2) (abs(p1.first - p2.first) + abs(p1.second - p2.second))
typedef pair<int, int> P;
char s[9];
vector<P> p[2502];
vector<int> d[2502];
int main() {
  int w, h, gx, gy, n, t = 0, c;
  while (scanf("%d %d", &w, &h), w || h) {
    for (int i = 0; i < 2502; i++)
      p[i].clear();
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        scanf("%s", s);
        if (*s == 'S')
          p[0].push_back(P(j, i));
        else if (*s == 'G')
          gx = j, gy = i;
        else if (*s != '.') {
          p[n = atoi(s)].push_back(P(j, i));
          if (t < n)
            t = n;
        }
      }
    p[t + 1].push_back(P(gx, gy));
    t += 2;
    for (int i = 0; i < t; i++) {
      d[i].resize(c = p[i].size());
      for (int j = 0; j < c; j++)
        d[i][j] = (1 << 30);
    }
    d[0][0] = 0;
    for (int i = 1; i < t; i++)
      for (int j = 0; j < d[i - 1].size(); j++)
        for (int k = 0; k < d[i].size(); k++)
          d[i][k] = min(d[i][k], d[i - 1][j] + dist(p[i - 1][j], p[i][k]));
    printf("%d\\n", d[t - 1][0]);
  }
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> x[2500];
vector<int> dist[2500];
string S;
int h, w;
int main() {
  while (true) {
    cin >> h >> w;
    int gx, gy, cnt = 0;
    if (h == 0)
      break;
    for (int i = 0; i < 2500; i++) {
      x[i].clear();
      dist[i].clear();
    }
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        cin >> S;
        if (S == "S") {
          x[0].push_back(make_pair(i, j));
          dist[0].push_back(0);
        } else if (S == "G") {
          gx = i;
          gy = j;
        } else if (S != ".") {
          int v = stoi(S);
          x[v].push_back(make_pair(i, j));
          cnt = max(cnt, v);
          dist[v].push_back(1 << 30);
        }
      }
    }
    cnt++;
    x[cnt].push_back(make_pair(gx, gy));
    dist[cnt].push_back(1 << 30);
    for (int i = 1; i <= cnt; i++) {
      for (int j = 0; j < dist[i - 1].size(); j++) {
        for (int k = 0; k < dist[i].size(); k++) {
          int D = abs(x[i - 1][j].first - x[i][k].first);
          D += abs(x[i - 1][j].second - x[i][k].second);
          dist[i][k] = min(dist[i][k], dist[i - 1][j] + D);
        }
      }
    }
    cout << dist[cnt][0] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> x[2500];
vector<int> dist[2500];
string S;
int h, w;
int main() {
  while (true) {
    cin >> h >> w;
    int gx, gy, cnt = 0;
    if (h == 0)
      break;
    for (int i = 0; i < 2500; i++) {
      x[i].clear();
      dist[i].clear();
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> S;
        if (S == "S") {
          x[0].push_back(make_pair(i, j));
          dist[0].push_back(0);
        } else if (S == "G") {
          gx = i;
          gy = j;
        } else if (S != ".") {
          int v = stoi(S);
          x[v].push_back(make_pair(i, j));
          cnt = max(cnt, v);
          dist[v].push_back(1 << 30);
        }
      }
    }
    cnt++;
    x[cnt].push_back(make_pair(gx, gy));
    for (int i = 1; i <= cnt; i++) {
      for (int j = 0; j < dist[i - 1].size(); j++) {
        for (int k = 0; k < dist[i].size(); k++) {
          int D = abs(x[i - 1][j].first - x[i][k].first);
          D += abs(x[i - 1][j].second + x[i][k].second);
          dist[i][k] = min(dist[i][k], dist[i - 1][j] + D);
        }
      }
    }
    cout << dist[cnt][0] << endl;
  }
  return 0;
}