Problem p00897 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

int N, M, CAP;

struct Comp {
  bool operator()(pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
  }
};

// map<int, ll> dist;
// set<int> done;

// int dist[6010][2010];
// bool done[6010][2010];

// vector<vector<int> > dist;
// vector<vector<bool> > done;

int solve() {
  int idx = 0;
  map<string, int> m;

  vector<vector<int>> dist;
  vector<vector<bool>> done;

  vector<pair<int, int>> E[6010];
  set<int> gas;

  string src, dest;
  cin >> src >> dest;
  m[src] = 0;
  m[dest] = 1;
  idx = 2;

  rep(i, 0, 6010) E[i].clear();
  rep(i, 0, N) {
    string a, b;
    cin >> a >> b;
    if (m.find(a) == m.end())
      m[a] = idx, idx++;
    if (m.find(b) == m.end())
      m[b] = idx, idx++;
    int dd;
    cin >> dd;
    E[m[a]].push_back(make_pair(m[b], dd));
    E[m[b]].push_back(make_pair(m[a], dd));
  }

  gas.clear();
  rep(i, 0, M) {
    string s;
    cin >> s;
    if (m.find(s) == m.end())
      m[s] = idx, idx++;
    gas.insert(m[s]);
  }

  // dist.resize(idx, vector<int>(CAP + 1, INF));
  // done.resize(idx, vector<bool>(CAP + 1, false));

  dist.resize(idx);
  rep(i, 0, idx) dist[i] = vector<int>(CAP + 1);
  rep(i, 0, idx) done[i] = vector<bool>(CAP + 1);

  rep(i, 0, idx) rep(j, 0, CAP + 1) dist[i][j] = INF;
  rep(i, 0, idx) rep(j, 0, CAP + 1) done[i][j] = false;

  /*rep(i, 0, 6010) rep(j, 0, 2010) dist[i][j] = INF;
  rep(i, 0, 6010) rep(j, 0, 2010) done[i][j] = false;*/

  priority_queue<pair<int, int>, vector<pair<int, int>>, Comp> que;

  // dist[CAP] = 0;
  dist[0][CAP] = 0;
  que.push(make_pair(CAP, 0));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();

    int n = q.first / 10000;
    int c = q.first % 10000;

    if (n == 1) {
      return q.second;
    }

    /*if (done.find(q.first) != done.end()) continue;
    done.insert(q.first);*/

    if (done[n][c])
      continue;
    done[n][c] = true;

    for (auto p : E[n]) {
      if (c < p.second)
        continue;

      int nn = p.first;
      int nc = c - p.second;
      if (gas.find(nn) != gas.end())
        nc = CAP;
      int nnn = nn * 10000 + nc;

      int ncost = q.second + p.second;
      /*if (dist.find(nnn) == dist.end()) dist[nnn] = INF;
      if (ncost < dist[nnn])
      {
              dist[nnn] = ncost;
              que.push(make_pair(nnn, ncost));
      }*/

      if (ncost < dist[nn][nc]) {
        dist[nn][nc] = ncost;
        que.push(make_pair(nnn, ncost));
      }
    }
  }

  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> N >> M >> CAP) {
    if (N == 0)
      return 0;

    CAP *= 10;

    cout << solve() << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

int N, M, CAP;

struct Comp {
  bool operator()(pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
  }
};

// map<int, ll> dist;
// set<int> done;

// int dist[6010][2010];
// bool done[6010][2010];

// vector<vector<int> > dist;
// vector<vector<bool> > done;

int solve() {
  int idx = 0;
  map<string, int> m;

  vector<vector<int>> dist;
  vector<vector<bool>> done;

  vector<pair<int, int>> E[6010];
  set<int> gas;

  string src, dest;
  cin >> src >> dest;
  m[src] = 0;
  m[dest] = 1;
  idx = 2;

  rep(i, 0, 6010) E[i].clear();
  rep(i, 0, N) {
    string a, b;
    cin >> a >> b;
    if (m.find(a) == m.end())
      m[a] = idx, idx++;
    if (m.find(b) == m.end())
      m[b] = idx, idx++;
    int dd;
    cin >> dd;
    E[m[a]].push_back(make_pair(m[b], dd));
    E[m[b]].push_back(make_pair(m[a], dd));
  }

  gas.clear();
  rep(i, 0, M) {
    string s;
    cin >> s;
    if (m.find(s) == m.end())
      m[s] = idx, idx++;
    gas.insert(m[s]);
  }

  // dist.resize(idx, vector<int>(CAP + 1, INF));
  // done.resize(idx, vector<bool>(CAP + 1, false));

  dist.resize(idx);
  done.resize(idx);
  rep(i, 0, idx) dist[i] = vector<int>(CAP + 1);
  rep(i, 0, idx) done[i] = vector<bool>(CAP + 1);

  rep(i, 0, idx) rep(j, 0, CAP + 1) dist[i][j] = INF;
  rep(i, 0, idx) rep(j, 0, CAP + 1) done[i][j] = false;

  /*rep(i, 0, 6010) rep(j, 0, 2010) dist[i][j] = INF;
  rep(i, 0, 6010) rep(j, 0, 2010) done[i][j] = false;*/

  priority_queue<pair<int, int>, vector<pair<int, int>>, Comp> que;

  // dist[CAP] = 0;
  dist[0][CAP] = 0;
  que.push(make_pair(CAP, 0));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();

    int n = q.first / 10000;
    int c = q.first % 10000;

    if (n == 1) {
      return q.second;
    }

    /*if (done.find(q.first) != done.end()) continue;
    done.insert(q.first);*/

    if (done[n][c])
      continue;
    done[n][c] = true;

    for (auto p : E[n]) {
      if (c < p.second)
        continue;

      int nn = p.first;
      int nc = c - p.second;
      if (gas.find(nn) != gas.end())
        nc = CAP;
      int nnn = nn * 10000 + nc;

      int ncost = q.second + p.second;
      /*if (dist.find(nnn) == dist.end()) dist[nnn] = INF;
      if (ncost < dist[nnn])
      {
              dist[nnn] = ncost;
              que.push(make_pair(nnn, ncost));
      }*/

      if (ncost < dist[nn][nc]) {
        dist[nn][nc] = ncost;
        que.push(make_pair(nnn, ncost));
      }
    }
  }

  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> N >> M >> CAP) {
    if (N == 0)
      return 0;

    CAP *= 10;

    cout << solve() << endl;
  }
}