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

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;
const int64 INF = 1LL << 60;
typedef tuple<int64, int, int> Pi;

struct edge {
  int to, cost;
};

int main() {
  int N, M, S1, S2, T;

  int64 dp[1000][101];

  while (cin >> N >> M >> S1 >> S2 >> T, N) {
    vector<edge> G[1000];
    --S1, --S2, --T;

    int edged = 0;
    for (int i = 0; i < M; i++) {
      int a, b;
      string c;
      cin >> a >> b >> c;
      --a, --b;
      if (c == "x") {
        G[a].emplace_back((edge){b, -1});
        G[b].emplace_back((edge){a, -1});
        ++edged;
      } else {
        G[a].emplace_back((edge){b, stoi(c)});
        G[b].emplace_back((edge){a, stoi(c)});
      }
    }

    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    fill_n(*dp, 101 * 1000, INF);
    dp[T][0] = 0;
    que.emplace(0, T, 0);
    while (!que.empty()) {
      int64 cost, now, slow;
      tie(cost, now, slow) = que.top();
      que.pop();
      if (cost > dp[now][slow])
        continue;
      for (auto &e : G[now]) {
        int64 ncost = cost, nslow = slow;
        if (e.cost == -1)
          ++nslow;
        else
          ncost += e.cost;
        if (nslow > edged || ncost >= dp[e.to][nslow])
          continue;
        dp[e.to][nslow] = ncost;
        que.emplace(ncost, e.to, nslow);
      }
    }

    vector<int64> latte;
    latte.push_back(0);
    latte.push_back(10000000000000LL);
    for (int i = 0; i <= edged; i++) {
      for (int j = i + 1; j <= edged; j++) {
        latte.emplace_back((dp[S1][i] - dp[S1][j]) / (j - i));
        latte.emplace_back((dp[S2][i] - dp[S2][j]) / (j - i));
        latte.emplace_back((dp[S1][i] - dp[S2][j]) / (j - i));
        latte.emplace_back((dp[S2][i] - dp[S1][j]) / (j - i));
      }
    }

    int64 ret = INF;
    auto get = [&](int64 v) {
      v = max(v, 0LL);
      int64 aa = INF, bb = INF;
      for (int i = 0; i <= edged; i++)
        aa = min(aa, dp[S1][i] + i * v);
      for (int i = 0; i <= edged; i++)
        bb = min(bb, dp[S2][i] + i * v);
      return (llabs(aa - bb));
    };

    for (int i = -10; i <= 10; i++) {
      for (auto &cost : latte)
        ret = min(ret, get(cost + i));
    }
    // zinsei
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int64;
const int64 INF = 1LL << 59;
typedef tuple<int64, int, int> Pi;

struct edge {
  int to, cost;
};

int main() {
  int N, M, S1, S2, T;
  vector<edge> G[1000];
  int64 dp[101][1000];

  while (cin >> N >> M >> S1 >> S2 >> T, N) {
    --S1, --S2, --T;

    int edged = 0;
    for (int i = 0; i < M; i++) {
      int a, b;
      string c;
      cin >> a >> b >> c;
      --a, --b;
      if (c == "x") {
        G[a].emplace_back((edge){b, -1});
        G[b].emplace_back((edge){a, -1});
        ++edged;
      } else {
        G[a].emplace_back((edge){b, stoi(c)});
        G[b].emplace_back((edge){a, stoi(c)});
      }
    }

    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    fill_n(*dp, 101 * 1000, INF);
    dp[T][0] = 0;
    que.emplace(0, T, 0);
    while (!que.empty()) {
      int cost, now, slow;
      tie(cost, now, slow) = que.top();
      que.pop();
      if (cost > dp[now][slow])
        continue;
      for (auto &e : G[now]) {
        int64 ncost = cost, nslow = slow;
        if (e.cost == -1)
          ++nslow;
        else
          ncost += e.cost;
        if (nslow > edged || ncost >= dp[e.to][nslow])
          continue;
        dp[e.to][nslow] = ncost;
        que.emplace(ncost, e.to, nslow);
      }
    }

    vector<int64> latte;
    latte.push_back(0);
    latte.push_back(10000000000000LL);
    for (int i = 0; i <= edged; i++) {
      for (int j = i + 1; j <= edged; j++) {
        latte.emplace_back((dp[S1][i] - dp[S1][j]) / (j - i));
        latte.emplace_back((dp[S2][i] - dp[S2][j]) / (j - i));
        latte.emplace_back((dp[S1][i] - dp[S2][j]) / (j - i));
        latte.emplace_back((dp[S2][i] - dp[S1][j]) / (j - i));
      }
    }

    int64 ret = INF;

    auto get = [&](int64 v) {
      int64 aa = INF, bb = INF;
      for (int i = 0; i <= edged; i++)
        aa = min(aa, dp[S1][i] + i * v);
      for (int i = 0; i <= edged; i++)
        bb = min(bb, dp[S2][i] + i * v);
      return (llabs(aa - bb));
    };
    for (auto &cost : latte)
      if (cost >= 0)
        ret = min(ret, get(cost));
    for (auto &cost : latte)
      if (cost >= -1)
        ret = min(ret, get(cost + 1));
    cout << ret << endl;
  }
}