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

#include <bits/stdc++.h>

using namespace std;

// #define LOG(...) fprintf(stderr, __VA_ARGS__)
#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct Edge {
  int to, cost;
};

struct P {
  int p, cost, dist;

  bool operator>(const P &p) const { return cost > p.cost; }
};

const int INF = (int)1e9;

int main() {
  int road_n, gas_n, capacity;
  string src, dst;
  while (cin >> road_n >> gas_n >> capacity, road_n | gas_n | capacity) {
    cin >> src >> dst;

    map<string, int> id;
    vector<vector<Edge>> E;
    vector<bool> isgas;

    REP(i, road_n) {
      string from, to;
      int cost;
      cin >> from >> to >> cost;
      if (!EXIST(id, from)) {
        id.insert({from, id.size()});
        E.push_back(vector<Edge>());
        isgas.push_back(false);
      }
      if (!EXIST(id, to)) {
        id.insert({to, id.size()});
        E.push_back(vector<Edge>());
        isgas.push_back(false);
      }
      E[id[from]].push_back({id[to], cost});
      E[id[to]].push_back({id[from], cost});
    }
    REP(i, gas_n) {
      string town;
      cin >> town;
      isgas[id[town]] = true;
    }

    int start = id[src];
    int goal = id[dst];
    vvi G(id.size(), vi(capacity * 10 + 1, INF));
    G[start][0] = 0;

    priority_queue<P, vector<P>, greater<P>> que;
    que.push({start, 0, 0});

    int res = -1;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      if (p.p == goal) {
        res = p.cost;
        break;
      }

      for (Edge e : E[p.p]) {
        int next_dist = p.dist + e.cost;
        if (next_dist > capacity * 10)
          continue;
        if (isgas[e.to])
          next_dist = 0;
        int next_cost = p.cost + e.cost;
        if (G[e.to][next_dist] > next_cost) {
          G[e.to][next_dist] = next_cost;
          que.push({e.to, next_cost, next_dist});
        }
      }
    }

    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define LOG(...) fprintf(stderr, __VA_ARGS__)
#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct Edge {
  int town, cost;
};

struct P {
  int town, cost;
  int distance;

  bool operator>(const P &p) const { return cost > p.cost; }
};

const int MAX_CAPACITY = 200;
const int INF = (int)1e9;

struct Edgestr {
  string from, to;
  int cost;
};

int main() {
  int town_n, gas_n, capacity;
  string src, dst;
  while (cin >> town_n >> gas_n >> capacity, town_n | gas_n | capacity) {
    cin >> src >> dst;

    vector<vector<Edge>> E(town_n + 1);
    vb isgas(town_n + 1);

    map<string, int> id;
    map<int, string> ids;
    vector<Edgestr> edgestring(town_n);
    REP(i, town_n) {
      string from, to;
      int cost;
      cin >> from >> to >> cost;
      if (!EXIST(id, from))
        id[from] = id.size();
      if (!EXIST(id, to))
        id[to] = id.size();
      ids[id[from]] = from;
      ids[id[to]] = to;
      E[id[from]].push_back({id[to], cost});
      E[id[to]].push_back({id[from], cost});
    }
    REP(i, gas_n) {
      string town;
      cin >> town;
      isgas[id[town]] = true;
    }
    int start = id[src];
    int goal = id[dst];

    vvi G(town_n + 1, vi(capacity * 10 + 1, INF));
    G[start][0];

    priority_queue<P, vector<P>, greater<P>> que;
    que.push({start, 0, 0});

    int res = -1;
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      LOG("%10s %3d %d\\n", ids[p.town].c_str(), p.cost, p.distance);
      if (p.town == goal) {
        res = p.cost;
        break;
      }

      for (Edge e : E[p.town]) {
        int next_dist = p.distance + e.cost;
        LOG("%s -->> %s%s %d/%d\\n", ids[p.town].c_str(),
            isgas[e.town] ? "*" : "", ids[e.town].c_str(), next_dist,
            capacity * 10);
        if (next_dist > capacity * 10)
          continue;
        if (isgas[e.town])
          next_dist = 0;
        int next_cost = p.cost + e.cost;
        if (G[e.town][next_dist] > next_cost) {
          G[e.town][next_dist] = next_cost;
          que.push({e.town, next_cost, next_dist});
        }
      }
    }

    cout << res << endl;
  }
}