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

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

#define int long long

void solve();
void make_edge(int o);

typedef pair<int, int> pii;
typedef pair<pii, int> ppiii;
typedef pair<int, pii> pipii;

int M, N, K;
ppiii room[200010];
vector<pii> V[200010][2];
int D[200010][2] = {0};

signed main() {
  scanf("%lld%lld%lld", &M, &N, &K);
  for (int i = 0; i < K; i++) {
    scanf("%lld%lld", &room[i].first.first, &room[i].first.second);
    room[i].second = i;
  }
  solve();
  return 0;
}

void solve() {
  int ans = 1000000000000000;
  sort(room, room + K);
  make_edge(0);
  for (int i = 0; i < K; i++) {
    int mem;
    mem = room[i].first.first;
    room[i].first.first = room[i].first.second;
    room[i].first.second = mem;
  }
  sort(room, room + K);
  make_edge(1);
  priority_queue<pipii, vector<pipii>, greater<pipii>> Q;
  for (int i = 0; i < K; i++) {
    int mem;
    mem = room[i].first.first;
    room[i].first.first = room[i].first.second;
    room[i].first.second = mem;
  }
  sort(room, room + K);
  int I = 0;
  while (room[I].first.first == 1) {
    if (room[I].first.second == 1) {
      Q.push(make_pair(1, make_pair(room[I].second, 1)));
      I++;
      continue;
    }
    Q.push(make_pair(room[I].first.second - 1, make_pair(room[I].second, 0)));
    I++;
  }
  while (Q.size()) {
    pipii now = Q.top();
    Q.pop();
    if (D[now.second.first][now.second.second] <= now.first &&
        D[now.second.first][now.second.second]) {
      continue;
    }
    D[now.second.first][now.second.second] = now.first;
    Q.push(make_pair(now.first + 1,
                     make_pair(now.second.first, (now.second.second + 1) % 2)));
    for (int i = 0; i < V[now.second.first][now.second.second].size(); i++) {
      Q.push(make_pair(
          now.first + V[now.second.first][now.second.second][i].second,
          make_pair(V[now.second.first][now.second.second][i].first,
                    now.second.second)));
    }
  }
  for (int i = 0; i < K; i++) {
    if (room[i].first.first == M && D[room[i].second][0] != 0) {
      ans = min(ans,
                (long long)(N - room[i].first.second + D[room[i].second][0]));
    }
    if (room[i].first.second == N && D[room[i].second][1] != 0) {
      ans =
          min(ans, (long long)(M - room[i].first.first + D[room[i].second][1]));
    }
  }
  if (ans == 1000000000000000) {
    printf("-1\\n");
    return;
  }
  printf("%lld\\n", ans);
  return;
}

void make_edge(int o) {
  int mem1 = 0, mem2;
  for (int i = 0; i < K; i++) {
    if (mem1 != room[i].first.first) {
      mem1 = room[i].first.first;
      mem2 = room[i].first.second;
      continue;
    }
    V[room[i - 1].second][o].push_back(
        make_pair(room[i].second, room[i].first.second - mem2));
    V[room[i].second][o].push_back(
        make_pair(room[i - 1].second, room[i].first.second - mem2));
    mem2 = room[i].first.second;
  }
  return;
}

Error: TLE

Fixed submission:

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

void solve();
void make_edge(int o);

typedef pair<int, int> pii;
typedef pair<pii, int> ppiii;
typedef pair<int, pii> pipii;

int M, N, K;
ppiii room[200010];
vector<pii> V[200010][2];
int D[200010][2] = {0};

int main() {
  scanf("%d%d%d", &M, &N, &K);
  for (int i = 0; i < K; i++) {
    scanf("%d%d", &room[i].first.first, &room[i].first.second);
    room[i].second = i;
  }
  solve();
  return 0;
}

void solve() {
  int ans = 1000000000;
  sort(room, room + K);
  make_edge(0);
  for (int i = 0; i < K; i++) {
    int mem;
    mem = room[i].first.first;
    room[i].first.first = room[i].first.second;
    room[i].first.second = mem;
  }
  sort(room, room + K);
  make_edge(1);
  priority_queue<pipii> Q;
  for (int i = 0; i < K; i++) {
    int mem;
    mem = room[i].first.first;
    room[i].first.first = room[i].first.second;
    room[i].first.second = mem;
  }
  sort(room, room + K);
  int I = 0;
  while (room[I].first.first == 1) {
    if (room[I].first.second == 1) {
      Q.push(make_pair(1, make_pair(room[I].second, 1)));
      I++;
      continue;
    }
    Q.push(make_pair(room[I].first.second - 1, make_pair(room[I].second, 0)));
    I++;
  }
  while (Q.size()) {
    pipii now = Q.top();
    Q.pop();
    if (D[now.second.first][now.second.second] <= now.first &&
        D[now.second.first][now.second.second]) {
      continue;
    }
    D[now.second.first][now.second.second] = now.first;
    Q.push(make_pair(now.first + 1,
                     make_pair(now.second.first, (now.second.second + 1) % 2)));
    for (int i = 0; i < V[now.second.first][now.second.second].size(); i++) {
      Q.push(make_pair(
          now.first + V[now.second.first][now.second.second][i].second,
          make_pair(V[now.second.first][now.second.second][i].first,
                    now.second.second)));
    }
  }
  for (int i = 0; i < K; i++) {
    if (room[i].first.first == M && D[room[i].second][0] != 0) {
      ans = min(ans, N - room[i].first.second + D[room[i].second][0]);
    }
    if (room[i].first.second == N && D[room[i].second][1] != 0) {
      ans = min(ans, M - room[i].first.first + D[room[i].second][1]);
    }
  }
  if (ans == 1000000000) {
    printf("-1\\n");
    return;
  }
  printf("%d\\n", ans);
  return;
}

void make_edge(int o) {
  int mem1 = 0, mem2;
  for (int i = 0; i < K; i++) {
    if (mem1 != room[i].first.first) {
      mem1 = room[i].first.first;
      mem2 = room[i].first.second;
      continue;
    }
    V[room[i - 1].second][o].push_back(
        make_pair(room[i].second, room[i].first.second - mem2));
    V[room[i].second][o].push_back(
        make_pair(room[i - 1].second, room[i].first.second - mem2));
    mem2 = room[i].first.second;
  }
  return;
}