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

// e
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; ++i)
#define REPR(i, b, n) for (int i = n - 1; i >= b; --i)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PI acos(-1.0)

#define LIM 1000010

using namespace std;

static const double EPS = 1e-9;
typedef pair<int, int> pii;
typedef long long ll;

int h, w;
string str;

int main() {

  while (cin >> w >> h, (h || w)) {

    vector<vector<pii>> pt(2560);
    vector<vector<int>> dist(2560);
    int glx, gly, mx = 0;

    REP(j, 0, h) {
      REP(i, 0, w) {

        cin >> str;

        if (str[0] == 'S') {
          pt[0].push_back(make_pair(i, j));
          dist[0].push_back(0);
        } else if (str[0] == 'G') {
          glx = i;
          gly = j;
        } else if ('0' <= str[0] && str[0] <= '9') {

          int tmp = 0;

          REP(k, 0, str.size()) {
            tmp *= 10;
            tmp += (int)(str[k] - '0');
          }

          pt[tmp].push_back(make_pair(i, j));
          dist[tmp].push_back(0);
          mx = max(mx, tmp);
        }
      }
    }

    pt[mx + 1].push_back(make_pair(glx, gly));
    dist[mx + 1].push_back(0);

    /*
    for(int i=0; i<=mx; ++i){
            for(int j=0; j<pt[i].size(); ++j){
                    cout << pt[i][j].first << "," << pt[i][j].second << " " ;
            }

            cout << endl;
    }
    */

    REPR(k, 0, mx + 1) {
      REP(i, 0, pt[k].size()) {

        int tmp = INT_MAX;

        REP(j, 0, pt[k + 1].size()) {
          tmp = min(tmp, dist[k + 1][j] +
                             abs(pt[k + 1][j].first - pt[k][i].first) +
                             abs(pt[k + 1][j].second - pt[k][i].second));
        }

        dist[k][i] = tmp;
      }
    }

    cout << dist[0][0] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// e
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i, b, n) for (int i = b; i < n; ++i)
#define REPR(i, b, n) for (int i = n - 1; i >= b; --i)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define BIT(n, b) ((n >> b) & 1)
#define PI acos(-1.0)

#define LIM 1000010

using namespace std;

static const double EPS = 1e-9;
typedef pair<int, int> pii;
typedef long long ll;

int h, w;
string str;

int main() {

  while (cin >> h >> w, (h || w)) {

    vector<vector<pii>> pt(500);
    vector<vector<int>> dist(500);
    int glx, gly, mx = 0;

    REP(j, 0, h) {
      REP(i, 0, w) {

        cin >> str;

        if (str[0] == 'S') {
          pt[0].push_back(make_pair(i, j));
          dist[0].push_back(0);
        } else if (str[0] == 'G') {
          glx = i;
          gly = j;
        } else if ('0' <= str[0] && str[0] <= '9') {

          int tmp = 0;

          REP(k, 0, str.size()) {
            tmp *= 10;
            tmp += (int)(str[k] - '0');
          }

          pt[tmp].push_back(make_pair(i, j));
          dist[tmp].push_back(0);
          mx = max(mx, tmp);
        }
      }
    }

    pt[mx + 1].push_back(make_pair(glx, gly));
    dist[mx + 1].push_back(0);

    REPR(k, 0, mx + 1) {
      REP(i, 0, pt[k].size()) {

        int tmp = INT_MAX;

        REP(j, 0, pt[k + 1].size()) {
          tmp = min(tmp, dist[k + 1][j] +
                             abs(pt[k + 1][j].first - pt[k][i].first) +
                             abs(pt[k + 1][j].second - pt[k][i].second));
        }

        dist[k][i] = tmp;
      }
    }

    cout << dist[0][0] << endl;
  }

  return 0;
}