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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;

bool visit[6001][2001];
vector<pii> edge[6001];
vector<int> mst;
struct node {
  int id, cap, dist;
  node(int i, int c, int d) {
    id = i;
    cap = c;
    dist = d;
  }
};

bool operator<(const node &lhs, const node &rhs) { return lhs.dist > rhs.dist; }

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n and m and cap) {
    memset(visit, 0, sizeof(visit));
    mst.clear();
    cap *= 10;
    map<string, int> mp;
    rep(i, 2 * n + 1) edge[i].clear();
    string start, goal;
    cin >> start >> goal;
    int k = 0;
    rep(i, n) {
      string a, b;
      int d;
      cin >> a >> b >> d;
      if (mp.find(a) == mp.end())
        mp[a] = k++;
      if (mp.find(b) == mp.end())
        mp[b] = k++;
      edge[mp[a]].push_back(pii(mp[b], d));
      edge[mp[b]].push_back(pii(mp[a], d));
    }
    rep(i, m) {
      string s;
      cin >> s;
      mst.push_back(mp[s]);
    }
    sort(mst.begin(), mst.end());
    priority_queue<node> que;
    que.push(node(mp[start], cap, 0));
    bool found = false;
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (visit[nd.id][nd.cap])
        continue;
      visit[nd.id][nd.cap] = true;
      if (nd.id == mp[goal]) {
        cout << nd.dist << endl;
        found = true;
        break;
      }
      if (binary_search(mst.begin(), mst.end(), nd.id) and !visit[nd.id][cap]) {
        que.push(node(nd.id, cap, nd.dist));
      }
      rep(i, edge[nd.id].size()) {
        pii p = edge[nd.id][i];
        if (nd.cap >= p.second && !visit[p.first][nd.cap - p.second]) {
          que.push(node(p.first, nd.cap - p.second, nd.dist + p.second));
        }
      }
    }
    if (!found) {
      cout << -1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, n) for (int i = s; i < n; i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

bool visit[3001][2001];
vector<pii> conn[3001];
bool station[3001];

struct node {
  int p, gas;
  ll cost;
  node(int pp, ll cc, int gg) {
    p = pp;
    cost = cc;
    gas = gg;
  }
};

bool operator<(const node &lhs, const node &rhs) { return lhs.cost > rhs.cost; }

int main() {
  int n, m, c;
  while (cin >> n >> m >> c, n) {
    c *= 10;
    memset(visit, 0, sizeof(visit));
    memset(station, 0, sizeof(station));
    rep(i, 3001) conn[i].clear();
    map<string, int> mp;
    int towns = 0;
    string start, goal;
    cin >> start >> goal;
    mp[start] = towns++;
    if (start != goal) {
      mp[goal] = towns++;
    }
    rep(i, n) {
      string s, e;
      int d;
      cin >> s >> e >> d;
      if (mp.find(s) == mp.end()) {
        mp[s] = towns++;
      }
      if (mp.find(e) == mp.end()) {
        mp[e] = towns++;
      }
      conn[mp[s]].push_back(pii(mp[e], d));
      conn[mp[e]].push_back(pii(mp[s], d));
    }
    rep(i, m) {
      string in;
      cin >> in;
      station[mp[in]] = true;
    }
    priority_queue<node> que;
    que.push(node(mp[start], 0, c));
    bool found = false;
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (nd.p == mp[goal]) {
        cout << nd.cost << endl;
        found = true;
        break;
      }
      if (visit[nd.p][nd.gas])
        continue;
      visit[nd.p][nd.gas] = true;

      if (station[nd.p] && !visit[nd.p][c]) {
        que.push(node(nd.p, nd.cost, c));
      }

      for (int i = 0; i < conn[nd.p].size(); i++) {
        if (nd.gas - conn[nd.p][i].second >= 0 &&
            !visit[conn[nd.p][i].first][nd.gas - conn[nd.p][i].second]) {
          que.push(node(conn[nd.p][i].first, nd.cost + conn[nd.p][i].second,
                        nd.gas - conn[nd.p][i].second));
        }
      }
    }
    if (!found)
      cout << -1 << endl;
  }
  return 0;
}