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

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> vec;
  for (int i = 0; i < h; i++) {
    string t;
    cin >> t;
    vec.emplace_back(t);
  }
  set<pair<int, int>> se;
  for (int i = 1; i < h - 1; i++) {
    for (int j = 1; j < w - 1; j++) {
      if (vec[i][j] == '.')
        continue;
      int cnt = 0;
      for (int k = -1; k <= 1; k++) {
        for (int l = -1; l <= 1; l++) {
          if (i == 0 && j == 0)
            continue;
          if (vec[i + k][j + l] == '.') {
            cnt++;
          }
        }
      }
      if (cnt >= vec[i][j] - '0') {
        se.insert(make_pair(i, j));
      }
    }
  }
  for (auto i : se) {
    vec[i.first][i.second] = '.';
  }
  int ans = 0;
  for (;; ans++) {
    if (se.empty())
      break;
    set<pair<int, int>> temp;
    for (auto i : se) {
      for (int j = -1; j <= 1; j++) {
        for (int k = -1; k <= 1; k++) {
          if (j == 0 && k == 0)
            continue;
          int x = i.first + j, y = i.second + k;
          if (vec[x][y] == '.')
            continue;
          int cnt = 0;
          for (int l = -1; l <= 1; l++) {
            for (int m = -1; m <= 1; m++) {
              if (l == 0 && m == 0)
                continue;
              if (vec[x + l][y + m] == '.') {
                cnt++;
              }
            }
          }
          if (cnt >= vec[x][y] - '0') {
            temp.insert(make_pair(x, y));
          }
        }
      }
    }
    se = temp;
    for (auto i : se) {
      vec[i.first][i.second] = '.';
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);

constexpr int MAX_V = 10010;
int n, k;
int C[10010], R[10010];
struct edge {
  LL to, cost;
};

typedef pair<LL, LL> PP;

LL d[10010]; //?????¢
vector<edge> vec[10010];

int last = 0;

LL dijkstra() {
  for (int i = 0; i < 10010; i++)
    d[i] = LINF;
  priority_queue<PP, std::vector<PP>, std::greater<PP>> pq;
  d[0] = 0;
  pq.push(make_pair(d[0], 0)); // cost,to
  while (!pq.empty()) {
    PP p = pq.top();
    pq.pop();
    int i = p.second;
    if (d[i] < p.first)
      continue;
    for (int k = 0; k < vec[i].size(); k++) {
      edge e = vec[i][k];
      if (d[e.to] <= d[i] + e.cost)
        continue;
      if (e.to == n - 1)
        last = e.cost;
      d[e.to] = d[i] + e.cost;
      pq.push(make_pair(d[e.to], e.to));
    }
  }
  return d[n - 1];
}

vector<int> G[MAX_V];
int start, goal;

int dis[MAX_V]; //???????????¢????±?????????´???

void bfs() {
  queue<int> que;
  //?????????
  for (int i = 0; i < MAX_V; i++)
    dis[i] = INF;

  que.push(start);
  dis[start] = 0;

  while (que.size()) {
    int p = que.front();
    que.pop();

    // if (p == goal)break;
    for (int i = 0; i < G[p].size(); i++) {
      if (dis[G[p][i]] == INF) {
        que.push(G[p][i]);
        dis[G[p][i]] = dis[p] + 1;
      }
    }
  }
  REP(i, n) {
    if (dis[i] <= R[start]) {
      if (i == start)
        continue;
      // cout << start+1 << " " << i+1 << endl;
      // vec[i].emplace_back(edge{ start,C[start] });
      vec[start].emplace_back(edge{i, C[start]});
    }
  }
}

int main() {
  cin >> n >> k;
  int a, b, c;

  REP(i, n) { cin >> C[i] >> R[i]; }

  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    a--, b--;
    // edgedata[a].emplace_back(edge{ b,c });
    // edgedata[b].emplace_back(edge{ a,c });
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    // vec[a].push_back(edge{ b,c });
    // vec[b].push_back(edge{ a,c });
  }
  REP(j, n) {
    start = j;
    bfs();
  }
  cout << dijkstra() << endl;
  // system("pause");
}