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

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool operator<(const Edge &e, const Edge &f) { return e.cost > f.cost; }

vector<int> dijkstra(const Graph &g, int s) {
  vector<int> d(g.size(), INF);
  d[s] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s)); // P(dis, pos)
  while (!que.empty()) {
    int dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist)
      continue;
    for (const auto &e : g[v]) {
      if (d[e.to] <= d[v] + e.cost)
        continue;
      d[e.to] = d[v] + e.cost;
      que.push(P(d[e.to], e.to));
    }
  }
  return d;
}

struct Road {
  string from, to;
  int d;
  Road(string from, string to, int d) : from(from), to(to), d(d) {}
};

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

  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    string src, dest;
    cin >> src >> dest;
    cap *= 10;
    map<string, int> mp;
    mp[src] = 1;
    mp[dest] = 2;
    vector<vector<Edge>> g(6666);
    vector<Road> roads;
    for (int i = 0; i < n; i++) {
      string st, gt;
      cin >> st >> gt;
      int d;
      cin >> d;
      roads.push_back(Road(st, gt, d));
    }
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    int idx = 3;
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      if (mp[s] != 0)
        continue;
      mp[s] = idx;
      v.push_back(idx);
      idx++;
    }
    for (int i = 0; i < n; i++) {
      string st = roads[i].from, gt = roads[i].to;
      int d = roads[i].d;
      if (mp[st] == 0) {
        mp[st] = idx;
        idx++;
      }
      if (mp[gt] == 0) {
        mp[gt] = idx;
        idx++;
      }
      g[mp[st]].push_back(Edge(mp[gt], d));
      g[mp[gt]].push_back(Edge(mp[st], d));
    }
    int sz = v.size();
    vector<vector<int>> dis(sz + 1, vector<int>(sz + 1, INF));
    for (int i = 1; i <= sz; i++)
      dis[i][i] = 0;
    for (int i = 1; i <= sz; i++) {
      vector<int> tmp = dijkstra(g, i);
      for (int j = i + 1; j <= sz; j++) {
        if (tmp[j] > cap)
          continue;
        dis[i][j] = dis[j][i] = tmp[j];
      }
    }

    for (int k = 1; k <= sz; k++) {
      for (int i = 1; i <= sz; i++) {
        for (int j = 1; j <= sz; j++) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    if (dis[1][2] >= INF)
      cout << -1 << endl;
    else
      cout << dis[1][2] << endl;
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int MOD = 1e9 + 7;

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

  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    string src, dest;
    cin >> src >> dest;
    cap *= 10;
    int idx = 3;
    map<string, int> mp;
    mp[src] = 1;
    mp[dest] = 2;
    vector<vector<int>> g(1000, vector<int>(1000, INF));
    for (int i = 0; i < n; i++) {
      string st, gt;
      cin >> st >> gt;
      int d;
      cin >> d;
      if (mp[st] == 0) {
        mp[st] = idx;
        idx++;
      }
      if (mp[gt] == 0) {
        mp[gt] = idx;
        idx++;
      }
      g[mp[st]][mp[gt]] = d;
      g[mp[gt]][mp[st]] = d;
    }
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      v.push_back(mp[s]);
    }
    for (int i = 1; i < idx; i++)
      g[i][i] = 0;
    for (int k = 1; k < idx; k++) {
      for (int i = 1; i < idx; i++) {
        for (int j = 1; j < idx; j++) {
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
        }
      }
    }
    vector<vector<int>> dis(idx, vector<int>(idx, INF));
    int sz = v.size();
    for (int i = 0; i < sz; i++) {
      for (int j = i; j < sz; j++) {
        int st = v[i], gt = v[j];
        if (g[st][gt] > cap)
          continue;
        dis[st][gt] = dis[gt][st] = g[st][gt];
      }
    }
    for (int k = 1; k < idx; k++) {
      for (int i = 1; i < idx; i++) {
        for (int j = 1; j < idx; j++) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    if (dis[1][2] >= INF)
      cout << -1 << endl;
    else
      cout << dis[1][2] << endl;
  }
}