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

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
  edge() {}
};
struct visit {
  int pos, total, b;
  visit(int pos, int total, int b) : pos(pos), total(total), b(b) {}
  visit() {}
  bool operator<(const visit &v) const { return total > v.total; }
};
int N, M, L, K, A, H;
vector<edge> G[100];
bool ice[100];

void solve() {
  bool used[100][101] = {{0}};
  priority_queue<visit> Q;
  Q.push(visit(A, 0, M));

  while (!Q.empty()) {
    visit v = Q.top();
    Q.pop();
    if (used[v.pos][v.b])
      continue;
    used[v.pos][v.b] = true;
    if (v.pos == H) {
      printf("%d\\n", v.total);
      return;
    }
    for (int i = 0; i < G[v.pos].size(); i++) {
      edge &e = G[v.pos][i];
      int tb = v.b - e.cost;
      if (tb < 0)
        continue;
      if (used[e.to][tb])
        continue;
      Q.push(visit(e.to, v.total + e.cost, tb));
    }
    if (!ice[v.pos])
      continue;
    for (int i = v.b + 1; i <= M; i++) {
      if (used[v.pos][i])
        continue;
      Q.push(visit(v.pos, v.total + i - v.b, i));
    }
  }
  puts("Help!");
}
int main() {
  while (scanf("%d%d%d%d%d%d", &N, &M, &L, &K, &A, &H),
         N || M || L || K || A || H) {
    fill_n(G, N, vector<edge>(0));
    fill_n(ice, N, false);
    for (int i = 0; i < L; i++) {
      int pos;
      scanf("%d", &pos);
      ice[pos] = true;
    }
    for (int i = 0; i < K; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      G[a].push_back(edge(b, c));
      G[b].push_back(edge(a, c));
    }

    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
  edge() {}
};
struct visit {
  int pos, total, b;
  visit(int pos, int total, int b) : pos(pos), total(total), b(b) {}
  visit() {}
  bool operator<(const visit &v) const { return total > v.total; }
};
int N, M, L, K, A, H;
vector<edge> G[100];
bool ice[100];

void solve() {
  bool used[100][101] = {{0}};
  priority_queue<visit> Q;
  Q.push(visit(A, 0, M));

  while (!Q.empty()) {
    visit v = Q.top();
    Q.pop();
    used[v.pos][v.b] = true;
    if (v.pos == H) {
      printf("%d\\n", v.total);
      return;
    }
    for (int i = 0; i < G[v.pos].size(); i++) {
      edge &e = G[v.pos][i];
      int tb = v.b - e.cost;
      if (tb < 0)
        continue;
      if (used[e.to][tb])
        continue;
      Q.push(visit(e.to, v.total + e.cost, tb));
    }
    if (!ice[v.pos])
      continue;
    for (int i = v.b + 1; i <= M; i++) {
      if (used[v.pos][i])
        continue;
      Q.push(visit(v.pos, v.total + i - v.b, i));
    }
  }
  puts("Help!");
}
int main() {
  while (scanf("%d%d%d%d%d%d", &N, &M, &L, &K, &A, &H), N || M) {
    fill_n(G, 100, vector<edge>(0));
    fill_n(ice, 100, false);
    for (int i = 0; i < L; i++) {
      int pos;
      scanf("%d", &pos);
      ice[pos] = true;
    }
    for (int i = 0; i < K; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      G[a].push_back(edge(b, c));
      G[b].push_back(edge(a, c));
    }

    solve();
  }
  return 0;
}