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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;

int n, m, s1, s2, t;
vector<pl> e[1000];
ll dist[101][1000];
ll d[2][101];
pl range[2][101];

void solve() {
  rep(i, 1000) e[i].clear();
  s1--;
  s2--;
  t--;
  int b_cnt = 0;
  rep(i, m) {
    int a, b;
    string s;
    cin >> a >> b >> s;
    a--;
    b--;
    if (s == "x") {
      e[a].push_back(pl(b, -1));
      e[b].push_back(pl(a, -1));
      b_cnt++;
    } else {
      ll num = 0;
      rep(j, s.size()) { num = num * 10 + s[j] - '0'; }
      e[a].push_back(pl(b, num));
      e[b].push_back(pl(a, num));
    }
  }
  rep(i, 101) rep(j, n) dist[i][j] = linf;
  dist[0][t] = 0;
  priority_queue<pl, vector<pl>, greater<pl>> pque;
  pque.push(pl(0, t));
  while (!pque.empty()) {
    ll dis = pque.top().first, num = pque.top().second / n,
       pos = pque.top().second % n;
    pque.pop();
    if (dis > dist[num][pos])
      continue;
    rep(i, e[pos].size()) {
      pl E = e[pos][i];
      if (E.second == -1) {
        if (num != b_cnt && dist[num + 1][E.first] > dist[num][pos]) {
          dist[num + 1][E.first] = dist[num][pos];
          pque.push(pl(dist[num + 1][E.first], (num + 1) * n + E.first));
        }
      } else {
        if (dist[num][E.first] > dist[num][pos] + E.second) {
          dist[num][E.first] = dist[num][pos] + E.second;
          pque.push(pl(dist[num][E.first], num * n + E.first));
        }
      }
    }
  }
  b_cnt++;
  rep(i, b_cnt) d[0][i] = dist[i][s1];
  rep(i, b_cnt) d[1][i] = dist[i][s2];
  /*
  rep(i,2) {
      rep(j,b_cnt) {
          cerr << d[i][j] << " ";
      }
      cerr << endl;
  }
  */
  rep(i, 2) rep(j, b_cnt) range[i][j] = pl(0, inf * 1000LL);
  rep(i, 2) {
    rep(j, b_cnt) {
      if (d[i][j] == linf) {
        range[i][j] = pl(-1, -1);
        continue;
      }
      rep(k, b_cnt) {
        if (d[i][k] == linf)
          continue;
        if (j == k)
          continue;
        if (j < k) {
          if (d[i][j] <= d[i][k])
            continue;
          ll hoge = (d[i][j] - d[i][k] + k - j - 1) / (k - j);
          range[i][j].first = max(range[i][j].first, hoge);
        } else {
          if (d[i][j] >= d[i][k]) {
            range[i][j] = pl(-1, -1);
            break;
          }
          ll hoge = (d[i][k] - d[i][j] + j - k - 1) / (j - k) - 1;
          range[i][j].second = min(range[i][j].second, hoge);
        }
      }
    }
  }
  /*
  rep(i,2) {
      rep(j,b_cnt) {
          cerr << "(" << range[i][j].first << ", " << range[i][j].second << ")
  ";
      }
      cerr << endl;
  }
  */
  ll ans = linf;
  rep(i, b_cnt) {
    rep(j, b_cnt) {
      if (range[0][i].first == -1 || range[1][j].first == -1)
        continue;
      ll l = max(range[0][i].first, range[1][j].first),
         r = min(range[0][i].second, range[1][j].second);
      if (l > r)
        continue;
      bool flag = (d[0][i] + i * l >= d[1][j] + j * l);
      if (flag) {
        if (i >= j) {
          ans = min(ans, d[0][i] + i * l - d[1][j] - j * l);
          continue;
        }
        // cerr << "Ok" << endl;
        while (r - l > 1) {
          ll mid = (r + l) / 2;
          ll val1 = d[0][i] + i * mid;
          ll val2 = d[1][j] + j * mid;
          if (val1 >= val2)
            l = mid;
          else
            r = mid;
        }
        ans = min(ans, d[0][i] + i * l - d[1][j] - j * l);
        ans = min(ans, abs(d[0][i] + i * r - d[1][j] - j * r));
      } else {
        if (i <= j) {
          ans = min(ans, d[1][j] + j * l - d[0][i] - i * l);
          continue;
        }
        while (r - l > 1) {
          ll mid = (r + l) / 2;
          ll val1 = d[0][i] + i * mid;
          ll val2 = d[1][j] + j * mid;
          if (val1 < val2)
            l = mid;
          else
            r = mid;
        }
        ans = min(ans, d[1][j] + j * l - d[0][i] - i * l);
        ans = min(ans, abs(d[1][j] + j * r - d[0][i] - i * r));
      }
    }
  }
  cout << ans << endl;
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  freopen("in.txt", "rt", stdin);
#endif
  while (cin >> n >> m >> s1 >> s2 >> t) {
    if (n == 0 && m == 0 && s1 == 0 && s2 == 0 && t == 0)
      break;
    solve();
  }
  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;

int n, m, s1, s2, t;
vector<pl> e[1000];
ll dist[101][1000];
ll d[2][101];
pl range[2][101];

void solve() {
  rep(i, 1000) e[i].clear();
  s1--;
  s2--;
  t--;
  int b_cnt = 0;
  rep(i, m) {
    int a, b;
    string s;
    cin >> a >> b >> s;
    a--;
    b--;
    if (s == "x") {
      e[a].push_back(pl(b, -1));
      e[b].push_back(pl(a, -1));
      b_cnt++;
    } else {
      ll num = 0;
      rep(j, s.size()) { num = num * 10 + s[j] - '0'; }
      e[a].push_back(pl(b, num));
      e[b].push_back(pl(a, num));
    }
  }
  rep(i, n) rep(j, n) dist[i][j] = linf;
  dist[0][t] = 0;
  priority_queue<pl, vector<pl>, greater<pl>> pque;
  pque.push(pl(0, t));
  while (!pque.empty()) {
    ll dis = pque.top().first, num = pque.top().second / n,
       pos = pque.top().second % n;
    pque.pop();
    if (dis > dist[num][pos])
      continue;
    rep(i, e[pos].size()) {
      pl E = e[pos][i];
      if (E.second == -1) {
        if (num != b_cnt && dist[num + 1][E.first] > dist[num][pos]) {
          dist[num + 1][E.first] = dist[num][pos];
          pque.push(pl(dist[num + 1][E.first], (num + 1) * n + E.first));
        }
      } else {
        if (dist[num][E.first] > dist[num][pos] + E.second) {
          dist[num][E.first] = dist[num][pos] + E.second;
          pque.push(pl(dist[num][E.first], num * n + E.first));
        }
      }
    }
  }
  b_cnt++;
  rep(i, b_cnt) d[0][i] = dist[i][s1];
  rep(i, b_cnt) d[1][i] = dist[i][s2];
  /*
  rep(i,2) {
      rep(j,b_cnt) {
          cerr << d[i][j] << " ";
      }
      cerr << endl;
  }
  */
  rep(i, 2) rep(j, b_cnt) range[i][j] = pl(0, inf * 1000LL);
  rep(i, 2) {
    rep(j, b_cnt) {
      if (d[i][j] == linf) {
        range[i][j] = pl(-1, -1);
        continue;
      }
      rep(k, b_cnt) {
        if (d[i][k] == linf)
          continue;
        if (j == k)
          continue;
        if (j < k) {
          if (d[i][j] <= d[i][k])
            continue;
          ll hoge = (d[i][j] - d[i][k] + k - j - 1) / (k - j);
          range[i][j].first = max(range[i][j].first, hoge);
        } else {
          if (d[i][j] >= d[i][k]) {
            range[i][j] = pl(-1, -1);
            break;
          }
          ll hoge = (d[i][k] - d[i][j] + j - k - 1) / (j - k);
          range[i][j].second = min(range[i][j].second, hoge);
        }
      }
    }
  }
  /*
  rep(i,2) {
      rep(j,b_cnt) {
          cerr << "(" << range[i][j].first << ", " << range[i][j].second << ")
  ";
      }
      cerr << endl;
  }
  */
  ll ans = linf;
  rep(i, b_cnt) {
    rep(j, b_cnt) {
      if (range[0][i].first == -1 || range[1][j].first == -1)
        continue;
      ll l = max(range[0][i].first, range[1][j].first),
         r = min(range[0][i].second, range[1][j].second);
      if (l > r)
        continue;
      bool flag = (d[0][i] + i * l >= d[1][j] + j * l);
      if (flag) {
        if (i >= j) {
          ans = min(ans, d[0][i] + i * l - d[1][j] - j * l);
          continue;
        }
        // cerr << "Ok" << endl;
        while (r - l > 1) {
          ll mid = (r + l) / 2;
          ll val1 = d[0][i] + i * mid;
          ll val2 = d[1][j] + j * mid;
          if (val1 >= val2)
            l = mid;
          else
            r = mid;
        }
        ans = min(ans, d[0][i] + i * l - d[1][j] - j * l);
        ans = min(ans, abs(d[0][i] + i * r - d[1][j] - j * r));
      } else {
        if (i <= j) {
          ans = min(ans, d[1][j] + j * l - d[0][i] - i * l);
          continue;
        }
        while (r - l > 1) {
          ll mid = (r + l) / 2;
          ll val1 = d[0][i] + i * mid;
          ll val2 = d[1][j] + j * mid;
          if (val1 < val2)
            l = mid;
          else
            r = mid;
        }
        ans = min(ans, d[1][j] + j * l - d[0][i] - i * l);
        ans = min(ans, abs(d[1][j] + j * r - d[0][i] - i * r));
      }
    }
  }
  cout << ans << endl;
}

int main() {
#ifndef LOCAL
  ios::sync_with_stdio(false);
  cin.tie(0);
#endif
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
#ifdef LOCAL
  // freopen("in.txt", "wt", stdout); //for tester
  freopen("in.txt", "rt", stdin);
#endif
  while (cin >> n >> m >> s1 >> s2 >> t) {
    if (n == 0 && m == 0 && s1 == 0 && s2 == 0 && t == 0)
      break;
    solve();
  }
  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
  return 0;
}