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

#pragma GCC optimize "O3"

#define F first
#define S second
#define G0(x) get<0>(x)
#define G1(x) get<1>(x)
#define G2(x) get<2>(x)

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;
typedef tuple<int, int, int> PIII;

constexpr int N = 3000, M = 300, INF = INT_MAX;

int n, m, cap, dis[2 * N + 2];
unordered_map<string, int> sid;
vector<PII> adj[2 * N + 2];
bool is_lpg[2 * N + 2];

inline void init() {
  sid.clear();
  cap *= 10;
  memset(is_lpg, 0, sizeof(is_lpg));
}

inline int solve() {
  init();
  string s0, s1;
  cin >> s0 >> s1;
  sid.emplace(s0, 0);
  adj[0].clear();
  sid.emplace(s1, 1);
  adj[1].clear();
  for (int i = 0; i < n; ++i) {
    int d;
    cin >> s0 >> s1 >> d;
    if (!sid.count(s0)) {
      adj[sid.size()].clear();
      sid.emplace(s0, sid.size());
    }
    if (!sid.count(s1)) {
      adj[sid.size()].clear();
      sid.emplace(s1, sid.size());
    }
    int id0 = sid[s0], id1 = sid[s1];
    adj[id0].emplace_back(id1, d);
    adj[id1].emplace_back(id0, d);
  }
  is_lpg[0] = is_lpg[1] = true;
  for (int i = 0; i < m; ++i) {
    cin >> s0;
    is_lpg[sid[s0]] = true;
  }
  int mid = sid.size();
  // cerr << "# nodes = " << mid << endl;
  fill(dis, dis + mid, INF);
  dis[0] = 0;
  priority_queue<PII, vector<PII>, greater<PII>> pq0;
  pq0.emplace(0, 0);
  while (!pq0.empty()) {
    PII nxt0 = pq0.top();
    pq0.pop();
    // cerr << "cost " << nxt0.F << " id " << nxt0.S << endl;

    vector<int> tmp_dis(mid, INF);
    tmp_dis[nxt0.S] = nxt0.F;
    priority_queue<PIII, vector<PIII>, greater<PIII>> pq1;
    pq1.emplace(nxt0.F, cap, nxt0.S);
    while (!pq1.empty()) {
      PIII nxt1 = pq1.top();
      pq1.pop();
      for (auto &v : adj[G2(nxt1)])
        if (G1(nxt1) >= v.S && G0(nxt1) + v.S < tmp_dis[v.F]) {
          tmp_dis[v.F] = G0(nxt1) + v.S;
          pq1.emplace(tmp_dis[v.F], G1(nxt1) - v.S, v.F);
        }
    }
    for (int i = 0; i < mid; ++i)
      if (is_lpg[i] && tmp_dis[i] < dis[i]) {
        dis[i] = tmp_dis[i];
        pq0.emplace(dis[i], i);
      }
  }
  if (dis[1] == INF)
    return -1;
  else
    return dis[1];
}

int main() {
  while (scanf("%d %d %d", &n, &m, &cap) == 3 && n)
    printf("%d\\n", solve());
}

Error: 0

Fixed submission:

#pragma GCC optimize "O3"

#define F first
#define S second
#define G0(x) get<0>(x)
#define G1(x) get<1>(x)
#define G2(x) get<2>(x)

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;
typedef tuple<int, int, int> PIII;

constexpr int N = 3000, M = 300, INF = INT_MAX;

int n, m, cap, dis[2 * N + 2];
unordered_map<string, int> sid;
vector<PII> adj[2 * N + 2];
bool is_lpg[2 * N + 2];

inline void init() {
  sid.clear();
  cap *= 10;
  memset(is_lpg, 0, sizeof(is_lpg));
}

inline int solve() {
  init();
  string s0, s1;
  cin >> s0 >> s1;
  sid.emplace(s0, 0);
  adj[0].clear();
  sid.emplace(s1, 1);
  adj[1].clear();
  for (int i = 0; i < n; ++i) {
    int d;
    cin >> s0 >> s1 >> d;
    if (!sid.count(s0)) {
      sid.emplace(s0, sid.size());
      adj[sid.size()].clear();
    }
    if (!sid.count(s1)) {
      sid.emplace(s1, sid.size());
      adj[sid.size()].clear();
    }
    int id0 = sid[s0], id1 = sid[s1];
    adj[id0].emplace_back(id1, d);
    adj[id1].emplace_back(id0, d);
  }
  is_lpg[0] = is_lpg[1] = true;
  for (int i = 0; i < m; ++i) {
    cin >> s0;
    is_lpg[sid[s0]] = true;
  }
  int mid = sid.size();
  // cerr << "# nodes = " << mid << endl;
  fill(dis, dis + mid, INF);
  dis[0] = 0;
  priority_queue<PII, vector<PII>, greater<PII>> pq0;
  pq0.emplace(0, 0);
  while (!pq0.empty()) {
    PII nxt0 = pq0.top();
    pq0.pop();
    // cerr << "cost " << nxt0.F << " id " << nxt0.S << endl;

    vector<int> tmp_dis(mid, INF);
    tmp_dis[nxt0.S] = nxt0.F;
    priority_queue<PIII, vector<PIII>, greater<PIII>> pq1;
    pq1.emplace(nxt0.F, cap, nxt0.S);
    while (!pq1.empty()) {
      PIII nxt1 = pq1.top();
      pq1.pop();
      for (auto &v : adj[G2(nxt1)])
        if (G1(nxt1) >= v.S && G0(nxt1) + v.S < tmp_dis[v.F]) {
          tmp_dis[v.F] = G0(nxt1) + v.S;
          pq1.emplace(tmp_dis[v.F], G1(nxt1) - v.S, v.F);
        }
    }
    if (tmp_dis[1] != INF)
      return tmp_dis[1];
    for (int i = 0; i < mid; ++i)
      if (is_lpg[i] && tmp_dis[i] < dis[i]) {
        dis[i] = tmp_dis[i];
        pq0.emplace(dis[i], i);
      }
  }
  if (dis[1] == INF)
    return -1;
  else
    return dis[1];
}

int main() {
  while (scanf("%d %d %d", &n, &m, &cap) == 3 && n)
    printf("%d\\n", solve());
}