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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void chmin(int &x, int v) {
  if (x > v)
    x = v;
}

int N, M;
vector<int> G[1000], D1[1000], D2[1000];
int dp[1000][1000][2];

int in[1000];
int ord[1000], rev[1000];
void tsort() {
  queue<int> q;
  rep(i, N) for (int t : G[i]) in[t]++;
  rep(i, N) if (in[i] == 0) q.push(i);

  int k = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    rev[k] = x, ord[x] = k++;
    for (int t : G[x]) {
      if (--in[t] == 0)
        q.push(t);
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int a, b, t1, t2;
    cin >> a >> b >> t1 >> t2;
    a--, b--;
    G[a].pb(b);
    D1[a].pb(t1);
    D2[a].pb(t2);
  }
  tsort();
  assert(rev[0] == 0 && rev[N - 1] == N - 1);

  rep(i, N) rep(j, N) rep(k, 2) dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  for (int b = 0; b < N; b++) {
    for (int a = 0; a <= b; a++) {
      rep(k, 2) {
        if (dp[a][b][k] == INF)
          continue;
        int x = rev[a];
        rep(i, G[x].size()) {
          int t = ord[G[x][i]], d = (k == 1 && t == b) ? D2[x][i] : D1[x][i];
          chmin(dp[min(t, b)][max(t, b)][a == b], dp[a][b][k] + d);
        }
      }
    }
  }
  int m = min(dp[N - 1][N - 1][0], dp[N - 1][N - 1][1]);
  cout << m << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
inline void chmin(int &x, int v) {
  if (x > v)
    x = v;
}

int N, M;
vector<int> G[1000], D1[1000], D2[1000];
int dp[1000][1000][2];

int in[1000];
int ord[1000], rev[1000];
void tsort() {
  queue<int> q;
  rep(i, N) for (int t : G[i]) in[t]++;
  rep(i, N) if (in[i] == 0) q.push(i);

  int k = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    rev[k] = x, ord[x] = k++;
    for (int t : G[x]) {
      if (--in[t] == 0)
        q.push(t);
    }
  }
  assert(k == N);
  rep(i, N) assert(rev[ord[i]] == i);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int a, b, t1, t2;
    cin >> a >> b >> t1 >> t2;
    a--, b--;
    G[a].pb(b);
    D1[a].pb(t1);
    D2[a].pb(t2);
  }
  tsort();
  assert(rev[0] == 0 && rev[N - 1] == N - 1);

  rep(i, N) rep(j, N) rep(k, 2) dp[i][j][k] = INF;
  dp[0][0][0] = 0;

  for (int b = 0; b < N; b++) {
    for (int a = 0; a <= b; a++) {
      rep(k, 2) {
        if (dp[a][b][k] == INF)
          continue;
        int x = ord[a];
        rep(i, G[x].size()) {
          int t = rev[G[x][i]], d = (k == 1 && t == b) ? D2[x][i] : D1[x][i];
          assert(t > a);
          chmin(dp[min(t, b)][max(t, b)][a == b], dp[a][b][k] + d);
        }
      }
    }
  }
  int m = min(dp[N - 1][N - 1][0], dp[N - 1][N - 1][1]);
  assert(m != INF);
  cout << m << "\\n";
  return 0;
}