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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> edge;
struct state {
  int cost, coupon, station;
  state(int cost_, int coupon_, int station_)
      : cost(cost_), coupon(coupon_), station(station_) {}
  bool operator<(const state &s) const {
    if (cost == s.cost) {
      return coupon > s.coupon;
    }
    return cost > s.cost;
  }
};
int djikstra(vector<vector<edge>> graph, int start, int goal, int coupon) {
  priority_queue<state> pq;
  pq.push(state(0, coupon, start));
  bool visited[coupon + 1][graph.size()];
  fill_n((bool *)visited, (coupon + 1) * graph.size(), false);
  while (!pq.empty()) {
    state cur = pq.top();
    pq.pop();
    if (visited[cur.coupon][cur.station]) {
      continue;
    }
    if (cur.station == goal) {
      return cur.cost;
    }
    visited[cur.coupon][cur.station] = true;
    for (int i = 0; i < graph[cur.station].size(); ++i) {
      int to = graph[cur.station][i].first;
      int c = graph[cur.station][i].second;
      if (cur.coupon > 0) {
        pq.push(state(cur.cost + c / 2, cur.coupon - 1, to));
      }
      pq.push(state(cur.cost + c, cur.coupon, to));
    }
  }
  // assert(false);
  return -1;
}
int main() {
  int c, n, m, s, d;
  while (true) {
    cin >> c >> n >> m >> s >> d;
    if ((c | n | m | s | d) == 0) {
      break;
    }
    vector<vector<edge>> graph(n + 1, vector<edge>());
    for (int i = 0; i < m; ++i) {
      int a, b, f;
      cin >> a >> b >> f;
      graph[a].push_back(make_pair(b, f));
      graph[b].push_back(make_pair(a, f));
    }
    cout << djikstra(graph, s, d, c) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> edge;
struct state {
  int cost, coupon, station;
  state(int cost_, int coupon_, int station_)
      : cost(cost_), coupon(coupon_), station(station_) {}
  bool operator<(const state &s) const {
    if (cost == s.cost) {
      return coupon > s.coupon;
    }
    return cost > s.cost;
  }
};
int djikstra(vector<vector<edge>> graph, int start, int goal, int coupon) {
  priority_queue<state> pq;
  pq.push(state(0, coupon, start));
  bool visited[coupon][graph.size()];
  fill_n((bool *)visited, (coupon + 1) * graph.size(), false);
  return 0;
  while (!pq.empty()) {
    state cur = pq.top();
    pq.pop();
    if (visited[cur.coupon][cur.station]) {
      continue;
    }
    if (cur.station == goal) {
      return cur.cost;
    }
    visited[cur.coupon][cur.station] = true;
    for (int i = 0; i < graph[cur.station].size(); ++i) {
      int to = graph[cur.station][i].first;
      int c = graph[cur.station][i].second;
      if (cur.coupon > 0) {
        pq.push(state(cur.cost + c / 2, cur.coupon - 1, to));
      }
      pq.push(state(cur.cost + c, cur.coupon, to));
    }
  }
  // assert(false);
  return -1;
}
int main() {
  int c, n, m, s, d;
  while (true) {
    cin >> c >> n >> m >> s >> d;
    if ((c | n | m | s | d) == 0) {
      break;
    }
    vector<vector<edge>> graph(n + 1, vector<edge>());
    for (int i = 0; i < m; ++i) {
      int a, b, f;
      cin >> a >> b >> f;
      graph[a].push_back(make_pair(b, f));
      graph[b].push_back(make_pair(a, f));
    }
    cout << djikstra(graph, s, d, c) << endl;
  }
  return 0;
}