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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;

const int inf = numeric_limits<int>::max() / 3;

struct edge {
  int to, cost;
  edge() {}
  edge(int to_, int cost_) : to(to_), cost(cost_) {}
};

int N, M, cap;
int src, dest;
vector<edge> g[6000];

void solve() {
  for (int i = 0; i < 6000; i++)
    g[i] = vector<edge>();
  string srcc, destt;
  cap *= 10;
  cin >> srcc >> destt;
  int namecount = 0;
  map<string, int> names;
  for (int i = 0; i < N; i++) {
    string c1, c2;
    int dist;
    cin >> c1 >> c2 >> dist;
    if (!names.count(c1)) {
      names[c1] = namecount++;
    }
    if (!names.count(c2)) {
      names[c2] = namecount++;
    }

    g[names[c1]].push_back(edge(names[c2], dist));
    g[names[c2]].push_back(edge(names[c1], dist));
  }

  if (!names.count(srcc)) {
    names[srcc] = namecount++;
  }
  if (!names.count(destt)) {
    names[destt] = namecount++;
  }
  int src = names[srcc];
  int dest = names[destt];

  set<int> s;
  for (int i = 0; i < M; i++) {
    string ss;
    cin >> ss;
    s.insert(names[ss]);
  }

  using Vertex = pair<int, int>;   // ?????????????,???????????¨
  using State = pair<int, Vertex>; // ????????????¢, ?????????????

  // dist[?????????????][?????£?????????????¨??????????????¢???????\\] =
  // ?????????????????????????¢
  vector<vector<int>> dist(6000, vector<int>(3000, inf));

  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State(0, Vertex(src, cap)));
  dist[src][cap] = 0;

  // for(auto p : names) cerr << p.first << " " << p.second << endl;

  while (!que.empty()) {
    int d = que.top().first;
    Vertex v = que.top().second;
    que.pop();

    int node = v.first;
    int c = v.second;

    // cerr << node << " " << c << " " << d << endl;

    if (s.count(node)) {
      c = cap;
    }

    for (edge &e : g[node]) {
      int ncap = c - e.cost;
      // cerr << "e = " << e.to << " " << e.cost << endl;
      if (ncap < 0)
        continue;
      if (dist[e.to][ncap] > d + e.cost) {
        // cerr << "push " << e.to << " " << ncap << endl;
        dist[e.to][ncap] = d + e.cost;
        que.push(State(dist[e.to][ncap], Vertex(e.to, ncap)));
      }
    }
  }

  int ans = inf;
  for (int i = 0; i < 3000; i++)
    ans = min(ans, dist[dest][i]);

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

int main() {
  while (1) {
    cin >> N >> M >> cap;
    if (N == 0 and M == 0 and cap == 0)
      break;
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;

const int inf = numeric_limits<int>::max() / 3;

struct edge {
  int to, cost;
  edge() {}
  edge(int to_, int cost_) : to(to_), cost(cost_) {}
};

int N, M, cap;
int src, dest;
vector<edge> g[4000];

void solve() {
  for (int i = 0; i < 4000; i++)
    g[i] = vector<edge>();
  string srcc, destt;
  cap *= 10;
  cin >> srcc >> destt;
  int namecount = 0;
  map<string, int> names;
  for (int i = 0; i < N; i++) {
    string c1, c2;
    int dist;
    cin >> c1 >> c2 >> dist;
    if (!names.count(c1)) {
      names[c1] = namecount++;
    }
    if (!names.count(c2)) {
      names[c2] = namecount++;
    }

    g[names[c1]].push_back(edge(names[c2], dist));
    g[names[c2]].push_back(edge(names[c1], dist));
  }

  if (!names.count(srcc)) {
    names[srcc] = namecount++;
  }
  if (!names.count(destt)) {
    names[destt] = namecount++;
  }
  int src = names[srcc];
  int dest = names[destt];

  set<int> s;
  for (int i = 0; i < M; i++) {
    string ss;
    cin >> ss;
    s.insert(names[ss]);
  }

  using Vertex = pair<int, int>;   // ?????????????,???????????¨
  using State = pair<int, Vertex>; // ????????????¢, ?????????????

  // dist[?????????????][?????£?????????????¨??????????????¢???????\\] =
  // ?????????????????????????¢
  vector<vector<int>> dist(4000, vector<int>(300, inf));

  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State(0, Vertex(src, cap)));
  dist[src][cap] = 0;

  // for(auto p : names) cerr << p.first << " " << p.second << endl;

  while (!que.empty()) {
    int d = que.top().first;
    Vertex v = que.top().second;
    que.pop();

    int node = v.first;
    int c = v.second;

    // cerr << node << " " << c << " " << d << endl;

    if (s.count(node)) {
      c = cap;
    }

    for (edge &e : g[node]) {
      int ncap = c - e.cost;
      // cerr << "e = " << e.to << " " << e.cost << endl;
      if (ncap < 0)
        continue;
      if (dist[e.to][ncap] > d + e.cost) {
        // cerr << "push " << e.to << " " << ncap << endl;
        dist[e.to][ncap] = d + e.cost;
        que.push(State(dist[e.to][ncap], Vertex(e.to, ncap)));
      }
    }
  }

  int ans = inf;
  for (int i = 0; i < 300; i++)
    ans = min(ans, dist[dest][i]);

  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

int main() {
  while (1) {
    cin >> N >> M >> cap;
    if (N == 0 and M == 0 and cap == 0)
      break;
    solve();
  }
  return 0;
}