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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct State {
  int i, j, t;
  int d; // {up,down,left,right}=>{0,1,2,3}
  State() {}
  State(int i, int j, int t, int d) : i(i), j(j), t(t), d(d) {}
  bool operator<(const State &s) const { return t < s.t; }
  bool operator>(const State &s) const { return t > s.t; }
};

int solve(const vvi &sig, const vvvvi &delay, pii start, pii goal) {
  int h = sig.size(), w = sig[0].size();

  vvvvi vis(h, vvvi(w, vvi(4, vi(101))));
  priority_queue<State, vector<State>, greater<State>> pq;
  pq.emplace(start.first, start.second, 0, 3);
  while (!pq.empty()) {
    State cur = pq.top();
    pq.pop();
    if (vis[cur.i][cur.j][cur.d][cur.t])
      continue;
    vis[cur.i][cur.j][cur.d][cur.t] = 1;
    if (cur.i == goal.first && cur.j == goal.second)
      break;

    int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1};
    rep(k, 4) if (k != (cur.d ^ 1)) {
      int ni = cur.i + di[k], nj = cur.j + dj[k];
      if (ni < 0 || h <= ni || nj < 0 || w <= nj)
        continue;
      int nt = cur.t + delay[cur.i][cur.j][ni][nj];
      if (nt >= INFTY)
        continue;
      if (sig[ni][nj] != -1 && nt / sig[ni][nj] % 2 != k / 2)
        continue;
      pq.emplace(ni, nj, nt, k);
    }
  }
  const vvi &res = vis[goal.first][goal.second];
  rep(i, 101) rep(j, 4) if (res[j][i]) return i;
}

int main() {
  for (int h, w; scanf("%d%d", &h, &w), h | w;) {
    int d, s;
    scanf("%d%d", &d, &s); // 基本コスト，信号機の数
    vvi sig(h, vi(w, -1));
    rep(i, s) {
      char a;
      int b, t;
      scanf(" %c-%d %d", &a, &b, &t);
      sig[a - 'a'][b - 1] = t;
    }
    vvvvi delay(h, vvvi(w, vvi(h, vi(w, INFTY))));
    rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
      if (i == k && j == l)
        delay[i][j][k][l] = 0;
      if (abs(i - k) + abs(j - l) == 1)
        delay[i][j][k][l] = d;
    }
    // 工事中
    int c;
    scanf("%d", &c);
    rep(i, c) {
      char a1, a2;
      int b1, b2;
      scanf(" %c-%d %c-%d", &a1, &b1, &a2, &b2);
      delay[a1 - 'a'][b1 - 1][a2 - 'a'][b2 - 1] = INFTY;
      delay[a2 - 'a'][b2 - 1][a1 - 'a'][b1 - 1] = INFTY;
    }
    // 渋滞
    int j;
    scanf("%d", &j);
    rep(i, j) {
      char a1, a2;
      int b1, b2, dd;
      scanf(" %c-%d %c-%d %d", &a1, &b1, &a2, &b2, &dd);
      delay[a1 - 'a'][b1 - 1][a2 - 'a'][b2 - 1] += dd;
      delay[a2 - 'a'][b2 - 1][a1 - 'a'][b1 - 1] += dd;
    }
    // 始点・終点
    pii start, goal;
    {
      char a1, a2;
      int b1, b2;
      scanf(" %c-%d %c-%d", &a1, &b1, &a2, &b2);
      start = mp(a1 - 'a', b1 - 1);
      goal = mp(a2 - 'a', b2 - 1);
    }

    cout << solve(sig, delay, start, goal) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

struct State {
  int i, j, t;
  int d; // {up,down,left,right}=>{0,1,2,3}
  State() {}
  State(int i, int j, int t, int d) : i(i), j(j), t(t), d(d) {}
  bool operator<(const State &s) const { return t < s.t; }
  bool operator>(const State &s) const { return t > s.t; }
};

void solve(const vvi &sig, const vvvvi &delay, pii start, pii goal) {
  int h = sig.size(), w = sig[0].size();

  vvvi vis(h, vvi(w, vi(4, INFTY)));
  priority_queue<State, vector<State>, greater<State>> pq;
  pq.emplace(start.first, start.second, 0, 3);
  while (!pq.empty()) {
    State cur = pq.top();
    pq.pop();
    if (vis[cur.i][cur.j][cur.d] != INFTY)
      continue;
    vis[cur.i][cur.j][cur.d] = cur.t;

    int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1};
    rep(k, 4) if (k != (cur.d ^ 1)) {
      int ni = cur.i + di[k], nj = cur.j + dj[k];
      if (ni < 0 || h <= ni || nj < 0 || w <= nj)
        continue;
      int nt = cur.t + delay[cur.i][cur.j][ni][nj];
      if (nt >= INFTY)
        continue;
      if (sig[ni][nj] != -1 && nt / sig[ni][nj] % 2 != k / 2)
        continue;
      pq.emplace(ni, nj, nt, k);
    }
  }
  int res = *min_element(all(vis[goal.first][goal.second]));
  assert(res <= 100);
  cout << res << endl;
}

int main() {
  for (int h, w; scanf("%d%d", &h, &w), h | w;) {
    int d, s;
    scanf("%d%d", &d, &s); // 基本コスト，信号機の数
    vvi sig(h, vi(w, -1));
    rep(i, s) {
      char a;
      int b, t;
      scanf(" %c-%d %d", &a, &b, &t);
      sig[a - 'a'][b - 1] = t;
    }
    vvvvi delay(h, vvvi(w, vvi(h, vi(w, INFTY))));
    rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
      if (i == k && j == l)
        delay[i][j][k][l] = 0;
      if (abs(i - k) + abs(j - l) == 1)
        delay[i][j][k][l] = d;
    }
    // 工事中
    int c;
    scanf("%d", &c);
    rep(i, c) {
      char a1, a2;
      int b1, b2;
      scanf(" %c-%d %c-%d", &a1, &b1, &a2, &b2);
      delay[a1 - 'a'][b1 - 1][a2 - 'a'][b2 - 1] = INFTY;
      delay[a2 - 'a'][b2 - 1][a1 - 'a'][b1 - 1] = INFTY;
    }
    // 渋滞
    int j;
    scanf("%d", &j);
    rep(i, j) {
      char a1, a2;
      int b1, b2, dd;
      scanf(" %c-%d %c-%d %d", &a1, &b1, &a2, &b2, &dd);
      delay[a1 - 'a'][b1 - 1][a2 - 'a'][b2 - 1] += dd;
      delay[a2 - 'a'][b2 - 1][a1 - 'a'][b1 - 1] += dd;
    }
    // 始点・終点
    pii start, goal;
    {
      char a1, a2;
      int b1, b2;
      scanf(" %c-%d %c-%d", &a1, &b1, &a2, &b2);
      start = mp(a1 - 'a', b1 - 1);
      goal = mp(a2 - 'a', b2 - 1);
    }

    solve(sig, delay, start, goal);
  }

  return 0;
}