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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
// typedef pair<int, int> P;
/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem1318 : Long Distance Taxi **/
const int MAX_V = 6010;
const int MAX_CAP = 2010;

int N, M, cap;

int idx;
map<string, int> idx_list;
bool LPG[MAX_V];
int d[MAX_V][MAX_CAP];
int target;

struct Node {
  int v, c, d;
  Node(int _d, int _v, int _c) : v(_v), c(_c), d(_d) {}
  bool operator<(const Node &n) const { return d > n.d; }
};

struct Edge {
  int to;
  int cost;
  Edge(int t, int c) : to(t), cost(c) {}
};
vector<Edge> G[MAX_V];

int toIdx(string name) {
  if (idx_list.count(name)) {
    return idx_list[name];
  } else {
    idx_list[name] = idx++;
    return idx_list[name];
  }
}

void dijkstra(int s) {
  fill(d[0], d[0] + MAX_V * MAX_CAP, INF);
  d[s][cap] = 0;

  priority_queue<Node> Q;
  Q.push(Node(0, s, cap));

  while (Q.size()) {
    Node p = Q.top();
    Q.pop();
    int v = p.v, c = p.c;
    if (v == target)
      break;

    if (LPG[v])
      c = cap;
    // cerr << v << " " << c << " " << p.d << endl;

    bool ok = true;
    for (int i = c; i <= cap; i++) {
      if (d[v][i] < p.d)
        ok = false;
    }
    if (!ok)
      continue;

    for (int i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      int nc = c - e.cost;
      if (nc < 0)
        continue;

      if (d[e.to][nc] > p.d + e.cost) {
        d[e.to][nc] = p.d + e.cost;
        Q.push(Node(d[e.to][nc], e.to, nc));
      }
    }
  }
}

int main() {
  while (cin >> N >> M >> cap, N || M || cap) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    idx_list.clear();
    idx = 0;
    cap *= 10;
    fill(LPG, LPG + MAX_V, false);

    string from, to;
    cin >> from >> to;

    for (int i = 0; i < N; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      int ia = toIdx(a), ib = toIdx(b);
      G[ia].push_back(Edge(ib, c));
      G[ib].push_back(Edge(ia, c));
    }

    for (int i = 0; i < M; i++) {
      string a;
      cin >> a;
      LPG[toIdx(a)] = true;
    }

    target = toIdx(to);
    dijkstra(toIdx(from));

    int ans = INF;
    for (int i = 0; i < MAX_CAP; i++) {
      ans = min(ans, d[toIdx(to)][i]);
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
// typedef pair<int, int> P;
/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem1318 : Long Distance Taxi **/
const int MAX_V = 6001;
const int MAX_CAP = 2001;

int N, M, cap;

int idx;
map<string, int> idx_list;
bool LPG[MAX_V];
int d[MAX_V][MAX_CAP];
int target;

struct Node {
  int v, c, d;
  Node(int _d, int _v, int _c) : v(_v), c(_c), d(_d) {}
  bool operator<(const Node &n) const { return d > n.d; }
};

struct Edge {
  int to;
  int cost;
  Edge(int t, int c) : to(t), cost(c) {}
};
vector<Edge> G[MAX_V];

int toIdx(string name) {
  if (idx_list.count(name)) {
    return idx_list[name];
  } else {
    idx_list[name] = idx++;
    return idx_list[name];
  }
}

void dijkstra(int s) {
  fill(d[0], d[0] + MAX_V * MAX_CAP, INF);
  d[s][cap] = 0;

  priority_queue<Node> Q;
  Q.push(Node(0, s, cap));

  while (Q.size()) {
    Node p = Q.top();
    Q.pop();
    int v = p.v, c = p.c;
    // if (v == target) break;

    if (LPG[v])
      c = cap;
    // cerr << v << " " << c << " " << p.d << endl;

    bool ok = true;
    for (int i = c; i <= cap; i++) {
      if (d[v][i] < p.d)
        ok = false;
    }
    if (!ok)
      continue;

    for (int i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      int nc = c - e.cost;
      if (nc < 0)
        continue;

      if (d[e.to][nc] > p.d + e.cost) {
        d[e.to][nc] = p.d + e.cost;
        Q.push(Node(d[e.to][nc], e.to, nc));
      }
    }
  }
}

int main() {
  while (cin >> N >> M >> cap, N || M || cap) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    cap *= 10;
    fill(LPG, LPG + MAX_V, false);

    string from, to;
    cin >> from >> to;

    for (int i = 0; i < N; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      int ia = toIdx(a), ib = toIdx(b);
      G[ia].push_back(Edge(ib, c));
      G[ib].push_back(Edge(ia, c));
    }

    for (int i = 0; i < M; i++) {
      string a;
      cin >> a;
      LPG[toIdx(a)] = true;
    }

    target = toIdx(to);
    dijkstra(toIdx(from));

    int ans = INF;
    for (int i = 0; i < MAX_CAP; i++) {
      ans = min(ans, d[toIdx(to)][i]);
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}