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

#include <algorithm>
#include <cstdio>
#include <cstring>

const int N = 50 + 1, S = 1 << 10, INF = 0x3f3f3f3f;

inline bool check(int &lhs, int rhs) {
  if (rhs < lhs)
    return lhs = rhs, true;
  else
    return false;
}

int n, m;

struct Info {
  int d, e, id;
  Info() {}
  Info(int _d, int _e, int _id) : d(_d), e(_e), id(_id) {}
  inline bool operator<(const Info &rhs) const { return e < rhs.e; }
} info[N];

int go[N][N], ret[N][N];

int f[N][N];

struct State {
  int x, y;
  State() {}
  State(int _x, int _y) : x(_x), y(_y) {}
};

void trans(int l, int r) {
  static int g[N][N][S];
  int tot = 1 << (r - l);
  for (int i = l; i < r; ++i)
    for (int j = l; j < r; ++j)
      for (int k = 0; k < tot; ++k)
        g[i][j][k] = INF;
  for (int i = l; i < r; ++i)
    for (int j = l; j < r; ++j)
      check(g[i][j][(1 << (i - l)) | ((1 << (j - l)))], f[i][j]);
  for (int s = 0; s < tot; ++s) {
    static State q[10 * N * N * S];
    static int tag[N][N], t;
    ++t;
    int cnt = 0;
    for (int i = l; i < r; ++i)
      for (int j = l; j < r; ++j)
        if (g[i][j][s] < INF)
          q[++cnt] = State(i, j), tag[i][j] = t;
    for (int i = 1; i <= cnt; ++i) {
      int x = q[i].x, y = q[i].y;
      tag[x][y] = 0;
      for (int a = l; a < r; ++a)
        if ((s >> (a - l) & 1) && check(g[a][y][s], g[x][y][s] + go[x][a]) &&
            tag[a][y] != t)
          tag[a][y] = t, q[++cnt] = State(a, y);
      for (int b = l; b < r; ++b)
        if ((s >> (b - l) & 1) && check(g[x][b][s], g[x][y][s] + ret[y][b]) &&
            tag[x][b] != t)
          tag[x][b] = t, q[++cnt] = State(x, b);
    }
    for (int i = l; i < r; ++i) {
      for (int j = l; j < r; ++j) {
        check(f[i][j], g[i][j][s]);
        for (int a = l; a < r; ++a)
          if (!(s >> (a - l) & 1))
            check(g[a][j][s | (1 << (a - l))],
                  g[i][j][s] + go[i][a] + info[a].d);
        for (int b = l; b < r; ++b)
          if (!(s >> (b - l) & 1))
            check(g[i][b][s | (1 << (b - l))],
                  g[i][j][s] + ret[j][b] + info[b].d);
      }
    }
  }
}

int main() {
  while (scanf("%d%d", &n, &m), n || m) {
    info[1] = Info(0, 0, 1);
    info[n] = Info(0, 1000, n);
    for (int i = 2; i < n; ++i)
      scanf("%d%d", &info[i].d, &info[i].e), info[i].id = i;
    static int pos[N];
    std::sort(info + 1, info + n + 1);
    for (int i = 1; i <= n; ++i)
      pos[info[i].id] = i;
    memset(go, 0x3f, sizeof go);
    memset(ret, 0x3f, sizeof ret);
    while (m--) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      a = pos[a], b = pos[b];
      if (info[a].e <= info[b].e)
        check(go[a][b], c);
      if (info[a].e >= info[b].e)
        check(ret[b][a], c);
    }
    memset(f, 0x3f, sizeof f);
    f[1][1] = 0;
    for (int i = 1, l = 1; i <= n; ++i) {
      if (i == l) {
        while (l <= n && info[l].e == info[i].e)
          ++l;
        trans(i, l);
      }
      for (int j = 1; j <= n; ++j) {
        int cur = f[i][j];
        if (cur == INF)
          continue;
        for (int k = i + 1; k <= n; ++k)
          check(f[k][j], cur + go[i][k] + (k != j ? info[k].d : 0));
        for (int k = j + 1; k <= n; ++k)
          check(f[i][k], cur + ret[j][k] + (k != i ? info[k].d : 0));
      }
    }
    printf("%d\\n", f[n][n] < INF ? f[n][n] : -1);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define scf scanf
#define ptf printf
#define forp(i, j, k) for (int i = j; i < k; i++)
#define form(i, j, k) for (int i = j; i > k; i--)
#define sz(x) (int)x.size()
#define pb push_back
#define fst first
#define scd second
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;

const int oo = 1e8;
const int NN = 4e3 + 10, N = 55;

struct node {
  int x, y;
  node() {}
  node(int _x, int _y) {
    x = _x;
    y = _y;
  }
};

vector<node> e1[N], e2[N];
int q[NN], dis1[N][N], dis2[N][N], inq[N], e[N], d[N], n;
set<LL> path1[N][N], path2[N][N];

void spfa1(int S) {
  memset(inq, 0, sizeof(inq));
  forp(i, 0, n) dis1[S][i] = oo;
  forp(i, 0, n) path1[S][i].clear();
  dis1[S][S] = d[S];
  inq[S] = 1;
  q[0] = S;
  path1[S][S].insert(1 << S);
  int maxn = 3000, u, v, w;
  for (int l = 0, r = 1; l != r; l = (l + 1) % maxn) {
    u = q[l];
    for (vector<node>::iterator it = e1[u].begin(); it != e1[u].end(); it++) {
      v = it->x;
      w = it->y;
      if (dis1[S][u] + w + d[v] == dis1[S][v]) {
        for (set<LL>::iterator it = path1[S][u].begin();
             it != path1[S][u].end(); it++)
          path1[S][v].insert((*it) | (1LL << v));
        if (!inq[v]) {
          inq[v] = 1;
          q[r] = v;
          r = (r + 1) % maxn;
        }
      }
      if (dis1[S][u] + w + d[v] < dis1[S][v]) {
        dis1[S][v] = dis1[S][u] + w + d[v];
        path1[S][v].clear();
        for (set<LL>::iterator it = path1[S][u].begin();
             it != path1[S][u].end(); it++)
          path1[S][v].insert((*it) | (1LL << v));
        if (!inq[v]) {
          inq[v] = 1;
          q[r] = v;
          r = (r + 1) % maxn;
        }
      }
    }
  }
}

void spfa2(int S) {
  memset(inq, 0, sizeof(inq));
  forp(i, 0, n) dis2[S][i] = oo;
  forp(i, 0, n) path2[S][i].clear();
  dis2[S][S] = d[S];
  inq[S] = 1;
  q[0] = S;
  path2[S][S].insert(1 << S);
  int maxn = 3000, u, v, w;
  for (int l = 0, r = 1; l != r; l = (l + 1) % maxn) {
    u = q[l];
    for (vector<node>::iterator it = e2[u].begin(); it != e2[u].end(); it++) {
      v = it->x;
      w = it->y;
      if (dis2[S][u] + w + d[v] == dis2[S][v]) {
        for (set<LL>::iterator it = path2[S][u].begin();
             it != path2[S][u].end(); it++)
          path2[S][v].insert((*it) | (1LL << v));
        if (!inq[v]) {
          inq[v] = 1;
          q[r] = v;
          r = (r + 1) % maxn;
        }
      }
      if (dis2[S][u] + w + d[v] < dis2[S][v]) {
        dis2[S][v] = dis2[S][u] + w + d[v];
        path1[S][v].clear();
        for (set<LL>::iterator it = path2[S][u].begin();
             it != path2[S][u].end(); it++)
          path2[S][v].insert((*it) | (1LL << v));
        if (!inq[v]) {
          inq[v] = 1;
          q[r] = v;
          r = (r + 1) % maxn;
        }
      }
    }
  }
}

int ans[N][N];

void floyed() {
  LL st;
  forp(i, 0, n) forp(j, 0, n) {
    ans[i][j] = dis1[i][j] + dis2[j][i];
    for (set<LL>::iterator it1 = path1[i][j].begin(); it1 != path1[i][j].end();
         it1++)
      for (set<LL>::iterator it2 = path2[j][i].begin();
           it2 != path2[j][i].end(); it2++) {
        st = (*it1) & (*it2);
        forp(k, 0, n) if (st & (1 << k)) ans[i][j] -= d[k];
      }
  }
  forp(i, 0, n) forp(j, 0, n) forp(k, 0, n) if (j != k) ans[j][k] =
      min(ans[j][k], ans[j][i] + ans[i][k] - d[i]);
}

int main() {
  int m, x, y, z;
  for (scf("%d%d", &n, &m); n || m; scf("%d%d", &n, &m)) {
    forp(i, 1, n - 1) scf("%d%d", d + i, e + i);
    e[0] = 0;
    e[n - 1] = 1000;
    forp(i, 0, n) {
      e1[i].clear();
      e2[i].clear();
    }
    forp(i, 0, m) {
      scf("%d%d%d", &x, &y, &z);
      x--;
      y--;
      if (e[x] <= e[y])
        e1[x].pb(node(y, z));
      if (e[x] >= e[y])
        e2[x].pb(node(y, z));
    }
    forp(i, 0, n) {
      spfa1(i);
      spfa2(i);
    }
    floyed();
    if (ans[0][n - 1] >= oo)
      puts("-1");
    else
      ptf("%d\\n", ans[0][n - 1]);
  }
  return 0;
}

/*
3 6
3 1
1 2 1
2 3 1
3 2 1
2 1 1
1 3 4
3 1 4
3 6
5 1
1 2 1
2 3 1
3 2 1
2 1 1
1 3 4
3 1 4
4 5
3 1
3 1
1 2 5
2 3 5
3 4 5
4 2 5
3 1 5
2 1
2 1 1
0 0
*/