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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAX_V = 6010, MAX_CAP = 2010;

int dist[MAX_V][MAX_CAP]; // ?????? ??????????????????
using tup = tuple<int, int, int>;

int dijkstra(int start, int goal, int cap,
             const vector<vector<pair<int, int>>> &G, const set<int> &LPG) {

  for (int i = 0; i < MAX_V; ++i)
    for (int j = 0; j < MAX_CAP; ++j)
      dist[i][j] = INF;
  cap *= 10;

  dist[start][cap] = 0;
  priority_queue<tup, vector<tup>, greater<tup>>
      que; // ?????¢ ?????? ?????????LPG
  que.push(make_tuple(0, start, cap));

  while (!que.empty()) {

    int cost, u, lpg; // ?????¢ ?????? ?????????LPG
    tie(cost, u, lpg) = que.top();
    que.pop();

    if (dist[u][lpg] < cost)
      continue; // ?????????????????????????¶????

    for (const auto &tmp : G[u]) {
      int v = tmp.first, e = tmp.second;
      int nlpg = lpg - e;   // ??????v?????§?????£??????????????????LPG
      int ncost = cost + e; // ??????v?????§?????£??????????????????????????¢
      if (nlpg < 0)
        continue; // LPG????¶??????????

      if (LPG.count(v)) { // ??????v???LPG??????????????§???
        if (dist[v][cap] > ncost) {
          dist[v][cap] = ncost;
          que.push(make_tuple(ncost, v, cap));
        }
      } else {
        if (dist[v][nlpg] > ncost) {
          dist[v][nlpg] = ncost;
          que.push(make_tuple(ncost, v, nlpg));
        }
      }
    }
  }

  int ans = INF;
  for (int j = 0; j <= cap; j++)
    ans = min(ans, dist[goal][j]);
  return (ans != INF) ? ans : -1;
}

int main(void) {

  while (1) {
    int N, M, cap;
    cin >> N >> M >> cap;
    if (N == 0 && M == 0 && cap == 0)
      break;

    string src, dest;
    cin >> src >> dest;

    vector<string> c1(N), c2(N);
    vector<int> d(N);
    for (int i = 0; i < N; ++i)
      cin >> c1[i] >> c2[i] >> d[i];

    vector<string> s(M);
    for (int i = 0; i < M; ++i)
      cin >> s[i];

    set<string> city;
    city.insert(src), city.insert(dest);
    for (int i = 0; i < N; ++i)
      city.insert(c1[i]), city.insert(c2[i]);

    int cnt = 0;
    map<string, int> city2num;
    for (const auto &t : city)
      city2num[t] = cnt++;

    set<int> lpg;
    for (int i = 0; i < M; ++i)
      lpg.insert(city2num[s[i]]);
    vector<vector<pair<int, int>>> G(6010);
    for (int i = 0; i < N; ++i) {
      G[city2num[c1[i]]].push_back(make_pair(city2num[c2[i]], d[i]));
      G[city2num[c2[i]]].push_back(make_pair(city2num[c1[i]], d[i]));
    }

    cout << dijkstra(city2num[src], city2num[dest], cap, G, lpg) << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAX_V = 6010, MAX_CAP = 6010;

int dist[MAX_V][MAX_CAP]; // ?????? ??????????????????
using tup = tuple<int, int, int>;

int dijkstra(int start, int goal, int cap,
             const vector<vector<pair<int, int>>> &G, const set<int> &LPG) {

  for (int i = 0; i < MAX_V; ++i)
    for (int j = 0; j < MAX_CAP; ++j)
      dist[i][j] = INF;
  cap *= 10;

  dist[start][cap] = 0;
  priority_queue<tup, vector<tup>, greater<tup>>
      que; // ?????¢ ?????? ?????????LPG
  que.push(make_tuple(0, start, cap));

  while (!que.empty()) {

    int cost, u, lpg; // ?????¢ ?????? ?????????LPG
    tie(cost, u, lpg) = que.top();
    que.pop();

    if (dist[u][lpg] < cost)
      continue; // ?????????????????????????¶????

    for (const auto &tmp : G[u]) {
      int v = tmp.first, e = tmp.second;
      int nlpg = lpg - e;   // ??????v?????§?????£??????????????????LPG
      int ncost = cost + e; // ??????v?????§?????£??????????????????????????¢
      if (nlpg < 0)
        continue; // LPG????¶??????????

      if (LPG.count(v)) { // ??????v???LPG??????????????§???
        if (dist[v][cap] > ncost) {
          dist[v][cap] = ncost;
          que.push(make_tuple(ncost, v, cap));
        }
      } else {
        if (dist[v][nlpg] > ncost) {
          dist[v][nlpg] = ncost;
          que.push(make_tuple(ncost, v, nlpg));
        }
      }
    }
  }

  int ans = INF;
  for (int j = 1; j < MAX_V; j++)
    ans = min(ans, dist[goal][j]);
  return (ans != INF) ? ans : -1;
}

int main(void) {

  while (1) {
    int N, M, cap;
    cin >> N >> M >> cap;
    if (N == 0 && M == 0 && cap == 0)
      break;

    string src, dest;
    cin >> src >> dest;

    vector<string> c1(N), c2(N);
    vector<int> d(N);
    for (int i = 0; i < N; ++i)
      cin >> c1[i] >> c2[i] >> d[i];

    vector<string> s(M);
    for (int i = 0; i < M; ++i)
      cin >> s[i];

    set<string> st;
    st.insert(src), st.insert(dest);
    for (int i = 0; i < N; ++i)
      st.insert(c1[i]), st.insert(c2[i]);

    int cnt = 0;
    map<string, int> city2num;
    for (const auto &t : st)
      city2num[t] = cnt++;

    set<int> lpg;
    for (int i = 0; i < M; ++i)
      lpg.insert(city2num[s[i]]);
    vector<vector<pair<int, int>>> G(110);
    for (int i = 0; i < N; ++i) {
      G[city2num[c1[i]]].push_back(make_pair(city2num[c2[i]], d[i]));
      G[city2num[c2[i]]].push_back(make_pair(city2num[c1[i]], d[i]));
    }

    cout << dijkstra(city2num[src], city2num[dest], cap, G, lpg) << endl;
  }

  return 0;
}