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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int dp[1 << 11][55][55];
struct beet {
  Int b, x, y, c;
  beet(int b, int x, int y, int c) : b(b), x(x), y(y), c(c) {}
  bool operator<(const beet &a) const { return c > a.c; }
};
signed main() {
  Int INF = 1e9;
  Int n, m;
  while (cin >> n >> m, n) {
    // cout<<n<<" "<<m<<endl;
    vector<Int> d(n, 0), e(n, 0);
    for (Int i = 1; i < n - 1; i++)
      cin >> d[i] >> e[i];
    e[n - 1] = 1000;
    map<Int, Int> sz;
    vector<Int> r(n);
    for (Int i = 0; i < n; i++)
      r[i] = sz[e[i]]++;

    vector<vector<Int>> G(n, vector<Int>(n, INF));
    for (Int i = 0; i < m; i++) {
      Int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      G[a][b] = c;
    }

    for (Int i = 0; i < (1 << 11); i++)
      for (Int j = 0; j < 55; j++)
        for (Int k = 0; k < 55; k++)
          dp[i][j][k] = INF;

    priority_queue<beet> q;
    q.push(beet(1 << r[0], 0, 0, 0));
    dp[1 << r[0]][0][0] = 0;

    Int ans = INF;

    while (!q.empty()) {
      beet ei = q.top();
      q.pop();
      Int b = ei.b, x = ei.x, y = ei.y, c = ei.c;
      if (dp[b][x][y] < c)
        continue;
      // cout<<b<<" "<<x<<" "<<y<<":"<<c<<endl;
      // dp[b][x][y]=c;
      if (x == n - 1 && y == n - 1 && c < ans) {
        ans = c;
        break;
      }
      if (e[x] <= e[y]) {
        for (Int i = 0; i < n; i++) {
          if (x == i || e[x] > e[i])
            continue;
          Int nb = b * (e[x] == e[i]);
          nb |= (e[i] >= e[y]) * (1 << r[y]);
          Int cost = G[x][i];
          if (e[i] <= e[y]) {
            if (!((nb >> r[i]) & 1))
              cost += d[i];
            nb |= 1 << r[i];
          } else
            cost += d[i];
          // cout<<nb<<" "<<i<<" "<<y<<"---"<<c+cost<<endl;
          if (dp[nb][i][y] <= c + cost)
            continue;
          q.push(beet(nb, i, y, c + cost));
          dp[nb][i][y] = c + cost;
        }
      }

      if (e[y] <= e[x]) {
        for (Int i = 0; i < n; i++) {
          if (y == i || e[y] > e[i])
            continue;
          Int nb = b * (e[y] == e[i]);
          nb |= (e[i] >= e[x]) * (1 << r[x]);
          Int cost = G[i][y];
          if (e[i] <= e[x]) {
            if (!((nb >> r[i]) & 1))
              cost += d[i];
            nb |= 1 << r[i];
          } else
            cost += d[i];
          // cout<<nb<<" "<<x<<" "<<i<<"---"<<c+cost<<endl;
          if (dp[nb][x][i] <= c + cost)
            continue;
          q.push(beet(nb, x, i, c + cost));
          dp[nb][x][i] = c + cost;
        }
      }
    }

    if (ans >= INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
Int dp[1 << 11][55][55];
struct beet {
  Int b, x, y, c;
  bool operator<(const beet &a) const { return c > a.c; }
};
signed main() {
  Int INF = 1e9;
  Int n, m;
  while (cin >> n >> m, n) {
    // cout<<n<<" "<<m<<endl;
    vector<Int> d(n, 0), e(n, 0);
    for (Int i = 1; i < n - 1; i++)
      cin >> d[i] >> e[i];
    e[n - 1] = 1000;
    map<Int, Int> sz;
    map<Int, Int> r;
    for (Int i = 0; i < n; i++)
      r[i] = sz[d[i]]++;

    vector<vector<Int>> G(n, vector<Int>(n, INF));
    for (Int i = 0; i < m; i++) {
      Int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      G[a][b] = c;
    }

    for (Int i = 0; i < (1 << 10); i++)
      for (Int j = 0; j < 50; j++)
        for (Int k = 0; k < 50; k++)
          dp[i][j][k] = INF;

    priority_queue<beet> q;
    q.push({1 << r[0], 0, 0, 0});

    while (!q.empty()) {
      beet ei = q.top();
      q.pop();
      Int b = ei.b, x = ei.x, y = ei.y, c = ei.c;
      if (dp[b][x][y] <= c)
        continue;
      // cout<<b<<" "<<x<<" "<<y<<":"<<c<<endl;
      dp[b][x][y] = c;
      if (e[x] <= e[y]) {
        for (Int i = 0; i < n; i++) {
          if (x == i || e[x] > e[i])
            continue;
          Int nb = b * (e[x] == e[i]);
          nb |= (e[i] == e[y]) * (1 << r[y]);
          Int cost = G[x][i];
          if (!((nb >> r[i]) & 1))
            cost += d[i];
          nb |= 1 << r[i];
          // cout<<i<<" "<<y<<":"<<c+cost<<endl;
          q.push({nb, i, y, c + cost});
        }
      }

      if (e[y] <= e[x]) {
        for (Int i = 0; i < n; i++) {
          if (y == i || e[y] > e[i])
            continue;
          Int nb = b * (e[y] == e[i]);
          nb |= (e[i] == e[x]) * (1 << r[x]);
          Int cost = G[i][y];
          if (!((nb >> r[i]) & 1))
            cost += d[i];
          nb |= 1 << r[i];
          // cout<<x<<" "<<i<<":"<<c+cost<<endl;
          q.push({nb, x, i, c + cost});
        }
      }
    }

    Int ans = INF;
    for (Int i = 0; i < (1 << 10); i++)
      ans = min(ans, dp[i][n - 1][n - 1]);
    if (ans >= INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}