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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

template <typename T> struct WG {
  using P = pair<int, T>;
  using Q = pair<T, int>;
  int n, k;
  vector<vector<P>> graph;
  vector<T> ans;
  WG(int n_, int k_) {
    n = n_, k = k_;
    graph.resize(n);
    ans.resize(n, 2000000000000000000);
  }
  void add_edge1(int from, int to, T cost) {
    P edge;
    edge.first = to;
    edge.second = cost;
    graph[from].push_back(edge);
  }
  void add_edge2(int from, int to, T cost) {
    P edge1, edge2;
    edge1.first = to;
    edge1.second = cost;
    edge2.first = from;
    edge2.second = cost;
    graph[from].push_back(edge1);
    graph[to].push_back(edge2);
  }
  void dijkstra(int start) {
    int a = n / 4;
    ans[start] = 0;
    Q s;
    s.first = 0;
    s.second = start;
    priority_queue<Q, vector<Q>, greater<Q>> pque;
    pque.push(s);
    vector<bool> seen(n, 0);
    while (!pque.empty()) {
      Q tmp = pque.top();
      pque.pop();
      int x = tmp.second;
      seen[x] = 1;
      for (auto edge : graph[x]) {
        if (seen[edge.first])
          continue;
        if (edge.first % a == x % a)
          edge.second = (k - ans[x] % k) % k;
        if (ans[edge.first] > ans[x] + edge.second) {
          ans[edge.first] = ans[x] + edge.second;
          Q next;
          next.first = ans[edge.first];
          next.second = edge.first;
          pque.push(next);
        }
      }
    }
  }
};

int main() {
  int h, w, k, sx, sy, gx, gy, a, a2, a3, b, c;
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx;
  --sy;
  --gx;
  --gy;
  a = h * w;
  a2 = a * 2;
  a3 = a * 3;
  b = sx * w + sy;
  c = gx * w + gy;
  WG<long> g(4 * a, k);
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '@')
        continue;
      int tmp = i * w + j;
      if (j < w - 1 && v[i][j + 1] == '.') {
        g.add_edge1(tmp, tmp + 1, 1);
        g.add_edge1(a2 + tmp + 1, a2 + tmp, 1);
      }
      if (j > 0 && v[i][j - 1] == '.') {
        g.add_edge1(tmp - 1, tmp, 1);
        g.add_edge1(a2 + tmp, a2 + tmp - 1, 1);
      }
      if (i < h - 1 && v[i + 1][j] == '.') {
        g.add_edge1(tmp + a, tmp + a + w, 1);
        g.add_edge1(a3 + tmp + w, a3 + tmp, 1);
      }
      if (i > 0 && v[i - 1][j] == '.') {
        g.add_edge1(tmp + a - w, tmp + a, 1);
        g.add_edge1(a3 + tmp, a3 + tmp - w, 1);
      }
      g.add_edge2(tmp, tmp + a, 0);
      g.add_edge2(tmp, tmp + a2, 0);
      g.add_edge2(tmp, tmp + a3, 0);
      g.add_edge2(tmp + a, tmp + a2, 0);
      g.add_edge2(tmp + a, tmp + a3, 0);
      g.add_edge2(tmp + a2, tmp + a3, 0);
    }
  }
  g.dijkstra(b);
  long ans =
      min(g.ans[c], min(g.ans[c + a], min(g.ans[c + a2], g.ans[c + a3])));
  if (ans == 2000000000000000000)
    ans = -1;
  else
    ans = (ans - 1) / k + 1;
  cout << ans << "\\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;

template <typename T> struct WG {
  using P = pair<int, T>;
  using Q = pair<T, int>;
  int n, k;
  vector<vector<P>> graph;
  vector<T> ans;
  WG(int n_, int k_) {
    n = n_, k = k_;
    graph.resize(n);
    ans.resize(n, 2000000000000000000);
  }
  void add_edge1(int from, int to, T cost) {
    P edge;
    edge.first = to;
    edge.second = cost;
    graph[from].push_back(edge);
  }
  void add_edge2(int from, int to, T cost) {
    P edge1, edge2;
    edge1.first = to;
    edge1.second = cost;
    edge2.first = from;
    edge2.second = cost;
    graph[from].push_back(edge1);
    graph[to].push_back(edge2);
  }
  void dijkstra(int start) {
    int a = n / 4;
    ans[start] = 0;
    Q s;
    s.first = 0;
    s.second = start;
    priority_queue<Q, vector<Q>, greater<Q>> pque;
    pque.push(s);
    vector<bool> seen(n, 0);
    while (!pque.empty()) {
      Q tmp = pque.top();
      pque.pop();
      int x = tmp.second;
      seen[x] = 1;
      for (auto edge : graph[x]) {
        if (seen[edge.first])
          continue;
        if (edge.first % a == x % a)
          edge.second = (k - ans[x] % k) % k;
        if (ans[edge.first] > ans[x] + edge.second) {
          ans[edge.first] = ans[x] + edge.second;
          Q next;
          next.first = ans[edge.first];
          next.second = edge.first;
          pque.push(next);
        }
      }
    }
  }
};

int main() {
  int h, w, k, sx, sy, gx, gy, a, b, c;
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx;
  --sy;
  --gx;
  --gy;
  a = h * w;
  b = sx * w + sy;
  c = gx * w + gy;
  WG<long> g(4 * a, k);
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '@')
        continue;
      int tmp = i * w + j;
      if (j < w - 1 && v[i][j + 1] == '.') {
        g.add_edge1(tmp, tmp + 1, 1);
        g.add_edge1(a * 2 + tmp + 1, a * 2 + tmp, 1);
      }
      if (j > 0 && v[i][j - 1] == '.') {
        g.add_edge1(tmp - 1, tmp, 1);
        g.add_edge1(a * 2 + tmp, a * 2 + tmp - 1, 1);
      }
      if (i < h - 1 && v[i + 1][j] == '.') {
        g.add_edge1(tmp + a, tmp + a + w, 1);
        g.add_edge1(a * 3 + tmp + w, a * 3 + tmp, 1);
      }
      if (i > 0 && v[i - 1][j] == '.') {
        g.add_edge1(tmp + a + w, tmp + a, 1);
        g.add_edge1(a * 3 + tmp, a * 3 + tmp + w, 1);
      }
      g.add_edge2(tmp, tmp + a, 0);
      g.add_edge2(tmp, tmp + a * 2, 0);
      g.add_edge2(tmp, tmp + a * 3, 0);
      g.add_edge2(tmp + a, tmp + a * 2, 0);
      g.add_edge2(tmp + a, tmp + a * 3, 0);
      g.add_edge2(tmp + a * 2, tmp + a * 3, 0);
    }
  }
  g.dijkstra(b);
  long ans =
      min(g.ans[c], min(g.ans[c + a], min(g.ans[c + 2 * a], g.ans[c + 3 * a])));
  if (ans == 2000000000000000000)
    ans = -1;
  else
    ans = (ans - 1) / k + 1;
  cout << ans << "\\n";
  return 0;
}