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

#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MINF(a) memset(a, 0x3f, sizeof(a))
#define POW(n) (1LL << (n))
#define IN(i, a, b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline void SORT(T &a) { sort(ALL(a)); }
template <typename T> inline void REV(T &a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T &a) {
  sort(ALL(a));
  a.erase(unique(ALL(a)), a.end());
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ----------------------------------------------------------------------------------------------------
 */

template <typename T>
vector<int> dijkstra(const vector<vector<pair<int, T>>> &G, int s, T inf) {
  vector<int> d(G.size(), inf);
  d[s] = 0;
  using P = pair<T, int>;
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(0, s);
  while (!que.empty()) {
    T dd;
    int v;
    tie(dd, v) = que.top();
    que.pop();
    if (d[v] < dd)
      continue;
    for (auto e : G[v]) {
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.emplace(d[e.first], e.first);
      }
    }
  }
  return d;
}

int N, M, cap;
string src, dst;
string c[2][3010];
int d[3010];
string s[310];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  while (cin >> N >> M >> cap, N) {
    vector<string> vs;
    cin >> src >> dst;
    vs.push_back(src);
    vs.push_back(dst);
    REP(i, N) {
      cin >> c[0][i] >> c[1][i] >> d[i];
      vs.push_back(c[0][i]);
      vs.push_back(c[1][i]);
    }
    vector<string> LPG;
    LPG.push_back(src);
    LPG.push_back(dst);
    REP(i, M) {
      cin >> s[i];
      vs.push_back(s[i]);
      LPG.push_back(s[i]);
    }
    UNI(LPG);
    UNI(vs);
    map<string, int> inv_vs, inv_LPG;
    vector<vector<pii>> G(vs.size());
    REP(i, vs.size()) inv_vs[vs[i]] = i;
    REP(i, LPG.size()) inv_LPG[LPG[i]] = i;
    REP(i, N) {
      int v = inv_vs[c[0][i]];
      int u = inv_vs[c[1][i]];
      G[v].emplace_back(u, d[i]);
      G[u].emplace_back(v, d[i]);
    }
    int dis[310][310];
    MINF(dis);
    REP(i, LPG.size()) {
      int v = inv_vs[LPG[i]];
      vector<int> dd = dijkstra<int>(G, v, INF);
      REP(j, LPG.size()) if (dd[inv_vs[LPG[j]]] <= cap * 10) {
        dis[i][j] = dis[j][i] = dd[inv_vs[LPG[j]]];
      }
    }
    REP(k, LPG.size()) REP(i, LPG.size()) REP(j, LPG.size()) {
      CHMIN(dis[i][j], dis[i][k] + dis[k][j]);
    }
    int ans = dis[inv_LPG[src]][inv_LPG[dst]];
    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MINF(a) memset(a, 0x3f, sizeof(a))
#define POW(n) (1LL << (n))
#define IN(i, a, b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline void SORT(T &a) { sort(ALL(a)); }
template <typename T> inline void REV(T &a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T &a) {
  sort(ALL(a));
  a.erase(unique(ALL(a)), a.end());
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ----------------------------------------------------------------------------------------------------
 */

template <typename T>
vector<int> dijkstra(const vector<vector<pair<int, T>>> &G, int s, T inf) {
  vector<int> d(G.size(), inf);
  d[s] = 0;
  using P = pair<T, int>;
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(0, s);
  while (!que.empty()) {
    T dd;
    int v;
    tie(dd, v) = que.top();
    que.pop();
    if (d[v] < dd)
      continue;
    for (auto e : G[v]) {
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.emplace(d[e.first], e.first);
      }
    }
  }
  return d;
}

int N, M, cap;
string src, dst;
string c[2][3010];
int d[3010];
string s[310];
int dis[310][310];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  while (cin >> N >> M >> cap, N) {
    cin >> src >> dst;
    map<string, int> mp;
    mp[src] = mp.size();
    mp[dst] = mp.size();
    REP(i, N) {
      cin >> c[0][i] >> c[1][i] >> d[i];
      if (mp.count(c[0][i]) == 0)
        mp[c[0][i]] = mp.size();
      if (mp.count(c[1][i]) == 0)
        mp[c[1][i]] = mp.size();
    }
    REP(i, M) {
      cin >> s[i];
      if (mp.count(s[i]) == 0)
        mp[s[i]] = mp.size();
    }
    vector<vector<pii>> G(3010);
    REP(i, N) {
      int u = mp[c[0][i]];
      int v = mp[c[1][i]];
      G[u].emplace_back(v, d[i]);
      G[v].emplace_back(u, d[i]);
    }
    map<int, int> cnv;
    vector<string> inv_cnv(310);
    REP(i, M) {
      inv_cnv[cnv.size()] = s[i];
      cnv[mp[s[i]]] = cnv.size() - 1;
    }
    if (cnv.count(mp[src]) == 0) {
      inv_cnv[cnv.size()] = src;
      cnv[mp[src]] = cnv.size() - 1;
    }
    if (cnv.count(mp[dst]) == 0) {
      inv_cnv[cnv.size()] = dst;
      cnv[mp[dst]] = cnv.size() - 1;
    }

    MINF(dis);
    int sz = G.size();
    REP(i, cnv.size()) {
      vector<int> dd = dijkstra<int>(G, mp[inv_cnv[i]], INF);
      for (int j = 0; j < sz; j++) {
        if (cnv.count(j) && dd[j] <= cap * 10) {
          dis[i][cnv[j]] = dis[cnv[j]][i] = dd[j];
        }
      }
    }
    REP(k, cnv.size()) REP(i, cnv.size()) REP(j, cnv.size()) {
      CHMIN(dis[i][j], dis[i][k] + dis[k][j]);
    }
    int ans = dis[cnv[mp[src]]][cnv[mp[dst]]];
    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}