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

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int MAXN = 1005;
int n, m, S1, S2, T;
char s[100];
struct edge {
  int to;
  long long w;
  bool type;
  edge(int to = 0, long long w = 0, bool type = false)
      : to(to), w(w), type(type) {}
};
vector<edge> G[MAXN];
long long dist[MAXN][MAXN];
priority_queue<pair<long long, pair<int, int>>> q;
pair<long long, pair<int, int>> now;
vector<long long> ans;
long long cross(long long a1, long long b1, long long a2, long long b2) {
  if (a1 == INF || a2 == INF)
    return 0;
  return floor((a1 - a2 + 1.0) / (b2 - b1));
}
long long calc(long long dp[MAXN], int k, long long x) {
  long long ret = INF;
  for (int i = 0; i <= k; i++) {
    ret = min(ret, dp[i] + x * i);
  }
  return ret;
}
int main() {
  while (~scanf("%d%d%d%d%d", &n, &m, &S1, &S2, &T) && n) {
    for (int i = 1; i <= n; i++) {
      G[i].clear();
    }
    int k = 0;
    for (int i = 0, x, y, z; i < m; i++) {
      scanf("%d%d%s", &x, &y, s);
      if (s[0] == 'x') {
        G[x].push_back(edge(y, 0, 1));
        G[y].push_back(edge(x, 0, 1));
        k++;
      } else {
        sscanf(s, "%d", &z);
        G[x].push_back(edge(y, z, 0));
        G[y].push_back(edge(x, z, 0));
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= k; j++) {
        dist[i][j] = INF;
      }
    }
    dist[T][0] = 0;
    while (!q.empty())
      q.pop();
    q.push(make_pair(0LL, make_pair(T, 0)));
    while (!q.empty()) {
      now = q.top();
      q.pop();
      int u = now.second.first, cnt = now.second.second;
      long long d = dist[u][cnt];
      if (-now.first > d)
        continue;
      for (auto p : G[u]) {
        int ncnt = cnt + p.type, nidx = p.to;
        long long ndist = d + p.w;
        if (ncnt <= k && ndist < dist[nidx][ncnt]) {
          dist[nidx][ncnt] = ndist;
          q.push(make_pair(-ndist, make_pair(nidx, ncnt)));
        }
      }
    }
    ans.clear();
    ans.push_back(0LL);
    for (int i = 0; i <= k; i++) {
      for (int j = 0; j <= k; j++)
        if (i != j) {
          long long x1, x2, x3;
          x1 = cross(dist[S1][i], i, dist[S2][j], j);
          x2 = cross(dist[S1][i], i, dist[S1][j], j);
          x3 = cross(dist[S2][i], i, dist[S2][j], j);
          for (int d = -1; d <= 1; d++) {
            ans.push_back(x1 + d);
            ans.push_back(x2 + d);
            ans.push_back(x3 + d);
          }
        }
    }
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    long long ret = INF;
    for (auto p : ans) {
      if (p >= 0) {
        ret = min(ret, abs(calc(dist[S1], k, p) - calc(dist[S2], k, p)));
      }
    }
    printf("%lld\\n", ret);
  }
  return 0;
}

Error: WA

Fixed submission:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
#define Msn(x, y) (memset((x), 0, sizeof((x[0])) * (y + 1)))
#define msn(x) (memset((x), 0, sizeof((x))))
#define msx(x, y) (memset((x), 0x7f, sizeof((x[0])) * (y + 3)))
#define acer cout << "sb" << endl
typedef long long ll;
using namespace std;
int n, m;
const int maxn = 1002;
struct node {
  int v;
  ll w;
};
vector<node> g[maxn];
int T, s1, s2;
bool inq[maxn];
ll dis[maxn * 102];
const int M = 101;
void solve() {
  queue<int> q;
  memset(dis, -1, sizeof(dis));
  msn(inq);
  q.push(T * M);
  dis[T * M] = 0;
  inq[T * M] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    inq[now] = 0;
    int a = now / M;
    int b = now % M;
    for (int i = 0; i < g[a].size(); i++) {
      if (g[a][i].w == -1) {
        int nxt = g[a][i].v * M + b + 1;
        if (b < 100) {
          if (dis[nxt] == -1 || dis[now] < dis[nxt]) {
            dis[nxt] = dis[now];
            if (!inq[nxt]) {
              q.push(nxt);
              inq[nxt] = 1;
            }
          }
        }
      } else {
        int nxt = g[a][i].v * M + b;
        if (dis[nxt] == -1 || dis[nxt] > dis[now] + g[a][i].w) {
          dis[nxt] = dis[now] + g[a][i].w;
          if (!inq[nxt]) {
            q.push(nxt);
            inq[nxt] = 1;
          }
        }
      }
    }
  }
}

struct node1 {
  ll a, b;
};
vector<node1> p[2];
vector<node1> nod, line;
ll Abs(ll x) { return x < 0 ? -x : x; }

ll check(int x) {
  ll _1, _2;
  _1 = _2 = 1e18 + 7;
  for (int i = 0; i < p[0].size(); i++) {
    _1 = min(_1, x * p[0][i].a + p[0][i].b);
  }
  for (int i = 0; i < p[1].size(); i++) {
    _2 = min(_2, x * p[1][i].a + p[1][i].b);
  }
  return Abs(_1 - _2);
}

int main() {
  while (scanf("%d%d%d%d%d", &n, &m, &s1, &s2, &T)) {
    if (n == 0 && m == 0)
      break;
    T--, s1--, s2--;
    p[0].clear();
    p[1].clear();
    nod.clear();
    line.clear();
    for (int i = 0; i <= n; i++)
      g[i].clear();
    int u, v, w;
    char s[50];
    for (int i = 0; i < m; i++) {
      scanf("%d%d%s", &u, &v, s);
      if (s[0] == 'x') {
        w = -1;
      } else {
        w = 0;
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
          w = w * 10 + s[i] - '0';
        }
      }
      u--, v--;
      g[u].push_back((node){v, w});
      g[v].push_back((node){u, w});
    }
    solve();
    /*  for(int i=0;i<=100;i++)
      {
          if(dis[s1*M+i]>=0)p[0].push_back((node1){i,dis[s1*M+i]}),line.push_back((node1){i,dis[s1*M+i]});
          if(dis[s2*M+i]>=0)p[1].push_back((node1){i,dis[s2*M+i]}),line.push_back((node1){i,dis[s2*M+i]});
      }
      ll ans=check(0);
      for(int i=0;i<line.size();i++)
      {
          for(int j=i+1;j<line.size();j++)
          {
              if(line[i].a==line[j].a)
              {
                  ans=min(Abs(line[i].b-line[j].b),ans);
              }
              else
              {
                  ll a1=Abs(line[i].b-line[j].b)/Abs((line[i].a-line[j].a));
                  ans=min(ans,check(a1));
                  a1++;
                  ans=min(check(a1),ans);
              }
          }
      }
      printf("%lld\\n",ans);*/
  }
  return 0;
}