Problem p00897 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

class Edge {
public:
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost){};
};

vector<int> dijkstra(vector<vector<Edge>> &G, int s) {
  int N = G.size();
  vector<int> d(N, INF);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  d[s] = 0;
  q.push(make_pair(d[s], s));

  while (!q.empty()) {
    pair<int, int> v = q.top();
    q.pop();
    if (v.first != d[v.second])
      continue;

    for (Edge &e : G[v.second]) {
      if (e.cost + d[v.second] < d[e.to]) {
        d[e.to] = e.cost + d[v.second];
        q.push(make_pair(d[e.to], e.to));
      }
    }
  }
  return d;
}

int N, M, C;

void solve() {
  string src, dst;
  cin >> src >> dst;

  map<string, int> stoi;
  vector<string> cs(N), cd(N);
  vector<int> ds(N);
  for (int i = 0; i < N; i++) {
    cin >> cs[i] >> cd[i] >> ds[i];

    if (stoi.find(cs[i]) == stoi.end())
      stoi[cs[i]] = stoi.size();
    if (stoi.find(cd[i]) == stoi.end())
      stoi[cd[i]] = stoi.size();
  }

  vector<int> gass(M);
  for (int i = 0; i < M; i++) {
    string s;
    cin >> s;
    gass[i] = stoi[s];
  }
  gass.push_back(stoi[src]);
  gass.push_back(stoi[dst]);
  M = gass.size();

  vector<vector<Edge>> G(stoi.size());
  for (int i = 0; i < N; i++) {
    int si = stoi[cs[i]];
    int di = stoi[cd[i]];

    G[si].emplace_back(di, ds[i]);
    G[di].emplace_back(si, ds[i]);
  }

  vector<vector<int>> mat(M, vector<int>(M, INF));
  for (int i = 0; i < M; i++)
    mat[i][i] = 0;

  for (int i = 0; i < M; i++) {
    int gs = gass[i];
    auto d = dijkstra(G, gs);
    for (int j = 0; j < M; j++) {
      int gd = gass[j];
      if (d[gd] <= C * 10) {
        mat[i][j] = d[gd];
        mat[j][i] = d[gd];
      }
    }
  }

  for (int k = 0; k < M; k++)
    for (int i = 0; i < M; i++)
      for (int j = 0; j < M; j++)
        mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);

  cout << (mat[M - 2][M - 1] == INF ? -1 : mat[M - 2][M - 1]) << endl;
}

int main() {
  while (cin >> N >> M >> C, N | M | C)
    solve();

  return 0;
}

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

const int INF = 0x3f3f3f3f;

class Edge {
public:
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost){};
};

vector<int> dijkstra(vector<vector<Edge>> &G, int s) {
  int N = G.size();
  vector<int> d(N, INF);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  d[s] = 0;
  q.push(make_pair(d[s], s));

  while (!q.empty()) {
    pair<int, int> v = q.top();
    q.pop();
    if (v.first != d[v.second])
      continue;

    for (Edge &e : G[v.second]) {
      if (e.cost + d[v.second] < d[e.to]) {
        d[e.to] = e.cost + d[v.second];
        q.push(make_pair(d[e.to], e.to));
      }
    }
  }
  return d;
}

int N, M, C;

void solve() {
  string src, dst;
  cin >> src >> dst;

  map<string, int> stoi;
  vector<string> cs(N), cd(N);
  vector<int> ds(N);
  for (int i = 0; i < N; i++) {
    cin >> cs[i] >> cd[i] >> ds[i];

    if (stoi.find(cs[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cs[i]] = x;
    }
    if (stoi.find(cd[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cd[i]] = x;
    }
  }

  vector<int> gass(M);
  for (int i = 0; i < M; i++) {
    string s;
    cin >> s;
    gass[i] = stoi[s];
  }
  gass.push_back(stoi[src]);
  gass.push_back(stoi[dst]);
  M = gass.size();

  vector<vector<Edge>> G(stoi.size());
  for (int i = 0; i < N; i++) {
    int si = stoi[cs[i]];
    int di = stoi[cd[i]];

    G[si].emplace_back(di, ds[i]);
    G[di].emplace_back(si, ds[i]);
  }

  vector<vector<int>> mat(M, vector<int>(M, INF));
  for (int i = 0; i < M; i++)
    mat[i][i] = 0;

  for (int i = 0; i < M; i++) {
    int gs = gass[i];
    auto d = dijkstra(G, gs);
    for (int j = 0; j < M; j++) {
      int gd = gass[j];
      if (d[gd] <= C * 10) {
        mat[i][j] = d[gd];
        mat[j][i] = d[gd];
      }
    }
  }

  for (int k = 0; k < M; k++)
    for (int i = 0; i < M; i++)
      for (int j = 0; j < M; j++)
        mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);

  cout << (mat[M - 2][M - 1] == INF ? -1 : mat[M - 2][M - 1]) << endl;
}

int main() {
  while (cin >> N >> M >> C, N | M | C)
    solve();

  return 0;
}