Problem p00897 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF (1e9)
#define N 3005
#define M 2005
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;

int n, m, cap, d[N][M], n_max;
map<string, int> num;
string src, dest;
vector<P> G[N];
set<int> lpc;

int dijkstra(int s, int g) {
  priority_queue<P1, vector<P1>, greater<P1>> q;
  for (int i = 0; i < n_max; i++)
    for (int j = 0; j <= cap * 10; j++)
      d[i][j] = INF;
  q.push(P1(0, P(s, cap * 10)));
  d[s][cap * 10] = 0;
  while (!q.empty()) {
    P1 t = q.top();
    q.pop();
    int cost = t.first;
    int node = t.second.first;
    int c = t.second.second;
    if (d[node][c] < cost)
      continue;
    if (node == g)
      return cost;
    for (int i = 0; i < G[node].size(); i++) {
      int ncost = cost + G[node][i].second;
      int nnode = G[node][i].first;
      int nc = c - G[node][i].second;
      if (nc < 0)
        continue;
      if (d[nnode][nc] > ncost) {
        d[nnode][nc] = ncost;
        q.push(P1(ncost, P(nnode, nc)));
      }
      if (lpc.count(nnode)) {
        nc = cap * 10;
        if (d[nnode][nc] > ncost) {
          d[nnode][nc] = ncost;
          q.push(P1(ncost, P(nnode, nc)));
        }
      }
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> cap;
    if (!n && !m && !cap)
      break;
    cin >> src >> dest;
    string a, b;
    int c;
    n_max = 0;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      if (!num.count(a))
        num[a] = n_max++;
      if (!num.count(b))
        num[b] = n_max++;
      G[num[a]].push_back(P(num[b], c));
      G[num[b]].push_back(P(num[a], c));
    }
    if (!num.count(src))
      num[src] = n_max++;
    if (!num.count(dest))
      num[dest] = n_max++;
    for (int i = 0; i < m; i++) {
      cin >> a;
      lpc.insert(num[a]);
    }
    int s = num[src], g = num[dest];
    cout << dijkstra(s, g) << endl;
    num.clear();
    lpc.clear();
    for (int i = 0; i < n_max; i++)
      G[i].clear();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF (1e9)
#define N 6005
#define M 2005
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;

int n, m, cap, d[N][M], n_max;
map<string, int> num;
string src, dest;
vector<P> G[N];
set<int> lpc;

int dijkstra(int s, int g) {
  priority_queue<P1, vector<P1>, greater<P1>> q;
  for (int i = 0; i < n_max; i++)
    for (int j = 0; j <= cap * 10; j++)
      d[i][j] = INF;
  q.push(P1(0, P(s, cap * 10)));
  d[s][cap * 10] = 0;
  while (!q.empty()) {
    P1 t = q.top();
    q.pop();
    int cost = t.first;
    int node = t.second.first;
    int c = t.second.second;
    if (d[node][c] < cost)
      continue;
    if (node == g)
      return cost;
    for (int i = 0; i < G[node].size(); i++) {
      int ncost = cost + G[node][i].second;
      int nnode = G[node][i].first;
      int nc = c - G[node][i].second;
      if (nc < 0)
        continue;
      if (d[nnode][nc] > ncost) {
        d[nnode][nc] = ncost;
        q.push(P1(ncost, P(nnode, nc)));
      }
      if (lpc.count(nnode)) {
        nc = cap * 10;
        if (d[nnode][nc] > ncost) {
          d[nnode][nc] = ncost;
          q.push(P1(ncost, P(nnode, nc)));
        }
      }
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m >> cap;
    if (!n && !m && !cap)
      break;
    cin >> src >> dest;
    string a, b;
    int c;
    n_max = 0;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c;
      if (!num.count(a))
        num[a] = n_max++;
      if (!num.count(b))
        num[b] = n_max++;
      G[num[a]].push_back(P(num[b], c));
      G[num[b]].push_back(P(num[a], c));
    }
    if (!num.count(src))
      num[src] = n_max++;
    if (!num.count(dest))
      num[dest] = n_max++;
    for (int i = 0; i < m; i++) {
      cin >> a;
      lpc.insert(num[a]);
    }
    int s = num[src], g = num[dest];
    cout << dijkstra(s, g) << endl;
    num.clear();
    lpc.clear();
    for (int i = 0; i < n_max; i++)
      G[i].clear();
  }
  return 0;
}