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

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> Pi;
typedef long long ll;
struct edge {
  int to;
  ll dist;
  edge() {}
  edge(int to, ll dist) : to(to), dist(dist) {}
};
const ll inf = 1LL << 55;

vector<vector<edge>> G(110);
ll minkal[1 << 8][110];

int m, n, k, d;
ll cake[8];

ll dijkstra() {
  fill(minkal[0], minkal[1 << 8], inf);
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  que.push(Pi(0, P(0, 0)));
  minkal[0][0] = 0;
  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    int vd = p.second.second, now = p.second.first;
    ll kal = p.first;
    if (minkal[vd][now] < kal)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if (0 < e.to && e.to <= m) {
        if (!((vd >> (e.to - 1)) & 1)) {
          if (minkal[vd | (1 << (e.to - 1))][e.to] >
              kal + e.dist * k - cake[e.to - 1]) {
            que.push(Pi(kal + e.dist * k - cake[e.to - 1],
                        P(e.to, vd | (1 << (e.to - 1)))));
            minkal[vd | (1 << (e.to - 1))][e.to] =
                kal + e.dist * k - cake[e.to - 1];
          }
        }
      } else if (minkal[vd][e.to] > kal + e.dist * k) {
        que.push(Pi(kal + e.dist * k, P(e.to, vd)));
        minkal[vd][e.to] = kal + e.dist * k;
      }
    }
  }
  ll ret = inf;
  for (int i = 0; i < 1 << m; i++) {
    ret = min(ret, minkal[i][n + m + 1]);
  }
  return ret;
}

int main() {
  while (cin >> m >> n >> k >> d, m || n || k || d) {
    G.clear();
    G.resize(m + n + 2);
    memset(cake, 0, sizeof(cake));
    for (int i = 0; i < m; i++)
      cin >> cake[i];
    for (int i = 0; i < d; i++) {
      string f_str, t_str;
      int from, to;
      ll dist;
      cin >> f_str >> t_str >> dist;
      if (f_str[0] == 'H')
        from = 0;
      if (f_str[0] == 'D')
        from = n + m + 1;
      if (f_str[0] == 'C')
        from = atoi(f_str.substr(1).c_str());
      if (f_str[0] == 'L')
        from = atoi(f_str.substr(1).c_str()) + m;
      if (t_str[0] == 'H')
        to = 0;
      if (t_str[0] == 'D')
        to = n + m + 1;
      if (t_str[0] == 'C')
        to = atoi(t_str.substr(1).c_str());
      if (t_str[0] == 'L')
        to = atoi(t_str.substr(1).c_str()) + m;
      G[from].push_back(edge(to, dist));
      G[to].push_back(edge(from, dist));
    }
    cout << dijkstra() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> Pi;

struct edge {
  int to, dist;
  edge() {}
  edge(int to, int dist) : to(to), dist(dist) {}
};
const int inf = 1 << 25;

vector<vector<edge>> G(110);
int minkal[1 << 8][110];

int m, n, k, d;
int cake[110];

int dijkstra() {
  fill(minkal[0], minkal[1 << 8], inf);
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  vector<bool> used(110, false);
  que.push(Pi(0, P(0, 0)));
  minkal[0][0] = 0;
  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    int vd = p.second.second, now = p.second.first, kal = p.first;
    if (minkal[vd][now] < kal)
      continue;
    for (int i = 0; i < G[now].size(); i++) {
      edge e = G[now][i];
      if (0 < e.to && e.to <= m) {
        if (!((vd >> (e.to - 1)) & 1)) {
          if (minkal[vd | (1 << (e.to - 1))][e.to] >
              kal + e.dist * k - cake[e.to]) {
            que.push(Pi(kal + e.dist * k - cake[e.to],
                        P(e.to, vd | (1 << (e.to - 1)))));
            minkal[vd | (1 << (e.to - 1))][e.to] =
                kal + e.dist * k - cake[e.to];
          }
        }
      } else if (minkal[vd][e.to] > kal + e.dist * k) {
        que.push(Pi(kal + e.dist * k, P(e.to, vd)));
        minkal[vd][e.to] = kal + e.dist * k;
      }
    }
  }
  int ret = inf;
  for (int i = 0; i < 1 << 8; i++) {
    ret = min(ret, minkal[i][n + m + 1]);
  }
  return ret;
}

int main() {
  while (cin >> m >> n >> k >> d, m || n || k || d) {
    G.clear();
    G.resize(m + n + 1);
    memset(cake, 0, sizeof(cake));
    for (int i = 1; i <= m; i++)
      cin >> cake[i];
    for (int i = 0; i < d; i++) {
      string f_str, t_str;
      int from, to, dist;
      cin >> f_str >> t_str >> dist;
      if (f_str[0] == 'H')
        from = 0;
      if (f_str[0] == 'D')
        from = n + m + 1;
      if (f_str[0] == 'C')
        from = atoi(f_str.substr(1).c_str());
      if (f_str[0] == 'L')
        from = atoi(f_str.substr(1).c_str()) + m;
      if (t_str[0] == 'H')
        to = 0;
      if (t_str[0] == 'D')
        to = n + m + 1;
      if (t_str[0] == 'C')
        to = atoi(t_str.substr(1).c_str());
      if (t_str[0] == 'L')
        to = atoi(t_str.substr(1).c_str()) + m;
      G[from].push_back(edge(to, dist));
      G[to].push_back(edge(from, dist));
    }
    cout << dijkstra() << endl;
  }
  return 0;
}