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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_to, int arg_fee, int arg_need_time, int arg_company) {
    to = arg_to;
    fee = arg_fee;
    need_time = arg_need_time;
    company = arg_company;
  }

  int to, fee, need_time, company;
};

struct Passport {
  int state, price;
};

struct Data {
  Data(int arg_station, int arg_total_cost, int arg_time) {
    station = arg_station;
    total_cost = arg_total_cost;
    time = arg_time;
  }

  bool operator<(const struct Data &arg) const {
    return total_cost > arg.total_cost;
  };
  int station, total_cost, time;
};

int N, M, H, K, POW[9];

void func() {

  vector<Info> V[N + 1];

  int from, to, cost, time, company;
  for (int loop = 0; loop < M; loop++) {
    scanf("%d %d %d %d %d", &from, &to, &cost, &time, &company);
    company--;
    V[from].push_back(Info(to, cost, time, company));
    V[to].push_back(Info(from, cost, time, company));
  }

  int start, goal;
  scanf("%d %d", &start, &goal);

  int P;
  scanf("%d", &P);

  Passport passport[P];

  int num, tmp, tmp_state;

  for (int loop = 0; loop < P; loop++) {
    scanf("%d %d", &num, &passport[loop].price);

    tmp_state = 0;
    for (int i = 0; i < num; i++) {
      scanf("%d", &tmp);
      tmp--;
      tmp_state += POW[tmp];
    }
    passport[loop].state = tmp_state;
  }

  int dp[POW[K]];

  dp[0] = 0;
  for (int i = 1; i < POW[K]; i++)
    dp[i] = BIG_NUM;

  int next_state;

  for (int i = 0; i < P; i++) {
    for (int state = 0; state < POW[K]; state++) {
      if (dp[state] == BIG_NUM)
        continue;

      next_state = 0;

      for (int loop = 0; loop < K; loop++) {
        if ((state & (1 << loop)) || (passport[i].state & (1 << loop))) {
          next_state += POW[loop];
        }
      }

      dp[next_state] = min(dp[next_state], dp[state] + passport[i].price);
    }
  }

  int min_Cost[N + 1][H + 1];

  int ans = BIG_NUM, next_time;
  priority_queue<Data> Q;

  for (int passport_state = 0; passport_state < POW[K]; passport_state++) {

    if (dp[passport_state] == BIG_NUM)
      continue;

    for (int i = 1; i <= N; i++) {
      for (int k = 0; k < H + 1; k++)
        min_Cost[i][k] = BIG_NUM;
    }

    min_Cost[start][0] = dp[passport_state];
    Q.push(Data(start, dp[passport_state], 0));

    while (!Q.empty()) {

      if (Q.top().total_cost > min_Cost[Q.top().station][Q.top().time]) {
        Q.pop();
      } else {

        for (int i = 0; i < V[Q.top().station].size(); i++) {
          next_time = Q.top().time + V[Q.top().station][i].need_time;
          if (next_time > H)
            continue;

          if (passport_state & (1 << V[Q.top().station][i].company)) {

            if (min_Cost[V[Q.top().station][i].to][next_time] >
                Q.top().total_cost) {
              min_Cost[V[Q.top().station][i].to][next_time] =
                  Q.top().total_cost;
              Q.push(Data(V[Q.top().station][i].to, Q.top().total_cost,
                          next_time));
            }

          } else {

            if (min_Cost[V[Q.top().station][i].to][next_time] >
                Q.top().total_cost + V[Q.top().station][i].fee) {
              min_Cost[V[Q.top().station][i].to][next_time] =
                  Q.top().total_cost + V[Q.top().station][i].fee;
              Q.push(Data(V[Q.top().station][i].to,
                          min_Cost[V[Q.top().station][i].to][next_time],
                          next_time));
            }
          }
        }

        Q.pop();
      }
    }
    for (int i = 0; i <= H; i++)
      ans = min(ans, min_Cost[goal][i]);
  }

  if (ans == BIG_NUM) {
    printf("-1\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  for (int i = 0; i <= 8; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d %d %d %d", &N, &M, &H, &K);
    if (N == 0 && M == 0 && H == 0 && K == 0)
      break;

    func();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info(int arg_to, int arg_fee, int arg_need_time, int arg_company) {
    to = arg_to;
    fee = arg_fee;
    need_time = arg_need_time;
    company = arg_company;
  }

  int to, fee, need_time, company;
};

struct Passport {
  int state, price;
};

struct Data {
  Data(int arg_station, int arg_total_cost, int arg_time) {
    station = arg_station;
    total_cost = arg_total_cost;
    time = arg_time;
  }

  bool operator<(const struct Data &arg) const {
    return total_cost > arg.total_cost;
  };
  int station, total_cost, time;
};

int N, M, H, K, POW[8];

void func() {

  vector<Info> V[N + 1];

  int from, to, cost, time, company;
  for (int loop = 0; loop < M; loop++) {
    scanf("%d %d %d %d %d", &from, &to, &cost, &time, &company);
    company--;
    V[from].push_back(Info(to, cost, time, company));
    V[to].push_back(Info(from, cost, time, company));
  }

  int start, goal;
  scanf("%d %d", &start, &goal);

  int P;
  scanf("%d", &P);

  Passport passport[P];

  int num, tmp, tmp_state;

  for (int loop = 0; loop < P; loop++) {
    scanf("%d %d", &num, &passport[loop].price);

    tmp_state = 0;
    for (int i = 0; i < num; i++) {
      scanf("%d", &tmp);
      tmp--;
      tmp_state += POW[tmp];
    }
    passport[loop].state = tmp_state;
  }

  int dp[POW[K]];

  dp[0] = 0;
  for (int i = 1; i < POW[K]; i++)
    dp[i] = BIG_NUM;

  int next_state;

  for (int i = 0; i < P; i++) {
    for (int state = 0; state < POW[K]; state++) {
      if (dp[state] == BIG_NUM)
        continue;

      next_state = 0;

      for (int loop = 0; loop < K; loop++) {
        if ((state & (1 << loop)) || (passport[i].state & (1 << loop))) {
          next_state += POW[loop];
        }
      }

      dp[next_state] = min(dp[next_state], dp[state] + passport[i].price);
    }
  }

  int min_Cost[N + 1][H + 1];

  int ans = BIG_NUM, next_time;
  priority_queue<Data> Q;

  for (int passport_state = 0; passport_state < POW[K]; passport_state++) {

    if (dp[passport_state] == BIG_NUM)
      continue;

    for (int i = 1; i <= N; i++) {
      for (int k = 0; k < H + 1; k++)
        min_Cost[i][k] = BIG_NUM;
    }

    min_Cost[start][0] = dp[passport_state];
    Q.push(Data(start, dp[passport_state], 0));

    while (!Q.empty()) {

      if (Q.top().total_cost > min_Cost[Q.top().station][Q.top().time]) {
        Q.pop();
      } else {

        for (int i = 0; i < V[Q.top().station].size(); i++) {
          next_time = Q.top().time + V[Q.top().station][i].need_time;
          if (next_time > H)
            continue;

          if (passport_state & (1 << V[Q.top().station][i].company)) {

            if (min_Cost[V[Q.top().station][i].to][next_time] >
                Q.top().total_cost) {
              min_Cost[V[Q.top().station][i].to][next_time] =
                  Q.top().total_cost;
              Q.push(Data(V[Q.top().station][i].to, Q.top().total_cost,
                          next_time));
            }

          } else {

            if (min_Cost[V[Q.top().station][i].to][next_time] >
                Q.top().total_cost + V[Q.top().station][i].fee) {
              min_Cost[V[Q.top().station][i].to][next_time] =
                  Q.top().total_cost + V[Q.top().station][i].fee;
              Q.push(Data(V[Q.top().station][i].to,
                          min_Cost[V[Q.top().station][i].to][next_time],
                          next_time));
            }
          }
        }

        Q.pop();
      }
    }
    for (int i = 0; i <= H; i++)
      ans = min(ans, min_Cost[goal][i]);
  }

  if (ans == BIG_NUM) {
    printf("-1\\n");
  } else {
    printf("%d\\n", ans);
  }
}

int main() {

  for (int i = 0; i <= 7; i++)
    POW[i] = pow(2, i);

  while (true) {
    scanf("%d %d %d %d", &N, &M, &H, &K);
    if (N == 0 && M == 0 && H == 0 && K == 0)
      break;

    func();
  }

  return 0;
}