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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_M = 1225;
const int MAX_K = 10;
const int MAX_L = 7;
const int MAX_BITS = (1 << MAX_L);

const int INF = 1 << 30;

/* typedef */

typedef pair<int, int> pii;
typedef vector<pii> vpii;

struct Stat {
  int i, b, d;
  Stat(int _i, int _b, int _d) { i = _i, b = _b, d = _d; }
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int n, m, l, k;
int st, gl, gbits;
int bis[MAX_N];

vpii nbrs[MAX_N];
int dists[MAX_N][MAX_BITS][MAX_K];

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l >> k;
    if (n == 0)
      break;

    gbits = (1 << l) - 1;

    for (int i = 0; i < n; i++) {
      nbrs[i].clear();
      bis[i] = -1;
      for (int b = 0; b <= gbits; b++)
        for (int j = 0; j < k; j++)
          dists[i][b][j] = INF;
    }

    for (int i = 0; i < m; i++) {
      int u, v, c;
      cin >> u >> v >> c;
      u--, v--;
      nbrs[u].push_back(pii(v, c));
      nbrs[v].push_back(pii(u, c));
    }

    cin >> st >> gl;
    st--, gl--;

    for (int i = 0; i < l; i++) {
      int bi;
      cin >> bi;
      bi--;
      bis[bi] = i;
    }

    int sb = (bis[st] >= 0) ? (1 << bis[st]) : 0;

    priority_queue<Stat> q;
    q.push(Stat(st, sb, 0));
    dists[st][sb][0] = 0;

    int distk = -1;

    while (!q.empty()) {
      Stat u = q.top();
      q.pop();

      int udk = dists[u.i][u.b][k - 1];
      if (u.d > udk)
        continue;

      if (u.i == gl && u.b == gbits && u.d == udk) {
        distk = u.d;
        break;
      }

      vpii &nbru = nbrs[u.i];
      for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        int vi = vit->first;

        int vb = u.b;
        if (bis[vi] >= 0)
          vb |= (1 << bis[vi]);

        int vd = u.d + vit->second;
        int *dpt = dists[vi][vb];

        if (dpt[k - 1] > vd) {
          dpt[k - 1] = vd;
          for (int ki = k - 1; ki > 0 && dpt[ki] < dpt[ki - 1]; ki--) {
            int tmp = dpt[ki];
            dpt[ki] = dpt[ki - 1];
            dpt[ki - 1] = tmp;
          }
          q.push(Stat(vi, vb, vd));
        }
      }
    }

    if (distk < 0)
      cout << "NA" << endl;
    else
      cout << distk << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_M = 1225;
const int MAX_L = 7;
const int MAXBITS = (1 << MAX_L);

const int INF = 1 << 30;

/* typedef */

typedef pair<int, int> pii;
typedef vector<pii> vpii;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];

  UFT() {}

  void clear(int n) {
    for (int i = 0; i < n; i++)
      links[i] = i, ranks[i] = sizes[i] = 1;
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0)
      i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1);
    int mr;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    } else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    } else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

struct Stat {
  int i, b, d;
  Stat(int _i, int _b, int _d) { i = _i, b = _b, d = _d; }
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int n, m, l, k;
int st, gl, gbits;
int bis[MAX_N];

UFT uft;
vpii nbrs[MAX_N];

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l >> k;
    if (n == 0)
      break;

    gbits = (1 << l) - 1;

    for (int i = 0; i < n; i++) {
      nbrs[i].clear();
      bis[i] = -1;
    }

    uft.clear(n);

    for (int i = 0; i < m; i++) {
      int u, v, c;
      cin >> u >> v >> c;
      u--, v--;
      nbrs[u].push_back(pii(v, c));
      nbrs[v].push_back(pii(u, c));
      uft.merge(u, v);
    }

    cin >> st >> gl;
    st--, gl--;

    for (int i = 0; i < l; i++) {
      int b;
      cin >> b;
      b--;
      bis[b] = i;
    }

    if (uft.root(st) != uft.root(gl)) {
      cout << "NA" << endl;
      continue;
    }

    int sb = (bis[st] >= 0) ? (1 << bis[st]) : 0;

    priority_queue<Stat> q;
    q.push(Stat(st, sb, 0));

    int distk = -1;

    while (!q.empty()) {
      Stat u = q.top();
      q.pop();

      if (u.i == gl && u.b == gbits)
        if (--k == 0) {
          distk = u.d;
          break;
        }

      vpii &nbru = nbrs[u.i];
      for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        int vi = vit->first;
        int vc = vit->second;

        int vb = u.b;
        int bi = bis[vi];
        if (bi >= 0)
          vb |= (1 << bi);

        int vd = u.d + vc;

        q.push(Stat(vi, vb, vd));
      }
    }

    cout << distk << endl;
  }

  return 0;
}