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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 10000000000000000LL

struct state {
  int v, xcnt;
  ll dist;
};
bool operator<(const state &a, const state &b) { return a.dist > b.dist; }
struct func {
  ll a, b, l, r;
};

int N, M, S[2], T;
vector<P> g[1001];
ll d[1001][110];
vector<func> fs[2];
int sz[2];
bool dame[2][101];

int main() {
  while (1) {
    cin >> N >> M >> S[0] >> S[1] >> T;
    if (N == 0)
      break;
    rep(i, N) g[i].clear();
    fs[0].clear();
    fs[1].clear();
    S[0]--;
    S[1]--;
    T--;
    rep(i, M) {
      int a, b;
      string c;
      cin >> a >> b >> c;
      a--;
      b--;
      if (c == "x") {
        g[a].push_back(P(b, -1));
        g[b].push_back(P(a, -1));
      } else {
        g[a].push_back(P(b, stoi(c)));
        g[b].push_back(P(a, stoi(c)));
      }
    }
    rep(s, 2) {
      rep(i, N) rep(j, 110) d[i][j] = INF;
      priority_queue<state> que;
      que.push((state){S[s], 0, 0});
      while (que.size()) {
        state crt = que.top();
        que.pop();
        if (d[crt.v][crt.xcnt] != INF)
          continue;
        d[crt.v][crt.xcnt] = crt.dist;
        for (P p : g[crt.v]) {
          if (p.se == -1 && crt.xcnt < 100) {
            que.push((state){(int)p.fi, crt.xcnt + 1, crt.dist});
          } else if (p.se != -1) {
            que.push((state){(int)p.fi, crt.xcnt, crt.dist + p.se});
          }
        }
      }
      rep(j, 101) if (d[T][j] != INF) fs[s].push_back(
          (func){j, d[T][j], 0LL, INF});
      sz[s] = fs[s].size();
      rep(i, sz[s]) dame[s][i] = false;
      rep(i, sz[s]) rep(j, sz[s]) {
        if (i == j)
          continue;
        if (fs[s][i].a >= fs[s][j].a && fs[s][i].b >= fs[s][j].b)
          dame[s][i] = true;
      }
      rep(i, sz[s]) rep(j, sz[s]) {
        if (i == j || dame[s][i] || dame[s][j])
          continue;
        if (fs[s][i].a < fs[s][j].a) {
          assert(fs[s][i].b - fs[s][j].b > 0);
          maxch(fs[s][i].l,
                (fs[s][i].b - fs[s][j].b + fs[s][j].a - fs[s][i].a - 1) /
                    (fs[s][j].a - fs[s][i].a));
        } else {
          assert(fs[s][j].b - fs[s][i].b > 0);
          minch(fs[s][i].r,
                (fs[s][j].b - fs[s][i].b) / (fs[s][i].a - fs[s][j].a));
        }
      }
    }
    ll res = INF;
    rep(i, sz[0]) rep(j, sz[1]) {
      if (dame[0][i] || dame[1][j])
        continue;
      ll l = max(fs[0][i].l, fs[1][j].l);
      ll r = min(fs[0][i].r, fs[1][j].r);
      if (l > r)
        continue;
      ll a = fs[0][i].a - fs[1][j].a;
      ll b = fs[0][i].b - fs[1][j].b;
      if (a == 0) {
        minch(res, abs(b));
        continue;
      }
      if (a < 0) {
        a = -a;
        b = -b;
      }
      if (b >= 0) {
        minch(res, abs(a * l + b));
        continue;
      }
      ll mn1 = (-b) / a, mn2 = (-b + a - 1) / a;
      if (l <= mn1 && mn1 <= r)
        minch(res, abs(a * mn1 + b));
      if (l <= mn2 && mn2 <= r)
        minch(res, abs(a * mn2 + b));
      minch(res, abs(a * l + b));
      minch(res, abs(a * r + b));
    }
    cout << res << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 10000000000000000LL

struct state {
  int v, xcnt;
  ll dist;
};
bool operator<(const state &a, const state &b) { return a.dist > b.dist; }
struct func {
  ll a, b, l, r;
};

int N, M, S[2], T;
vector<P> g[1001];
ll d[1001][101];
vector<func> fs[2];
int sz[2];
bool dame[2][101];

int main() {
  while (1) {
    cin >> N >> M >> S[0] >> S[1] >> T;
    if (N == 0)
      break;
    rep(i, N) g[i].clear();
    fs[0].clear();
    fs[1].clear();
    S[0]--;
    S[1]--;
    T--;
    rep(i, M) {
      int a, b;
      string c;
      cin >> a >> b >> c;
      a--;
      b--;
      if (c == "x") {
        g[a].push_back(P(b, -1));
        g[b].push_back(P(a, -1));
      } else {
        g[a].push_back(P(b, stoi(c)));
        g[b].push_back(P(a, stoi(c)));
      }
    }
    rep(s, 2) {
      rep(i, N) rep(j, 101) d[i][j] = INF;
      priority_queue<state> que;
      que.push((state){S[s], 0, 0});
      while (que.size()) {
        state crt = que.top();
        que.pop();
        if (d[crt.v][crt.xcnt] != INF)
          continue;
        d[crt.v][crt.xcnt] = crt.dist;
        for (P p : g[crt.v]) {
          if (p.se == -1) {
            que.push((state){(int)p.fi, crt.xcnt + 1, crt.dist});
          } else {
            que.push((state){(int)p.fi, crt.xcnt, crt.dist + p.se});
          }
        }
      }
      rep(j, 101) if (d[T][j] != INF) fs[s].push_back(
          (func){j, d[T][j], 0LL, INF});
      sz[s] = fs[s].size();
      rep(i, sz[s]) dame[s][i] = false;
      rep(i, sz[s]) rep(j, sz[s]) {
        if (i == j)
          continue;
        if (fs[s][i].a >= fs[s][j].a && fs[s][i].b >= fs[s][j].b)
          dame[s][i] = true;
      }
      rep(i, sz[s]) rep(j, sz[s]) {
        if (i == j || dame[s][i] || dame[s][j])
          continue;
        if (fs[s][i].a < fs[s][j].a) {
          maxch(fs[s][i].l,
                (fs[s][i].b - fs[s][j].b + fs[s][j].a - fs[s][i].a - 1) /
                    (fs[s][j].a - fs[s][i].a));
        } else {
          minch(fs[s][i].r,
                (fs[s][j].b - fs[s][i].b) / (fs[s][i].a - fs[s][j].a));
        }
      }
    }
    ll res = INF;
    rep(i, sz[0]) rep(j, sz[1]) {
      if (dame[0][i] || dame[1][j])
        continue;
      ll l = max(fs[0][i].l, fs[1][j].l);
      ll r = min(fs[0][i].r, fs[1][j].r);
      if (l > r)
        continue;
      ll a = fs[0][i].a - fs[1][j].a;
      ll b = fs[0][i].b - fs[1][j].b;
      if (a == 0) {
        minch(res, abs(b));
        continue;
      }
      if (a < 0) {
        a = -a;
        b = -b;
      }
      ll mn1 = (-b) / a, mn2 = (-b + a - 1) / a;
      if (l <= mn1 && mn1 <= r)
        minch(res, abs(a * mn1 + b));
      if (l <= mn2 && mn2 <= r)
        minch(res, abs(a * mn2 + b));
      minch(res, abs(a * l + b));
      minch(res, abs(a * r + b));
    }
    cout << res << endl;
  }
  return 0;
}