Problem p00528 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * 0605.cc: Modern Mansion
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_K = 200000;
const int MAX_GN = MAX_K + 2;

typedef long long ll;
const ll LINF = 1LL << 60;

/* typedef */

typedef vector<int> vi;

struct Stat {
  ll d;
  int i, j;
  bool s;
  Stat() {}
  Stat(ll _d, int _i, int _j, bool _s) : d(_d), i(_i), j(_j), s(_s) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int xys[MAX_GN][2];
vi nbrs[2][MAX_N];
bool sws[MAX_GN];
ll dists[MAX_GN][2];

/* subroutines */

/* main */

int main() {
  int m, n, k;
  cin >> m >> n >> k;

  xys[0][0] = xys[0][1] = 0;
  nbrs[0][0].push_back(0);
  nbrs[1][0].push_back(0);

  xys[1][0] = m - 1, xys[1][1] = n - 1;
  nbrs[0][m - 1].push_back(1);
  nbrs[1][n - 1].push_back(1);

  int gn = 2;

  for (int i = 0; i < k; i++) {
    int xi, yi;
    cin >> xi >> yi;
    xi--, yi--;

    if (xi == 0 && yi == 0)
      sws[0] = true;
    else if (xi != m - 1 || yi != n - 1) {
      xys[gn][0] = xi;
      xys[gn][1] = yi;
      nbrs[0][xi].push_back(gn);
      nbrs[1][yi].push_back(gn);
      sws[gn] = true;
      gn++;
    }
  }
  // printf("gn=%d\\n", gn);

  for (int i = 0; i < gn; i++)
    dists[i][0] = dists[i][1] = LINF;
  dists[0][0] = 0;

  priority_queue<Stat> q;
  q.push(Stat(0, 0, 0, true));

  while (!q.empty()) {
    Stat u = q.top();
    q.pop();
    if (dists[u.i][u.j] != u.d)
      continue;
    if (u.i == 1)
      break;

    if (sws[u.i]) {
      int vj = u.j ^ 1;
      ll vd = u.d + 1;
      if (dists[u.i][vj] > vd) {
        dists[u.i][vj] = vd;
        q.push(Stat(vd, u.i, vj, true));
      }
    }

    if (u.s) {
      vi &nbru = nbrs[u.j][xys[u.i][u.j]];
      for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
        int &vi = *vit;
        if (vi != u.i) {
          ll vd = u.d + abs(xys[vi][u.j ^ 1] - xys[u.i][u.j ^ 1]);
          if (dists[vi][u.j] > vd) {
            dists[vi][u.j] = vd;
            q.push(Stat(vd, vi, u.j, false));
          }
        }
      }
    }
  }

  ll mind = (dists[1][0] < dists[1][1]) ? dists[1][0] : dists[1][1];
  printf("%lld\\n", (mind >= LINF) ? -1LL : mind);
  return 0;
}

Error: TLE

Fixed submission:

/*
 * 0605.cc: Modern Mansion
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_K = 200000;
const int MAX_GN = MAX_K + 2;

typedef long long ll;
const ll LINF = 1LL << 60;

/* typedef */

typedef vector<int> vi;

struct Stat {
  ll d;
  int i, j;
  Stat() {}
  Stat(ll _d, int _i, int _j) : d(_d), i(_i), j(_j) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int xys[MAX_GN][2];
vi nbrs[2][MAX_N];
bool sws[MAX_GN];
ll dists[MAX_GN][2];

/* subroutines */

/* main */

int main() {
  int m, n, k;
  cin >> m >> n >> k;

  xys[0][0] = xys[0][1] = 0;
  nbrs[0][0].push_back(0);
  nbrs[1][0].push_back(0);

  xys[1][0] = m - 1, xys[1][1] = n - 1;
  nbrs[0][m - 1].push_back(1);
  nbrs[1][n - 1].push_back(1);

  int gn = 2;

  for (int i = 0; i < k; i++) {
    int xi, yi;
    cin >> xi >> yi;
    xi--, yi--;

    if (xi == 0 && yi == 0)
      sws[0] = true;
    else if (xi != m - 1 || yi != n - 1) {
      xys[gn][0] = xi;
      xys[gn][1] = yi;
      nbrs[0][xi].push_back(gn);
      nbrs[1][yi].push_back(gn);
      sws[gn] = true;
      gn++;
    }
  }
  // printf("gn=%d\\n", gn);

  for (int i = 0; i < gn; i++)
    dists[i][0] = dists[i][1] = LINF;
  dists[0][0] = 0;

  priority_queue<Stat> q;
  q.push(Stat(0, 0, 0));

  while (!q.empty()) {
    Stat u = q.top();
    q.pop();
    if (dists[u.i][u.j] != u.d)
      continue;
    if (u.i == 1)
      break;

    if (sws[u.i]) {
      int vj = u.j ^ 1;
      ll vd = u.d + 1;
      if (dists[u.i][vj] > vd) {
        dists[u.i][vj] = vd;
        q.push(Stat(vd, u.i, vj));
      }
    }

    vi &nbru = nbrs[u.j][xys[u.i][u.j]];
    for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int &vi = *vit;
      if (vi != u.i) {
        ll vd = u.d + abs(xys[vi][u.j ^ 1] - xys[u.i][u.j ^ 1]);
        if (dists[vi][u.j] > vd) {
          dists[vi][u.j] = vd;
          q.push(Stat(vd, vi, u.j));
        }
      }
    }
  }

  ll mind = (dists[1][0] < dists[1][1]) ? dists[1][0] : dists[1][1];
  printf("%lld\\n", (mind >= LINF) ? -1LL : mind);
  return 0;
}