Submission #380398

#TimeUsernameProblemLanguageResultExecution timeMemory
380398LittlePantsJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1100 ms84256 KiB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define inf 1e9
#define mod 1000000007
#define F first
#define S second
#define wopen(x) freopen((x),"w",stdout)
#define ropen(x) freopen((x),"r",stdin)
#define de(x) cout << #x << " = " << x << ' '
#define IO ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
 
const int mxN = 3e4 + 5;
int n, m, b[mxN], p[mxN];
bitset<mxN * mxN> vis;
bitset<mxN> used;
vector<int> v[mxN];

namespace io {
    const int SIZE = 1e7 + 10;
    char inbuff[SIZE];
    char *l, *r;
    inline void init() {
        l = inbuff;
        r = inbuff + fread(inbuff, 1, SIZE, stdin);
    }
    inline char gc() {
        if (l == r) init();
        return (l != r) ? *(l++) : EOF;
    }
    void read(int &x) {
        x = 0; char ch = gc();
        while(!isdigit(ch)) ch = gc();
        while(isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
    }
} using io::read;
 
signed main() {
	read(n); read(m);
	for(int i = 0; i < m; i++) read(b[i]), read(p[i]), v[b[i]].pb(p[i]);
	queue<array<int, 3>> q;
	q.push({b[0], p[0], 0});
	while(!q.empty()) {
		auto now = q.front(); q.pop();
		vis[now[0] * n + now[1]] = 1;
		if(now[0] == b[1]) {
			printf("%d\n", now[2]);
			return 0;
		}
		if(!used[now[0]]) {
			for(int i : v[now[0]]) {
				if(now[0] + i < n and vis[(now[0] + i) * n + i] == 0) 
					q.push({now[0] + i, i, now[2] + 1});
				if(now[0] - i >= 0 and vis[(now[0] - i) * n + i] == 0) 
					q.push({now[0] - i, i, now[2] + 1});
			}
		}
		used[now[0]] = 1;
		if(now[0] + now[1] < n and vis[(now[0] + now[1]) * n + now[1]] == 0) 
				q.push({now[0] + now[1], now[1], now[2] + 1});
		if(now[0] - now[1] >= 0 and vis[(now[0] - now[1]) * n + now[1]] == 0)
				q.push({now[0] - now[1], now[1], now[2] + 1});
	}
	puts("-1");
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...