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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to;
  int cost;
};

int d[2001][6011];
vector<edge> G[6011];
bool isGas[10001];
map<string, int> m;
int idx;
int st, gl;

int N, M, cap;
const int INF = 1 << 30;
typedef pair<int, int> pii;
typedef pair<int, pii> Sit;

int dijkstra() {
  for (int i = 0; i < 2001; i++)
    for (int j = 0; j < 6011; j++)
      d[i][j] = INF;
  priority_queue<Sit, vector<Sit>, greater<Sit>> pq;
  pq.push(make_pair(0, pii(cap * 10, st)));
  d[cap * 10][st] = 0;
  while (pq.size()) {
    Sit p = pq.top();
    pq.pop();
    int ccost = p.first;
    int ccap = p.second.first;
    int cnode = p.second.second;
    if (d[ccap][cnode] < ccost)
      continue;
    for (int i = 0; i < (int)G[cnode].size(); i++) {
      edge &e = G[cnode][i];
      int ncap = ccap - e.cost;
      if (ncap < 0)
        continue;
      int nnode = e.to;
      int ncost = ccost + e.cost;
      if (isGas[nnode])
        ncap = cap * 10;
      if (d[ncap][nnode] > ncost) {
        d[ncap][nnode] = ncost;
        pq.push(make_pair(ncost, pii(ncap, nnode)));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 2001; i++)
    res = min(res, d[i][gl]);
  return res;
}

int main() {
  while (cin >> N >> M >> cap && (N | M | cap)) {
    memset(isGas, 0, sizeof(isGas));
    for (int i = 0; i < 6011; i++)
      G[i].clear();
    m.clear();
    idx = 0;
    string s, t;
    cin >> s >> t;
    if (m.count(s) == 0)
      m[s] = idx++;
    if (m.count(t) == 0)
      m[t] = idx++;
    st = m[s];
    gl = m[t];
    for (int i = 0; i < N; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (m.count(a) == 0)
        m[a] = idx++;
      if (m.count(b) == 0)
        m[b] = idx++;
      int x, y;
      x = m[a];
      y = m[b];
      edge e;
      e.to = y;
      e.cost = c;
      G[x].push_back(e);
      e.to = x;
      G[y].push_back(e);
    }
    for (int i = 0; i < M; i++) {
      string s;
      cin >> s;
      if (m.count(s) == 0)
        m[s] = idx++;
      isGas[m[s]] = true;
    }
    int res = dijkstra();
    if (res == INF)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to;
  int cost;
};

int d[2001][3001];
vector<edge> G[3001];
bool isGas[10001];
map<string, int> m;
int idx;
int st, gl;

int N, M, cap;
const int INF = 1 << 30;
typedef pair<int, int> pii;
typedef pair<int, pii> Sit;

int dijkstra() {
  for (int i = 0; i < 2001; i++)
    for (int j = 0; j < 3001; j++)
      d[i][j] = INF;
  priority_queue<Sit, vector<Sit>, greater<Sit>> pq;
  pq.push(make_pair(0, pii(cap * 10, st)));
  d[cap * 10][st] = 0;
  while (pq.size()) {
    Sit p = pq.top();
    pq.pop();
    int ccost = p.first;
    int ccap = p.second.first;
    int cnode = p.second.second;
    if (d[ccap][cnode] < ccost)
      continue;
    for (int i = 0; i < (int)G[cnode].size(); i++) {
      edge &e = G[cnode][i];
      int ncap = ccap - e.cost;
      if (ncap < 0)
        continue;
      int nnode = e.to;
      int ncost = ccost + e.cost;
      if (isGas[nnode])
        ncap = cap * 10;
      if (d[ncap][nnode] > ncost) {
        d[ncap][nnode] = ncost;
        pq.push(make_pair(ncost, pii(ncap, nnode)));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < 2001; i++)
    res = min(res, d[i][gl]);
  return res;
}

int main() {
  while (cin >> N >> M >> cap && (N | M | cap)) {
    memset(isGas, 0, sizeof(isGas));
    for (int i = 0; i < 3001; i++)
      G[i].clear();
    idx = 0;
    string s, t;
    cin >> s >> t;
    if (m.count(s) == 0)
      m[s] = idx++;
    if (m.count(t) == 0)
      m[t] = idx++;
    st = m[s];
    gl = m[t];
    for (int i = 0; i < N; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (m.count(a) == 0)
        m[a] = idx++;
      if (m.count(b) == 0)
        m[b] = idx++;
      int x, y;
      x = m[a];
      y = m[b];
      edge e;
      e.to = y;
      e.cost = c;
      G[x].push_back(e);
      e.to = x;
      G[y].push_back(e);
    }
    for (int i = 0; i < M; i++) {
      string s;
      cin >> s;
      if (m.count(s) == 0)
        m[s] = idx++;
      isGas[m[s]] = true;
    }
    int res = dijkstra();
    if (res == INF)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}