Submission #306783

#TimeUsernameProblemLanguageResultExecution timeMemory
306783syyJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
269 ms23928 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for(int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (int) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss

const int sqr = 175;
int n, m, b[30005], p[30005], dist[30005][175];
vi big[30005], small[30005];
deque<pii> dq; //building, cur jump, dist

int main() {
	fastio; cin >> n >> m;
	FOR(i, 0, m-1) {
		cin >> b[i] >> p[i];
		if (p[i] > sqr) big[b[i]].pb(p[i]);
		else small[b[i]].pb(p[i]);
	}
	memset(dist, -1, sizeof dist);
	dist[b[0]][0] = 0; dq.push_back(pii(0, pi(b[0], 0)));
	while (!dq.empty()) {
		pii cur = dq.front(); dq.pop_front();
		int x = cur.s.f, d = cur.f, jump = cur.s.s;
		if (jump == 0) { //transferring
			if (d != dist[x][0]) continue;
			for (auto it:big[x]) {
				if (x + it < n) dq.push_back(pii(d+1, pi(x+it, it)));
				if (x - it >= 0) dq.push_back(pii(d+1, pi(x-it, -it)));
			}
			for (auto it:small[x]) {
				if (dist[x][it] == -1 or dist[x][it] > d) {
					dist[x][it] = d;
					dq.push_front(pii(d, pi(x, it)));
				}
			}
		} else if (jump > 0 and jump < sqr) { //small dog
			if (d != dist[x][jump]) continue;
			if (dist[x][0] == -1 or dist[x][0] > d) {
				dist[x][0] = d;
				dq.push_front(pii(d, pi(x, 0)));
			}
			for (auto it:{x - jump, x + jump}) if (it >= 0 and it < n) {
				if (dist[it][jump] == -1 or dist[it][jump] > d + 1) {
					dist[it][jump] = d + 1;
					dq.push_back(pii(d+1, pi(it, jump)));
				}
			}
		} else { //big dog
			//get off
			if (dist[x][0] == -1 or dist[x][0] > d) {
				dist[x][0] = d;
				dq.push_front(pii(d, pi(x, 0)));
			}
			//continue
			if (x + jump < n and x + jump >= 0) dq.push_back(pii(d+1, pi(x+jump, jump)));
		}
	}
	cout << dist[b[1]][0];
}
#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...