Submission #699865

#TimeUsernameProblemLanguageResultExecution timeMemory
699865happypotatoJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1088 ms87300 KiB
#include <bits/stdc++.h>
using namespace std;
const int mxN = 3e4 + 1;
pair<int, int> doge[mxN];
queue<int> jump[mxN];
map<int, int> dist[mxN];
queue<pair<int, int>> q[2];
int curdist = 0;
bool flag = false;
int n;
void PushElement(pair<int, pair<int, int>> &nxt) {
	q[flag ^ (nxt.first != curdist)].push(nxt.second);
}
void AddElement(pair<int, pair<int, int>> &nxt) {
	if (!(0 <= nxt.second.first && nxt.second.first < n)) return;
	if (dist[nxt.second.first].find(nxt.second.second) == dist[nxt.second.first].end()) {
		PushElement(nxt);
		dist[nxt.second.first][nxt.second.second] = nxt.first;
	} else if (dist[nxt.second.first][nxt.second.second] > nxt.first) {
		PushElement(nxt);
		dist[nxt.second.first][nxt.second.second] = nxt.first;
	}
}
void solve() {
	int m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> doge[i].first >> doge[i].second;
		if (i != 0) jump[doge[i].first].push(doge[i].second);
	}
	pair<int, pair<int, int>> nxt;
	nxt = {0, doge[0]};
	AddElement(nxt);
	while (!q[flag].empty()) {
		while (!q[flag].empty()) {
			pair<int, int> cur = q[flag].front();
			q[flag].pop();
			if (dist[cur.first][cur.second] < curdist) continue;
			if (cur.first == doge[1].first) {
				cout << curdist << endl;
				return;
			}
			while (!jump[cur.first].empty()) {
				int delta = jump[cur.first].front();
				jump[cur.first].pop();
				nxt = {curdist, {cur.first, delta}};
				AddElement(nxt);
			}
			nxt = {curdist + 1, {cur.first - cur.second, cur.second}};
			AddElement(nxt);
			nxt = {curdist + 1, {cur.first + cur.second, cur.second}};
			AddElement(nxt);
		}
		curdist++;
		flag ^= 1;
	}
	cout << "-1\n";
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	solve();
}
#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...