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

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

const int INF = 1001001001001001001ll;

int H, W, K;
int X[200000], Y[200000];

vpint row[100000], column[100000];

int to[200000][4];
int cc[200000][4];
int dist[200000][2];

signed main() {
  scanf("%lld%lld%lld", &W, &H, &K);
  rep(i, K) scanf("%lld%lld", &X[i], &Y[i]), X[i]--, Y[i]--;

  rep(i, K) {
    row[Y[i]].pb(pint(X[i], i));
    column[X[i]].pb(pint(Y[i], i));
  }

  rep(i, H) sort(all(row[i]));
  rep(i, W) sort(all(column[i]));

  memset(to, -1, sizeof(to));

  rep(i, H) {
    rep(j, row[i].size()) {
      if (j) {
        to[row[i][j].se][3] = row[i][j - 1].se;
        cc[row[i][j].se][3] = row[i][j].fi - row[i][j - 1].fi;
      }
      if (j + 1 != row[i].size()) {
        to[row[i][j].se][1] = row[i][j + 1].se;
        cc[row[i][j].se][1] = row[i][j + 1].fi - row[i][j].fi;
      }
    }
  }

  rep(i, W) {
    rep(j, column[i].size()) {
      if (j) {
        to[column[i][j].se][0] = column[i][j - 1].se;
        cc[column[i][j].se][0] = column[i][j].fi - column[i][j - 1].fi;
      }
      if (j + 1 != column[i].size()) {
        to[column[i][j].se][2] = column[i][j + 1].se;
        cc[column[i][j].se][2] = column[i][j + 1].fi - column[i][j].fi;
      }
    }
  }
  if (column[0].size() == 0) {
    puts("-1");
    return 0;
  }
  int start = column[0][0].se;
  fill_n(*dist, 200000 * 2, INF);
  dist[start][0] = Y[start];
  priority_queue<pair<int, pint>> que;
  que.push(mp(-Y[start], pint(start, 0)));

  while (que.size()) {
    int c = -que.top().fi;
    int v = que.top().se.fi;
    int d = que.top().se.se;
    que.pop();
    if (dist[v][d] < c)
      continue;

    if (dist[v][1 - d] > c + 1) {
      dist[v][1 - d] = c + 1;
      que.push(mp(-dist[v][1 - d], pint(v, 1 - d)));
    }

    rep(i, 4) {
      if (to[v][i] == -1)
        continue;
      if (i % 2 != d)
        continue;
      int u = to[v][i];
      int cost = cc[v][i];
      if (dist[u][d] <= c + cost)
        continue;
      dist[u][d] = c + cost;
      que.push(mp(-dist[u][d], pint(u, d)));
    }
  }

  int ans = INF;
  rep(i, K) {
    // cout<<i<<" "<<dist[i][0]<<" "<<dist[i][1]<<endl;
    if (X[i] != W - 1 && Y[i] != H - 1)
      continue;
    rep(j, 2) {
      int cost = W - 1 - X[i] + H - 1 - Y[i];
      if ((X[i] == W - 1 && j == 1) || (Y[i] == H - 1 && j == 0))
        cost++;
      chmin(ans, dist[i][j] + cost);
    }
  }
  if (ans == INF)
    puts("-1");
  else
    printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

const int INF = 1001001001001001001ll;

int H, W, K;
int X[200000], Y[200000];

vpint row[100000], column[100000];

int to[200000][4];
int cc[200000][4];
int dist[200000][2];

signed main() {
  scanf("%lld%lld%lld", &W, &H, &K);
  rep(i, K) scanf("%lld%lld", &X[i], &Y[i]), X[i]--, Y[i]--;

  rep(i, K) {
    row[Y[i]].pb(pint(X[i], i));
    column[X[i]].pb(pint(Y[i], i));
  }

  rep(i, H) sort(all(row[i]));
  rep(i, W) sort(all(column[i]));

  memset(to, -1, sizeof(to));

  rep(i, H) {
    rep(j, row[i].size()) {
      if (j) {
        to[row[i][j].se][3] = row[i][j - 1].se;
        cc[row[i][j].se][3] = row[i][j].fi - row[i][j - 1].fi;
      }
      if (j + 1 != row[i].size()) {
        to[row[i][j].se][1] = row[i][j + 1].se;
        cc[row[i][j].se][1] = row[i][j + 1].fi - row[i][j].fi;
      }
    }
  }

  rep(i, W) {
    rep(j, column[i].size()) {
      if (j) {
        to[column[i][j].se][0] = column[i][j - 1].se;
        cc[column[i][j].se][0] = column[i][j].fi - column[i][j - 1].fi;
      }
      if (j + 1 != column[i].size()) {
        to[column[i][j].se][2] = column[i][j + 1].se;
        cc[column[i][j].se][2] = column[i][j + 1].fi - column[i][j].fi;
      }
    }
  }
  if (column[0].size() == 0) {
    puts("-1");
    return 0;
  }
  int start = column[0][0].se;
  fill_n(*dist, 200000 * 2, INF);
  dist[start][0] = Y[start];
  priority_queue<pair<int, pint>> que;
  que.push(mp(Y[start], pint(start, 0)));

  while (que.size()) {
    int c = que.top().fi;
    int v = que.top().se.fi;
    int d = que.top().se.se;
    que.pop();
    if (dist[v][d] < c)
      continue;

    if (dist[v][1 - d] > c + 1) {
      dist[v][1 - d] = c + 1;
      que.push(mp(dist[v][1 - d], pint(v, 1 - d)));
    }

    rep(i, 4) {
      if (to[v][i] == -1)
        continue;
      if (i % 2 != d)
        continue;
      int u = to[v][i];
      int cost = cc[v][i];
      if (dist[u][d] <= c + cost)
        continue;
      dist[u][d] = c + cost;
      que.push(mp(dist[u][d], pint(u, d)));
    }
  }

  int ans = INF;
  rep(i, K) {
    // cout<<i<<" "<<dist[i][0]<<" "<<dist[i][1]<<endl;
    if (X[i] != W - 1 && Y[i] != H - 1)
      continue;
    rep(j, 2) {
      int cost = W - 1 - X[i] + H - 1 - Y[i];
      if ((X[i] == W - 1 && j == 1) || (Y[i] == H - 1 && j == 0))
        cost++;
      chmin(ans, dist[i][j] + cost);
    }
  }
  if (ans == INF)
    puts("-1");
  else
    printf("%lld\\n", ans);
  return 0;
}