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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
bool ISINT(double x) { return fabs(x - (int)x) < EPS; }
bool ISEQ(double x, double y) { return fabs(x - y) < EPS; }
string itos(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define EREP(i, a, b) for (int i = a; i <= b; i++)
#define erep(i, n) EREP(i, 0, n)
#define foreach(itr, c)                                                        \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)

typedef pair<int, int> P;
typedef pair<int, P> State;

int w, h;
int dp[2502][2502];
vector<P> t[2502];

int main(void) {
  while (cin >> w >> h, w || h) {
    rep(i, 2502) t[i].clear();

    int gx, gy;
    int maxDig = 0;
    rep(i, h) {
      rep(j, w) {
        string s;
        cin >> s;
        if (s == "S") {
          t[0].push_back(P(j, i));
        } else if (s == "G") {
          gx = j;
          gy = i;
        } else if (s != ".") {
          int dig = atoi(s.c_str());
          maxDig = max(maxDig, dig);
          t[dig].push_back(P(j, i));
        }
      }
    }
    t[maxDig + 1].push_back(P(gx, gy));

    rep(i, maxDig + 2) rep(j, t[i].size()) dp[i][j] = INT_MAX;
    dp[0][0] = 0;

    rep(i, maxDig + 1) {
      rep(j, t[i].size()) {
        rep(k, t[i + 1].size()) {
          int dist = abs(t[i + 1][k].first - t[i][j].first) +
                     abs(t[i + 1][k].second - t[i][j].second);
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dist);
        }
      }
    }

    cout << dp[maxDig + 1][0] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
bool ISINT(double x) { return fabs(x - (int)x) < EPS; }
bool ISEQ(double x, double y) { return fabs(x - y) < EPS; }
string itos(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define EREP(i, a, b) for (int i = a; i <= b; i++)
#define erep(i, n) EREP(i, 0, n)
#define foreach(itr, c)                                                        \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); itr++)

typedef pair<int, int> P;
typedef pair<int, P> State;

int w, h;
int dp[102][102];
vector<P> t[102];

int main(void) {
  while (cin >> w >> h, w || h) {
    rep(i, 102) t[i].clear();

    int gx, gy;
    int maxDig = 0;
    rep(i, h) {
      rep(j, w) {
        string s;
        cin >> s;
        if (s == "S") {
          t[0].push_back(P(j, i));
        } else if (s == "G") {
          gx = j;
          gy = i;
        } else if (s != ".") {
          int dig = atoi(s.c_str());
          maxDig = max(maxDig, dig);
          t[dig].push_back(P(j, i));
        }
      }
    }
    t[maxDig + 1].push_back(P(gx, gy));

    rep(i, 102) rep(j, 102) dp[i][j] = INT_MAX;
    dp[0][0] = 0;

    rep(i, maxDig + 1) {
      rep(j, t[i].size()) {
        rep(k, t[i + 1].size()) {
          int dist = abs(t[i + 1][k].first - t[i][j].first) +
                     abs(t[i + 1][k].second - t[i][j].second);
          dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dist);
        }
      }
    }

    cout << dp[maxDig + 1][0] << endl;
  }
}