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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MT make_tuple
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e9;

struct Edge {
  int to, cost;

  Edge(int t, int c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

int N, M, C, SZ;
vector<Edge> G[6010];

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

  while (cin >> N >> M >> C, N) {
    map<string, int> cor;
    string src, dest;
    cin >> src >> dest;
    SZ = 0;
    cor[src] = SZ++;
    cor[dest] = SZ++;
    C *= 10;
    REP(i, 6010) G[i].clear();
    REP(i, N) {
      string s1, s2;
      int d;
      cin >> s1 >> s2 >> d;
      if (!cor.count(s1))
        cor[s1] = SZ++;
      if (!cor.count(s2))
        cor[s2] = SZ++;
      int u = cor[s1], v = cor[s2];
      G[u].PB(Edge(v, d));
      G[v].PB(Edge(u, d));
    }

    set<int> LPG;
    REP(i, M) {
      string s;
      cin >> s;
      if (!cor.count(s))
        continue;
      LPG.insert(cor[s]);
    }

    typedef tuple<int, int, int> Node;
    priority_queue<Node, vector<Node>, greater<Node>> pq;
    VVI dist(SZ + 10, VI(C + 10, INF));
    dist[0][C] = 0;
    pq.push(MT(0, 0, C));

    int ans = INF;
    while (!pq.empty()) {
      auto tp = pq.top();
      pq.pop();
      int c, v, f;
      tie(c, v, f) = tp;

      if (v == 1) {
        ans = c;
        break;
      }
      if (dist[v][f] < c)
        continue;
      for (auto &e : G[v]) {
        int d = e.cost;
        if (f < d)
          continue;
        int nf = LPG.count(e.to) ? C : f - d;
        if (dist[e.to][nf] > dist[v][f] + d) {
          dist[e.to][nf] = dist[v][f] + d;
          pq.push(MT(dist[e.to][nf], e.to, nf));
        }
      }
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MT make_tuple
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e9;

struct Edge {
  int to, cost;

  Edge(int t, int c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

int N, M, C, SZ;
vector<Edge> G[6001];

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

  while (cin >> N >> M >> C, N) {
    map<string, int> cor;
    string src, dest;
    cin >> src >> dest;
    SZ = 0;
    cor[src] = SZ++;
    cor[dest] = SZ++;
    C *= 10;
    REP(i, 6010) G[i].clear();
    REP(i, N) {
      string s1, s2;
      int d;
      cin >> s1 >> s2 >> d;
      if (!cor.count(s1))
        cor[s1] = SZ++;
      if (!cor.count(s2))
        cor[s2] = SZ++;
      int u = cor[s1], v = cor[s2];
      G[u].PB(Edge(v, d));
      G[v].PB(Edge(u, d));
    }

    set<int> LPG;
    REP(i, M) {
      string s;
      cin >> s;
      if (!cor.count(s))
        continue;
      LPG.insert(cor[s]);
    }

    typedef tuple<int, int, int> Node;
    VVI dist(SZ + 10, VI(C + 10, INF));
    priority_queue<Node, vector<Node>, greater<Node>> pq;
    dist[0][C] = 0;
    pq.push(MT(0, 0, C));

    int ans = INF;
    while (!pq.empty()) {
      auto tp = pq.top();
      pq.pop();
      int c, v, f;
      tie(c, v, f) = tp;
      if (v == 1) {
        ans = c;
        break;
      }
      if (dist[v][f] < c)
        continue;
      for (int i = 0; i < G[v].size(); ++i) {
        const Edge &e = G[v][i];
        int d = e.cost;
        if (f < d)
          continue;
        int nf = LPG.count(e.to) ? C : f - d;
        if (dist[e.to][nf] > dist[v][f] + d) {
          dist[e.to][nf] = dist[v][f] + d;
          pq.push(MT(dist[e.to][nf], e.to, nf));
        }
      }
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}