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

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

using namespace std;

#define INF 1 << 16

typedef complex<int> P;

int abs(int n) { return n < 0 ? -n : n; }

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    vector<P> v;
    vector<int> type;
    vector<vector<int>> m(7);

    for (int y = 0; y < H; ++y) {
      string str;
      cin >> str;
      for (int x = 0; x < W; ++x) {
        if (str[x] == '.')
          continue;

        v.push_back(P(x, y));
        if (str[x] == 'S')
          type.push_back(5);
        else if (str[x] == 'G')
          type.push_back(6);
        else if (str[x] != '.')
          type.push_back(str[x] - '1');

        m[type[type.size() - 1]].push_back(v.size() - 1);
      }
    }

    int count = 0, no;
    for (int i = 0; i < 5; ++i)
      if (!m[i].size())
        ++count, no = i;

    queue<int> que;
    if (count >= 2) {
      cout << "NA" << endl;
      continue;
    } else if (count == 1)
      que.push(no);
    else
      for (int i = 0; i < 5; ++i)
        que.push(i);

    pair<int, int> ans = pair<int, int>(0, INF);
    while (!que.empty()) {
      int kind = que.front();
      que.pop();
      vector<int> len(v.size(), INF);

      vector<int> types;
      types.push_back(5);
      for (int i = 0; i < 4; ++i)
        types.push_back((kind + i + 1) % 5);

      types.push_back(6);

      len[m[5][0]] = 0;
      for (int i = 0; i < 5; ++i) {
        int now = types[i], next = types[i + 1];
        for (int j = 0; j < m[next].size(); ++j)
          for (int k = 0; k < m[now].size(); ++k)
            if (len[m[next][j]] >
                len[m[now][k]] +
                    abs(v[m[now][k]].real() - v[m[next][j]].real()) +
                    abs(v[m[now][k]].imag() - v[m[next][j]].imag()))
              len[m[next][j]] =
                  len[m[now][k]] +
                  abs(v[m[now][k]].real() - v[m[next][j]].real()) +
                  abs(v[m[now][k]].imag() - v[m[next][j]].imag());
      }

      if (len[m[6][0]] < ans.second)
        ans = pair<int, int>(kind + 1, len[m[6][0]]);
    }

    cout << ans.first << " " << ans.second << endl;
  }
}

Error: 0

Fixed submission:

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

using namespace std;

#define INF 1 << 16

typedef complex<int> P;

int abs(int n) { return n < 0 ? -n : n; }

int main() {
  int W, H;
  while (cin >> W >> H, W || H) {
    vector<P> v;
    vector<int> type;
    vector<vector<int>> m(7);

    for (int y = 0; y < H; ++y) {
      string str;
      cin >> str;
      for (int x = 0; x < W; ++x) {
        if (str[x] == '.')
          continue;

        v.push_back(P(x, y));
        if (str[x] == 'S')
          type.push_back(5);
        else if (str[x] == 'G')
          type.push_back(6);
        else if (str[x] != '.')
          type.push_back(str[x] - '1');

        m[type[type.size() - 1]].push_back(v.size() - 1);
      }
    }

    int f = false;
    for (int i = 0; i < 5; ++i)
      if (!m[i].size())
        f = true;

    if (f) {
      cout << "NA" << endl;
      continue;
    }

    pair<int, int> ans = pair<int, int>(0, INF);
    for (int kind = 0; kind < 5; ++kind) {
      vector<int> len(v.size(), INF);

      vector<int> types;
      types.push_back(5);
      for (int i = 0; i < 4; ++i)
        types.push_back((kind + i + 1) % 5);

      types.push_back(6);

      len[m[5][0]] = 0;
      for (int i = 0; i < 6; ++i) {
        int now = types[i], next = types[i + 1];
        for (int j = 0; j < m[next].size(); ++j)
          for (int k = 0; k < m[now].size(); ++k)
            if (len[m[next][j]] >
                len[m[now][k]] +
                    abs(v[m[now][k]].real() - v[m[next][j]].real()) +
                    abs(v[m[now][k]].imag() - v[m[next][j]].imag()))
              len[m[next][j]] =
                  len[m[now][k]] +
                  abs(v[m[now][k]].real() - v[m[next][j]].real()) +
                  abs(v[m[now][k]].imag() - v[m[next][j]].imag());
      }

      if (len[m[6][0]] < ans.second)
        ans = pair<int, int>(kind + 1, len[m[6][0]]);
    }

    cout << ans.first << " " << ans.second << endl;
  }
}