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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
struct A {
  ll a /*??????*/, b /*??????*/, c /*???????????????(vector)*/;
};
struct P {
  ll a /*???*/, i /*???*/, j /*??????*/, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
vector<A> a[2][100100];
ll b[400100], inf = 999999999999999999;
int main() {
  ll h, w, k;
  cin >> w >> h >> k;
  for (ll i = 0; i < k * 2; i += 2) {
    ll x, y;
    cin >> x >> y;
    a[0][x].push_back(A{y, i, (ll)a[1][y].size()});
    a[1][y].push_back(A{x, i + 1, (ll)a[0][x].size() - 1});
  }
  a[0][1].push_back(A{1, k * 2, (ll)a[1][1].size()});
  a[0][w].push_back(A{h, k * 2 + 1, (ll)a[1][h].size()});
  a[1][h].push_back(A{w, k * 2 + 2, (ll)a[0][w].size() - 1});
  fill(b, b + k * 2 + 100, inf);
  priority_queue<P, vector<P>, greater<P>> p;
  b[k * 2] = 0;
  p.push(P{0, 1, (ll)a[0][1].size() - 1, 0});
  while (!p.empty()) {
    P t = p.top();
    p.pop();
    int c, d;
    if (t.a == 0) {
      c = 0;
      d = 1;
    } else {
      c = 1;
      d = 0;
    }
    for (ll i = 0; i < a[c][t.i].size(); i++) {
      ll res = abs(a[c][t.i][t.j].a - a[c][t.i][i].a);
      if (res + t.cost < b[a[c][t.i][i].b]) {
        b[a[c][t.i][i].b] = res + t.cost;
      }
      if (a[c][t.i][i].b != k * 2 &&
          b[a[d][a[c][t.i][i].a][a[c][t.i][i].c].b] > t.cost + res + 1) {
        b[a[d][a[c][t.i][i].a][a[c][t.i][i].c].b] = t.cost + res + 1;
        p.push(P{d, a[c][t.i][i].a, a[c][t.i][i].c,
                 b[a[d][a[c][t.i][i].a][a[c][t.i][i].c].b]});
      }
    }
  }
  ll ans = min(b[k * 2 + 1], b[k * 2 + 2]);
  if (ans >= inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
ll h, w, k;
struct A {
  ll pos, b;
};
vector<A> x[1000000], y[1000000];
const ll inf = 9000000000000000000;
ll dp[2000000][2];
ll m[2000000][2];
ll solve(ll i, ll j, short c, ll s) {
  ll sum = inf, d, e;
  if (c == 1) {
    d = x[i][j].b;
    e = x[i][j].pos;
    if (e == w) {
      sum = (h - i);
      goto stop;
    }
    for (int z = 0; z < y[e].size(); z++) {
      ll res = abs(y[e][z].pos - i);
      if (y[e][z].b != d && s + res + 1 < m[y[e][z].b][0]) {
        m[y[e][z].b][0] = 1 + res + s;
        if (dp[y[e][z].b][0] == -1)
          dp[y[e][z].b][0] = solve(e, z, 0, 1 + res + s);
        sum = min(sum, 1 + res + dp[y[e][z].b][0]);
      }
    }
  } else {
    d = y[i][j].b;
    e = y[i][j].pos;
    if (e == h) {
      sum = w - i;
      goto stop;
    }
    for (int z = 0; z < x[e].size(); z++) {
      ll res = abs(x[e][z].pos - i);
      if (x[e][z].b != d && 1 + res + s < m[x[e][z].b][1]) {
        m[x[e][z].b][1] = 1 + res + s;
        if (dp[x[e][z].b][1] == -1)
          dp[x[e][z].b][1] = solve(e, z, 1, 1 + res + s);
        sum = min(sum, 1 + res + dp[x[e][z].b][1]);
      }
    }
  }
stop:;
  return sum;
}
int main() {
  cin >> w >> h >> k;
  m[0][0] = inf;
  m[0][1] = inf;
  for (ll i = 1; i <= k; i++) {
    ll a, b;
    cin >> b >> a;
    x[a].push_back(A{b, i});
    y[b].push_back(A{a, i});
    m[i][0] = inf;
    m[i][1] = inf;
  }
  for (ll i = 0; i <= k; i++) {
    for (int j = 0; j < 2; j++)
      dp[i][j] = -1;
  }
  ll sum;
  x[1].push_back(A{1, 0});
  y[1].push_back(A{1, 0});
  m[0][0] = 0;
  m[0][1] = 1;
  sum = solve(1, x[1].size() - 1, 1, 0);
  if (sum >= inf)
    sum = -1;
  cout << sum << endl;
}