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

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

int H, W, N;
std::string field[1123];
int Sx, Sy, xy[11];
int ans;

LL d[1123456];
std::vector<edge> G[1123456];
void dijkstra(LL s) {
  std::priority_queue<P, std::vector<P>, std::greater<P>> que;
  std::fill(d, d + H * W + 1, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    LL v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(x, H) {
    rep(y, W) {
      if (field[x][y] == 'X') {
        continue;
      }
      if (field[x][y] == 'S') {
        Sx = x;
        Sy = y;
      } else if (field[x][y] != '.') {
        xy[field[x][y] - '0'] = x * W + y;
      }
      if (x < H - 1) {
        if (field[x + 1][y] != 'X') {
          G[x * W + y].push_back({(x + 1) * W + y, 1});
          G[(x + 1) * W + y].push_back({x * W + y, 1});
        }
      }
      if (y < W - 1) {
        if (field[x][y + 1] != 'X') {
          G[x * W + y].push_back({x * W + (y + 1), 1});
          G[x * W + (y + 1)].push_back({x * W + y, 1});
        }
      }
    }
  }
  dijkstra(Sx * W + Sy);
  rep(i, N) {
    ans += d[xy[i + 1]];
    dijkstra(xy[i + 1]);
  }

  out << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

LL H, W, N;
std::string field[1123];
LL Sx, Sy, xy[11];
LL ans;

LL d[1123456];
std::vector<edge> G[1123456];
void dijkstra(LL s) {
  std::priority_queue<P, std::vector<P>, std::greater<P>> que;
  std::fill(d, d + H * W + 1, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    LL v = p.second;
    if (d[v] < p.first) {
      continue;
    }
    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(x, H) {
    rep(y, W) {
      if (field[x][y] == 'X') {
        continue;
      }
      if (field[x][y] == 'S') {
        Sx = x;
        Sy = y;
      }
      if (field[x][y] != '.') {
        xy[field[x][y] - '0'] = x * W + y;
      }
      if (x < H - 1) {
        if (field[x + 1][y] != 'X') {
          G[x * W + y].push_back({(x + 1) * W + y, 1});
          G[(x + 1) * W + y].push_back({x * W + y, 1});
        }
      }
      if (y < W - 1) {
        if (field[x][y + 1] != 'X') {
          G[x * W + y].push_back({x * W + (y + 1), 1});
          G[x * W + (y + 1)].push_back({x * W + y, 1});
        }
      }
    }
  }
  dijkstra(Sx * W + Sy);
  rep(i, N) {
    ans += d[xy[i + 1]];
    dijkstra(xy[i + 1]);
  }

  out << ans << std::endl;
  return 0;
}