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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int N, M, cap;

using Graph = vector<vector<pair<int, int>>>;

const int INF = 1 << 29;
int dist[6010][2010];

struct Elem {
  int pos, tot, cost;
  bool operator<(const Elem &e) const { return cost > e.cost; }
};

int solve(Graph &G, vector<int> &is_station, int S, int T) {
  fill(dist[0], dist[6010], INF);
  dist[S][0] = 0;

  priority_queue<Elem> que;
  que.push(Elem{S, 0, 0});

  while (que.size()) {
    Elem cur = que.top();
    que.pop();

    int u = cur.pos, pre_tot = cur.tot, pre_cost = cur.cost;
    if (dist[u][pre_tot] < pre_cost)
      continue;

    for (auto e : G[u]) {
      int v = e.first, cost = e.second;

      int nxt_cost = pre_cost + cost;
      int nxt_tot = pre_tot + cost;
      if (nxt_tot > cap)
        continue;
      if (is_station[v])
        nxt_tot = 0;

      if (dist[v][nxt_tot] > nxt_cost) {
        dist[v][nxt_tot] = nxt_cost;
        que.push(Elem{v, nxt_tot, nxt_cost});
      }
    }
  }

  int ret = *min_element(dist[T], dist[T] + cap + 1);
  return (ret == INF ? -1 : ret);
}

int main() {
  while (cin >> N >> M >> cap, N) {
    cap *= 10;
    string st, gl;
    cin >> st >> gl;

    vector<string> u(N), v(N);
    vector<int> d(N);

    map<string, int> to_int_id;
    for (int i = 0; i < N; i++) {
      cin >> u[i] >> v[i] >> d[i];
      to_int_id[u[i]]++;
      to_int_id[v[i]]++;
    }

    int id = 0;
    for (auto &e : to_int_id) {
      e.second = id++;
    }

    Graph G(id);
    for (int i = 0; i < N; i++) {
      int x = to_int_id[u[i]];
      int y = to_int_id[v[i]];
      G[x].push_back(make_pair(y, d[i]));
      G[y].push_back(make_pair(x, d[i]));
    }

    vector<int> is_station(id);
    for (int i = 0; i < M; i++) {
      string s;
      cin >> s;
      int s_id = to_int_id[s];

      is_station[s_id] = true;
    }

    int start_v = to_int_id[st];
    int goal_v = to_int_id[gl];
    cout << solve(G, is_station, start_v, goal_v) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int N, M, cap;

using Graph = vector<vector<pair<int, int>>>;

const int INF = 1 << 29;
int dist[3010][2010];

struct Elem {
  int pos, tot, cost;
  bool operator<(const Elem &e) const { return cost > e.cost; }
};

int solve(Graph &G, vector<int> &is_station, int S, int T) {
  fill(dist[0], dist[3010], INF);
  dist[S][0] = 0;

  priority_queue<Elem> que;
  que.push(Elem{S, 0, 0});

  while (que.size()) {
    Elem cur = que.top();
    que.pop();

    int u = cur.pos, pre_tot = cur.tot, pre_cost = cur.cost;
    if (dist[u][pre_tot] < pre_cost)
      continue;

    for (auto e : G[u]) {
      int v = e.first, cost = e.second;

      int nxt_cost = pre_cost + cost;
      int nxt_tot = pre_tot + cost;
      if (nxt_tot > cap)
        continue;
      if (is_station[v])
        nxt_tot = 0;

      if (dist[v][nxt_tot] > nxt_cost) {
        dist[v][nxt_tot] = nxt_cost;
        que.push(Elem{v, nxt_tot, nxt_cost});
      }
    }
  }

  int ret = *min_element(dist[T], dist[T] + cap + 1);
  return (ret == INF ? -1 : ret);
}

int main() {
  while (cin >> N >> M >> cap, N) {
    cap *= 10;
    string st, gl;
    cin >> st >> gl;

    vector<string> u(N), v(N);
    vector<int> d(N);

    map<string, int> to_int_id;
    for (int i = 0; i < N; i++) {
      cin >> u[i] >> v[i] >> d[i];
      to_int_id[u[i]]++;
      to_int_id[v[i]]++;
    }

    int id = 0;
    for (auto &e : to_int_id) {
      e.second = id++;
    }

    Graph G(id);
    for (int i = 0; i < N; i++) {
      int x = to_int_id[u[i]];
      int y = to_int_id[v[i]];
      G[x].push_back(make_pair(y, d[i]));
      G[y].push_back(make_pair(x, d[i]));
    }

    vector<int> is_station(id);
    for (int i = 0; i < M; i++) {
      string s;
      cin >> s;
      int s_id = to_int_id[s];

      is_station[s_id] = true;
    }

    int start_v = to_int_id[st];
    int goal_v = to_int_id[gl];
    cout << solve(G, is_station, start_v, goal_v) << endl;
  }
  return 0;
}