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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

//?§??????°??????????????°?????¶????????????????????°
int n, m, h, k;

//???????§???????????§????1day???????????°
int s, t, p;

typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
  int to, cost, time, corp;

  edge(int to_, int cost_, int time_, int corp_) {
    to = to_;
    cost = cost_;
    time = time_;
    corp = corp_;
  }
};
vector<edge> G[128];

// i????????????????????????????????????
int pass_corps[512];

// pass_cost[bit] bit?????¨?????????????????????????????????
int pass_cost[512];

// corps_cost[bit] bit?????¨???corps??????????????????1day??????????????????
int corps_cost[512];

const int INF = 1e8;

int solve(int use) {

  // d[time][pos]
  int d[25][128];
  for (int i = 0; i < 25; i++) {
    for (int j = 0; j < 128; j++)
      d[i][j] = INF;
  }

  d[0][s] = corps_cost[use];
  priority_queue<PP, vector<PP>, greater<PP>> q;

  // cost,time,pos
  q.push(PP(P(corps_cost[use], 0), s));
  while (!q.empty()) {

    PP pp = q.top();
    q.pop();

    int cost = pp.first.first, time = pp.first.second, pos = pp.second;

    // printf("cost %d, time %d, pos %d\\n",cost,time,pos);

    if (pos == t)
      return cost;

    if (d[time][pos] < cost)
      continue;

    for (int i = 0; i < G[pos].size(); i++) {
      edge e = G[pos][i];
      int to = e.to, ncost = cost + e.cost, nt = time + e.time, ncorp = e.corp;

      // printf("to %d, ncost %d, nt %d ncorp %d\\n",to,ncost,nt,ncorp);

      if (nt > h)
        continue;

      if (use & (1 << ncorp))
        ncost = cost;
      if (ncost < d[nt][to]) {
        d[nt][to] = ncost;
        q.push(PP(P(ncost, nt), to));
      }
    }
  }

  return INF;
}

int main() {

  while (cin >> n >> m >> h >> k, n) {

    for (int i = 0; i < 128; i++)
      G[i].clear();
    for (int i = 0; i < 512; i++) {
      pass_corps[i] = 0;
      pass_cost[i] = corps_cost[i] = INF;
    }
    for (int i = 0; i < m; i++) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      a--;
      b--;
      r--;

      G[a].push_back(edge(b, c, h, r));
      G[b].push_back(edge(a, c, h, r));
    }

    cin >> s >> t >> p;
    s--;
    t--;

    for (int i = 0; i < p; i++) {
      int l, d;
      cin >> l >> d;

      int corps = 0;
      for (int j = 0; j < l; j++) {
        int corp;
        cin >> corp;
        corp--;
        corps |= (1 << corp);
      }

      pass_corps[i] = corps;
      pass_cost[corps] = d;
    }

    // corps_cost[bit] bit?????¨???corps??????????????????1day??????????????????
    corps_cost[0] = 0;
    for (int i = 0; i < (1 << k); i++) {
      if (corps_cost[i] == INF)
        continue;
      for (int j = 0; j < p; j++) {
        corps_cost[i | pass_corps[j]] =
            min(corps_cost[i | pass_corps[j]],
                corps_cost[i] + pass_cost[pass_corps[j]]);
      }
    }

    int res = INF;
    for (int use = 0; use < (1 << k); use++) {
      if (corps_cost[use] == INF)
        continue;
      res = min(res, solve(use));
    }
    if (res == INF)
      res = -1;
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

//?§??????°??????????????°?????¶????????????????????°
int n, m, h, k;

//???????§???????????§????1day???????????°
int s, t, p;

typedef pair<int, int> P;
typedef pair<P, int> PP;
struct edge {
  int to, cost, time, corp;

  edge(int to_, int cost_, int time_, int corp_) {
    to = to_;
    cost = cost_;
    time = time_;
    corp = corp_;
  }
};
vector<edge> G[128];

// i????????????????????????????????????
int pass_corps[512];

// pass_cost[bit] bit?????¨?????????????????????????????????
int pass_cost[512];

// corps_cost[bit] bit?????¨???corps??????????????????1day??????????????????
int corps_cost[512];

const int INF = 1e8;

int solve(int use) {

  int d[25][128];
  for (int i = 0; i < 25; i++) {
    for (int j = 0; j < 128; j++)
      d[i][j] = INF;
  }

  d[0][s] = corps_cost[use];
  priority_queue<PP, vector<PP>, greater<PP>> q;

  // cost,time,pos
  q.push(PP(P(corps_cost[use], 0), s));
  while (!q.empty()) {

    PP pp = q.top();
    q.pop();

    int cost = pp.first.first, time = pp.first.second, pos = pp.second;

    if (pos == t)
      return cost;

    if (d[pos][time] < cost)
      continue;

    for (int i = 0; i < G[pos].size(); i++) {
      edge e = G[pos][i];
      int to = e.to, ncost = cost + e.cost, nt = time + e.time, ncorp = e.corp;

      if (nt > h)
        continue;

      if (use & (1 << ncorp))
        ncost = cost;
      if (ncost < d[to][nt]) {
        d[to][nt] = ncost;
        q.push(PP(P(ncost, nt), to));
      }
    }
  }

  return INF;
}

int main() {

  while (cin >> n >> m >> h >> k, n) {

    for (int i = 0; i < 128; i++)
      G[i].clear();
    for (int i = 0; i < 512; i++) {
      pass_corps[i] = 0;
      pass_cost[i] = corps_cost[i] = INF;
    }
    for (int i = 0; i < m; i++) {
      int a, b, c, h, r;
      cin >> a >> b >> c >> h >> r;
      a--;
      b--;
      r--;

      G[a].push_back(edge(b, c, h, r));
      G[b].push_back(edge(a, c, h, r));
    }

    cin >> s >> t >> p;
    s--;
    t--;

    for (int i = 0; i < p; i++) {
      int l, d;
      cin >> l >> d;

      int corps = 0;
      for (int j = 0; j < l; j++) {
        int corp;
        cin >> corp;
        corp--;
        corps |= (1 << corp);
      }

      pass_corps[i] = corps;
      pass_cost[corps] = d;
    }

    // corps_cost[bit] bit?????¨???corps??????????????????1day??????????????????
    corps_cost[0] = 0;
    for (int i = 0; i < (1 << k); i++) {
      if (corps_cost[i] == INF)
        continue;
      for (int j = 0; j < p; j++) {
        corps_cost[i | pass_corps[j]] =
            min(corps_cost[i | pass_corps[j]],
                corps_cost[i] + pass_cost[pass_corps[j]]);
      }
    }

    int res = INF;
    for (int use = 0; use < (1 << k); use++) {
      if (corps_cost[use] == INF)
        continue;
      res = min(res, solve(use));
    }
    if (res == INF)
      res = -1;
    cout << res << endl;
  }
}