Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define popb pop_back
- #define popf pop_front
- #define ins insert
- #define pq priority_queue
- #define minele min_element
- #define maxele max_element
- #define lb lower_bound //first pos >= val
- #define ub upper_bound // first pos > val
- #define cnt_bit __builtin_popcount
- #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
- //#pragma GCC optimize("Ofast")
- //#pragma GCC target("avx,avx2,fma")
- using namespace std;
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef pair<int, int> pii;
- int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
- int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- const ll oo = 1e18;
- const ll maxN = 1e6;
- /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
- void maximize(int &a, int b) {
- a = max(a, b);
- }
- void minimize(int &a, int b) {
- a = min(a, b);
- }
- const int N = 2e5 + 10;
- struct P {
- int x, y, id;
- } a[N];
- ll dp[N][2];
- vector<pii> g[N][2];
- void solve() {
- int n, m, k;
- cin >> n >> m >> k;
- bool ok1 = false, ok2 = false;
- for(int i = 1; i <= k; i++) {
- cin >> a[i].x >> a[i].y; a[i].id = i;
- if(a[i].x == 1 && a[i].y == 1) ok1 = true;
- if(a[i].x == n && a[i].y == m) ok2 = true;
- }
- a[++k] = {1, 1, k};
- a[++k] = {n, m, k};
- sort(a + 1, a + 1 + k, [&](P _a, P _b) {if(_a.x == _b.x) return _a.y < _b.y; return _a.x < _b.x;});
- for(int i = 2; i <= k; i++) {
- if(a[i].x == a[i - 1].x) {
- g[a[i].id][0].pb(make_pair(a[i - 1].id, abs(a[i].y - a[i - 1].y)));
- g[a[i - 1].id][0].pb(make_pair(a[i].id, abs(a[i].y - a[i - 1].y)));
- }
- }
- sort(a + 1, a + 1 + k, [&](P _a, P _b) {if(_a.y == _b.y) return _a.x < _b.x; return _a.y < _b.y;});
- for(int i = 2; i <= k; i++) {
- if(a[i].y == a[i - 1].y) {
- g[a[i].id][1].pb(make_pair(a[i - 1].id, abs(a[i].x - a[i - 1].x)));
- g[a[i - 1].id][1].pb(make_pair(a[i].id, abs(a[i].x - a[i - 1].x)));
- }
- }
- for(int i = 1; i <= k; i++) {
- dp[i][0] = dp[i][1] = oo;
- }
- dp[k - 1][0] = 0;
- struct PQ {
- int u, turn;
- ll w;
- bool operator < (const PQ & temp) const {
- return w > temp.w;
- }
- };
- pq<PQ> q;
- q.push({k - 1, 0, 0});
- while(!q.empty()) {
- PQ curr = q.top(); q.pop();
- int u = curr.u, turn = curr.turn; ll f = curr.w;
- if(u == k - 1) {
- if(ok1 && dp[u][turn ^ 1] > dp[u][turn] + 1) {
- dp[u][turn ^ 1] = dp[u][turn] + 1;
- q.push({u, turn ^ 1, dp[u][turn ^ 1]});
- }
- } else
- if(u == k) {
- if(ok2 && dp[u][turn ^ 1] > dp[u][turn] + 1) {
- dp[u][turn ^ 1] = dp[u][turn] + 1;
- q.push({u, turn ^ 1, dp[u][turn ^ 1]});
- }
- } else
- if(dp[u][turn ^ 1] > dp[u][turn] + 1) {
- dp[u][turn ^ 1] = dp[u][turn] + 1;
- q.push({u, turn ^ 1, dp[u][turn ^ 1]});
- }
- for(int i = 0; i < g[u][turn].size(); i++) {
- int v = g[u][turn][i].fi, w = g[u][turn][i].se;
- if(dp[v][turn] > dp[u][turn] + w) {
- dp[v][turn] = dp[u][turn] + w;
- q.push({v, turn, dp[v][turn]});
- }
- }
- }
- cout << (min(dp[k][0], dp[k][1]) < oo ? min(dp[k][0], dp[k][1]) : -1);
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- //online
- #endif
- int tc = 1, ddd = 0;
- // cin >> tc;
- while(tc--) {
- //ddd++;
- //cout << "Case #" << ddd << ": ";
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement