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

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

map<string, int> mp;
const int N = 10010;
vector<P> G[N];
int lpgid[N];
const int M = 310;
int lpg[M];
int d[M][M];

void dijk(int s, int cap) {
  vector<int> dist(N, 1e9);

  priority_queue<P> q;
  q.push(MP(0, s));
  dist[s] = 0;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();

    int dd = p.first;
    int pos = p.second;
    if (dist[pos] < dd)
      continue;
    if (lpgid[pos] >= 0)
      d[lpgid[s]][lpgid[pos]] = dd;

    for (auto edge : G[pos]) {
      int nd = dd + edge.second;
      int npos = edge.first;
      if (dist[npos] <= nd)
        continue;
      dist[npos] = nd;

      if (nd <= cap)
        q.emplace(nd, npos);
    }
  }
}

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    MEMSET(lpgid, -1);
    MEMSET(lpg, -1);
    rep(i, M) rep(j, M) if (i != j) d[i][j] = 1e9;
    rep(i, N) G[i].clear();
    mp.clear();

    string src, dest;
    cin >> src >> dest;
    int num = 0;
    mp[src] = num++;
    mp[dest] = num++;
    rep(i, n) {
      string s, t;
      cin >> s >> t;
      if (!mp.count(s))
        mp[s] = num++;
      if (!mp.count(t))
        mp[t] = num++;
      int d;
      cin >> d;
      G[mp[s]].emplace_back(mp[t], d);
      G[mp[t]].emplace_back(mp[s], d);
    }
    rep(i, m) {
      string s;
      cin >> s;
      assert(mp.count(s) == 1);
      lpgid[mp[s]] = i;
      lpg[i] = mp[s];
    }
    lpgid[mp[src]] = m;
    lpg[m] = mp[src];
    lpgid[mp[dest]] = m + 1;
    lpg[m + 1] = mp[dest];

    rep(i, m + 2) dijk(lpg[i], cap * 10);

    rep(i, m + 2) rep(j, m + 2) rep(k, m + 2) d[j][k] =
        min(d[j][k], d[j][i] + d[i][k]);

    int ans = d[m][m + 1];
    if (ans >= 1e9)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

map<string, int> mp;
const int N = 3010;
vector<P> G[N];
int lpgid[N];
const int M = 310;
int lpg[M];
int d[M][M];

void dijk(int s, int cap) {
  vector<int> dist(N, 1e9);

  priority_queue<P> q;
  q.push(MP(0, s));
  dist[s] = 0;
  while (!q.empty()) {
    auto p = q.top();
    q.pop();

    int dd = p.first;
    int pos = p.second;
    if (dist[pos] < dd)
      continue;
    if (lpgid[pos] >= 0)
      d[lpgid[s]][lpgid[pos]] = dd;

    for (auto edge : G[pos]) {
      int nd = dd + edge.second;
      int npos = edge.first;
      if (dist[npos] <= nd)
        continue;
      dist[npos] = nd;

      if (nd <= cap)
        q.emplace(nd, npos);
    }
  }
}

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n) {
    MEMSET(lpgid, -1);
    MEMSET(lpg, -1);
    rep(i, M) rep(j, M) if (i != j) d[i][j] = 1e9;
    rep(i, N) G[i].clear();
    mp.clear();

    string src, dest;
    cin >> src >> dest;
    int num = 0;
    mp[src] = num++;
    mp[dest] = num++;
    rep(i, n) {
      string s, t;
      cin >> s >> t;
      if (!mp.count(s))
        mp[s] = num++;
      if (!mp.count(t))
        mp[t] = num++;
      int d;
      cin >> d;
      G[mp[s]].emplace_back(mp[t], d);
      G[mp[t]].emplace_back(mp[s], d);
    }
    rep(i, m) {
      string s;
      cin >> s;
      assert(mp.count(s) == 1);
      lpgid[mp[s]] = i;
      lpg[i] = mp[s];
    }
    lpgid[mp[src]] = m;
    lpg[m] = mp[src];
    lpgid[mp[dest]] = m + 1;
    lpg[m + 1] = mp[dest];

    rep(i, m + 2) dijk(lpg[i], cap * 10);

    rep(i, m + 2) rep(j, m + 2) rep(k, m + 2) d[j][k] =
        min(d[j][k], d[j][i] + d[i][k]);

    int ans = d[m][m + 1];
    if (ans >= 1e9)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}