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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

const int N = 55;

int d[N][N];
vector<P> G[N];
int ball[N];

priority_queue<int> cand[N][1 << 7];

struct state {
  int pos, visit, total;
  bool operator<(const state &r) const { return total > r.total; }
};

int main() {
  ios::sync_with_stdio(false);

  int n, m, l, k;
  while (cin >> n >> m >> l >> k, n | m | l | k) {
    rep(i, N) rep(j, N) d[i][j] = 1 << 29;
    rep(i, N) rep(j, 1 << 7) while (!cand[i][j].empty()) cand[i][j].pop();
    rep(i, N) G[i].clear();
    MEMSET(ball, 0);
    rep(i, m) {
      int u, v, c;
      cin >> u >> v >> c;
      --u, --v;
      d[u][v] = d[v][u] = c;
      G[u].push_back(MP(v, c));
      G[v].push_back(MP(u, c));
    }
    int s, g;
    cin >> s >> g;
    --s, --g;
    rep(i, l) {
      int b;
      cin >> b;
      ball[--b] = 1 << i;
    }
    rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

    priority_queue<state> q;
    q.push({s, ball[s], 0});

    int cnt = 0, ans = -1;
    if (d[s][g] >= 1 << 29)
      goto fail;
    while (!q.empty()) {
      auto s = q.top();
      q.pop();

      auto &pq = cand[s.pos][s.visit];
      if (!pq.empty() && pq.top() < s.total)
        continue;

      if (s.pos == g && s.visit + 1 == (1 << l)) {
        // cout << s.total << endl;
        if (++cnt == k) {
          ans = s.total;
          break;
        }
      }

      for (auto e : G[s.pos]) {
        state nxt = s;
        nxt.pos = e.first;
        nxt.total += e.second;
        nxt.visit |= ball[nxt.pos];
        auto &c = cand[nxt.pos][nxt.visit];
        if (c.size() < k || nxt.total < c.top()) {
          q.push(nxt);
          c.push(nxt.total);
          if (c.size() > k)
            c.pop();
        }
      }
    }

  fail:;

    if (ans >= 0)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define MP make_pair
#define umap unordered_map

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> P;
typedef unsigned int uint;

const int N = 55;

int d[N][N];
vector<P> G[N];
int ball[N];
int bpos[N];

priority_queue<int> cand[N][1 << 7];

struct state {
  int pos, visit, total;
  bool operator<(const state &r) const { return total > r.total; }
};

int main() {
  ios::sync_with_stdio(false);

  int n, m, l, k;
  while (cin >> n >> m >> l >> k, n | m | l | k) {
    rep(i, N) rep(j, N) d[i][j] = 1 << 29;
    rep(i, N) rep(j, 1 << 7) while (!cand[i][j].empty()) cand[i][j].pop();
    rep(i, N) G[i].clear();
    MEMSET(ball, 0);
    MEMSET(bpos, 0);
    rep(i, m) {
      int u, v, c;
      cin >> u >> v >> c;
      --u, --v;
      d[u][v] = d[v][u] = c;
      G[u].push_back(MP(v, c));
      G[v].push_back(MP(u, c));
    }
    int s, g;
    cin >> s >> g;
    --s, --g;
    rep(i, l) {
      int b;
      cin >> b;
      ball[--b] = 1 << i;
      bpos[i] = b;
    }
    rep(i, n) rep(j, n) rep(k, n) d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

    priority_queue<state> q;
    q.push({s, 0, 0});

    int cnt = 0, ans = -1;
    if (d[s][g] >= 1 << 29)
      goto fail;
    while (!q.empty()) {
      auto s = q.top();
      q.pop();

      s.visit |= ball[s.pos];
      auto &pq = cand[s.pos][s.visit];
      if (pq.size() && pq.top() < s.total)
        continue;

      if (s.pos == g && s.visit + 1 == (1 << l)) {
        // cout << s.total << endl;
        if (++cnt == k) {
          ans = s.total;
          break;
        }
      }

      for (auto e : G[s.pos]) {
        state nxt = s;
        nxt.pos = e.first;
        nxt.total += e.second;
        auto &c = cand[nxt.pos][nxt.visit];
        if (c.size() < k || c.top() < nxt.total) {
          c.push(nxt.total);
          if (c.size() > k)
            c.pop();
        }
        q.push(nxt);
      }
    }

  fail:;
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}