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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
const int max_n = 6001;

int s_n, m;
int c;
map<string, int> mp;
int n = 2;
int to_id(const string &s) {
  if (mp[s])
    return mp[s];
  else {
    n++;
    mp[s] = n;
    return n;
  }
}
struct edge {
  int to, cost;
};
vector<edge> G[max_n];
vector<edge> trueG[max_n];
int d[max_n];
void init(int s) {
  rep1(i, n) { d[i] = mod; }
  d[s] = 0;
}
bool needed[max_n];

priority_queue<P, vector<P>, greater<P>> q;
void dijkstra(int sta) {
  init(sta);
  q.push({0, sta});
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    rep(j, G[v].size()) {
      int to = G[v][j].to;
      if (p.first + G[v][j].cost < d[to]) {
        d[to] = p.first + G[v][j].cost;
        q.push({d[to], to});
      }
    }
  }
  rep1(i, n) {
    if (i == sta)
      continue;
    if (!needed[i])
      continue;
    if (d[i] <= 10 * c) {
      trueG[sta].push_back({i, d[i]});
    }
  }
}
void all_init() {
  rep1(i, n) {
    G[i].clear();
    trueG[i].clear();
    needed[i] = false;
  }
  mp.clear();
  n = 2;
}
int main() {
  while (cin >> m >> s_n >> c, m) {
    all_init();
    string in1, in2;
    cin >> in1 >> in2;
    mp[in1] = 1;
    mp[in2] = 2;
    rep(i, m) {
      int z;
      cin >> in1 >> in2 >> z;
      int i1 = to_id(in1), i2 = to_id(in2);
      G[i1].push_back({i2, z});
      G[i2].push_back({i1, z});
    }
    needed[1] = needed[2] = true;
    rep(i, s_n) {
      cin >> in1;
      needed[to_id(in1)] = true;
    }
    rep1(i, n) if (needed[i]) dijkstra(i);
    init(1);
    q.push({0, 1});
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      rep(j, trueG[v].size()) {
        int to = trueG[v][j].to;
        int cost = p.first + trueG[v][j].cost;
        if (cost < d[to]) {
          d[to] = cost;
          q.push({d[to], to});
        }
      }
    }
    if (d[2] == mod)
      cout << -1 << endl;
    else
      cout << d[2] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int s_n, m;
int c;
map<string, int> mp;
int n = 2;
int to_id(const string &s) {
  if (mp[s])
    return mp[s];
  else {
    n++;
    mp[s] = n;
    return n;
  }
}
struct edge {
  int to, cost;
};
vector<edge> G[3001];
vector<edge> trueG[3001];
int d[3001];
void init(int s) {
  rep1(i, n) { d[i] = mod; }
  d[s] = 0;
}
bool needed[3001];

priority_queue<P, vector<P>, greater<P>> q;
void dijkstra(int sta) {
  init(sta);
  q.push({0, sta});
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    rep(j, G[v].size()) {
      int to = G[v][j].to;
      if (p.first + G[v][j].cost < d[to]) {
        d[to] = p.first + G[v][j].cost;
        q.push({d[to], to});
      }
    }
  }
  rep1(i, n) {
    if (i == sta)
      continue;
    if (!needed[i])
      continue;
    if (d[i] <= 10 * c) {
      trueG[sta].push_back({i, d[i]});
    }
  }
}
void all_init() {
  rep1(i, n) {
    G[i].clear();
    trueG[i].clear();
  }
  mp.clear();
  n = 2;
}
int main() {
  while (cin >> m >> s_n >> c, m) {
    all_init();
    string in1, in2;
    cin >> in1 >> in2;
    mp[in1] = 1;
    mp[in2] = 2;
    rep(i, m) {
      int z;
      cin >> in1 >> in2 >> z;
      int i1 = to_id(in1), i2 = to_id(in2);
      G[i1].push_back({i2, z});
      G[i2].push_back({i1, z});
    }
    needed[1] = needed[2] = true;
    rep(i, s_n) {
      cin >> in1;
      needed[to_id(in1)] = true;
    }
    rep1(i, n) if (needed[i]) dijkstra(i);
    init(1);
    q.push({0, 1});
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      rep(j, trueG[v].size()) {
        int to = trueG[v][j].to;
        int cost = p.first + trueG[v][j].cost;
        if (cost < d[to]) {
          d[to] = cost;
          q.push({d[to], to});
        }
      }
    }
    if (d[2] == mod)
      cout << -1 << endl;
    else
      cout << d[2] << endl;
  }
  return 0;
}