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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct P {
  int v, time, cur;
  P(int a, int b, int c) {
    v = a;
    time = b;
    cur = c;
  }
  bool operator<(const P &a) const { return v < a.v; }
  bool operator>(const P &a) const { return v > a.v; }
};

struct Edge {
  int to, cost;
  Edge(int t, int c) {
    to = t;
    cost = c;
  }
};

int N, M, L, K, A, H;
vector<Edge> G[128];
int dist[128][128];
int frez[128];

void dijkstra() {
  rep(i, M + 1) fill(dist[i], dist[i] + N, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  dist[M][A] = 0;
  q.push(P(0, M, A));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    //    cout << p.v << " " << p.time << " " << p.cur << endl;
    if (p.v > dist[p.time][p.cur])
      continue;
    //    cout << p.v << " " << p.time << " " << p.cur << endl;
    //    cout << G[23].size() << endl;
    //    cout << "data " << p.cur << " " << G[p.cur].size() << endl;
    rep(i, G[p.cur].size()) {
      //      cout << G[1].size() << endl;
      //      cout << G[p.cur][0].to<< endl;
      Edge e = G[p.cur][i];
      //      cout << p.cur << " " << i << " " << G[p.cur].size() << endl;
      //      cout << G[1].size() << endl;
      //      cout << e.to << " " << e.cost << endl;
      //      cout << G[p.cur][i].to << " " << G[p.cur][i].cost << endl;
      //      cout << p.time-e.cost << " " << e.to << " " <<
      //      dist[p.time-e.cost][e.to]  << " " << e.cost << " " <<
      //      dist[p.time][p.cur] << endl; cout << G[1].size() << endl; cout <<
      //      p.time-e.cost << " " << p.time << " " << p.cur << " " <<
      //      dist[p.time][p.cur] << endl; cout << p.time-e.cost << endl; cout
      //      << G[1].size() << endl;
      if (p.time - e.cost >= 0 &&
          dist[p.time - e.cost][e.to] > dist[p.time][p.cur] + e.cost) {
        //	cout << e.to << " " << e.cost << endl;
        //	cout << G[1].size() << endl;
        int tmp = dist[p.time][p.cur] + e.cost;
        //	cout << G[23].size() << endl;
        //	cout << p.time-e.cost << " "<< e.to << " " << tmp << endl;
        //	cout << &dist[p.time-e.cost][e.to]  << endl;
        //	cout << &G[23] << endl;
        dist[p.time - e.cost][e.to] = tmp; // dist[p.time][p.cur] + e.cost;
        //	cout << G[23].size() << endl;
        q.push(P(dist[p.time - e.cost][e.to], p.time - e.cost, e.to));
        //	cout << G[1].size() << endl;
      }
      //      cout << G[1].size() << endl;
      //      cout << "ok\\n";
      //      cout << p.cur << " " << frez[p.cur] << endl;
      //      cout << G[1].size() << endl;
      if (frez[p.cur]) {
        for (int r = 1; r <= M - p.time; r++) {
          //	  cout << G[1].size() << endl;
          if (p.time - e.cost + r >= 0 &&
              dist[p.time - e.cost + r][e.to] >
                  dist[p.time][p.cur] + e.cost + r) {
            //	    cout << p.time-e.cost+r << " "  << e.to << endl;
            dist[p.time - e.cost + r][e.to] = dist[p.time][p.cur] + e.cost + r;
            q.push(
                P(dist[p.time - e.cost + r][e.to], p.time - e.cost + r, e.to));
            //	    cout << dist[p.time-e.cost+r][e.to] << " " <<
            //p.time-e.cost+r << " " << e.to << endl;
          }
        }
      }
      //      cout << e.to << " " << e.cost << endl;
      //      cout << G[1].size() << endl;
    }
  }
}

int main() {
  while (cin >> N >> M >> L >> K >> A >> H, N + M + L + K + A + H) {
    memset(frez, 0, sizeof(frez));
    rep(i, 128) G[i].clear();
    frez[A] = frez[H] = 1;
    rep(i, L) {
      int x;
      cin >> x;
      frez[x] = 1;
    }
    rep(i, K) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].push_back(Edge(y, t));
      G[y].push_back(Edge(x, t));
    }

    //    rep(i,N){
    //      rep(j,G[i].size()) cout << G[i][j].to << " " ; cout << endl;
    //    }
    //    cout << G[1].size() << endl;
    //    cout << G[1][8].to << " " << G[1][8].cost << endl;
    dijkstra();
    int res = INF;
    rep(i, M + 1) {
      res = min(res, dist[i][H]);
      //      cout << dist[i][H] << endl;
    }
    if (res == INF)
      cout << "Help!\\n";
    else
      cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct P {
  int v, time, cur;
  P(int a, int b, int c) {
    v = a;
    time = b;
    cur = c;
  }
  bool operator<(const P &a) const { return v < a.v; }
  bool operator>(const P &a) const { return v > a.v; }
};

struct Edge {
  int to, cost;
  Edge(int t, int c) {
    to = t;
    cost = c;
  }
};

int N, M, L, K, A, H;
vector<Edge> G[128];
int dist[128][128];
int frez[128];

void dijkstra() {
  rep(i, M + 1) fill(dist[i], dist[i] + N, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  dist[M][A] = 0;
  q.push(P(0, M, A));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    //    cout << p.v << " " << p.time << " " << p.cur << endl;// " " <<
    //    dist[p.time][p.cur] << endl;
    if (p.v > dist[p.time][p.cur])
      continue;
    //    cout << p.v << " " << p.time << " " << p.cur << endl;
    rep(i, G[p.cur].size()) {
      Edge e = G[p.cur][i];
      //      cout << e.to << " " << e.cost << endl;
      //      cout << p.time-e.cost << " " << e.to << " " <<
      //      dist[p.time-e.cost][e.to]  << " " << e.cost << " " <<
      //      dist[p.time][p.cur] << endl;
      if (dist[p.time - e.cost][e.to] > dist[p.time][p.cur] + e.cost) {
        dist[p.time - e.cost][e.to] = dist[p.time][p.cur] + e.cost;
        q.push(P(dist[p.time - e.cost][e.to], p.time - e.cost, e.to));
      }
      if (frez[p.cur]) {
        for (int r = 1; r <= M - p.time; r++) {
          if (dist[p.time - e.cost + r][e.to] >
              dist[p.time][p.cur] + e.cost + r) {
            dist[p.time - e.cost + r][e.to] = dist[p.time][p.cur] + e.cost + r;
            q.push(
                P(dist[p.time - e.cost + r][e.to], p.time - e.cost + r, e.to));
            //	    cout << dist[p.time-e.cost+r][e.to] << " " <<
            //p.time-e.cost+r << " " << e.to << endl;
          }
        }
      }
    }
  }
}

int main() {
  while (cin >> N >> M >> L >> K >> A >> H, N + M + L + K + A + H) {
    memset(frez, 0, sizeof(frez));
    rep(i, 128) G[i].clear();
    frez[A] = frez[H] = 1;
    rep(i, L) {
      int x;
      cin >> x;
      frez[x] = 1;
    }
    rep(i, K) {
      int x, y, t;
      cin >> x >> y >> t;
      G[x].push_back(Edge(y, t));
      G[y].push_back(Edge(x, t));
    }

    dijkstra();
    int res = INF;
    rep(i, M + 1) {
      res = min(res, dist[i][H]);
      //      cout << dist[i][H] << endl;
    }
    if (res == INF)
      cout << "Help!\\n";
    else
      cout << res << endl;
  }
  return 0;
}