제출 #1336656

#제출 시각아이디문제언어결과실행 시간메모리
1336656duyanhchupapiJakarta Skyscrapers (APIO15_skyscraper)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e4 + 5, mod = 1e9 + 7;
const ll inf = 9e18;
int n, m, b[N], p[N], dist[N], u, v, w;
vector <int> luu[N];
queue <pair <pair <int, int>, int>> q;

void pc(int pos) {
	dist[pos] = w + 1;
	q.push({{pos, v}, dist[pos]});
	for (int x : luu[pos]) q.push({{pos, x}, dist[pos]});
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	// freopen(".INP", "r", stdin);
	// freopen(".OUT", "w", stdout);
	
	cin >> n >> m;
	for (int i = 0; i < m; ++i) cin >> b[i] >> p[i], luu[b[i]].push_back(i);
	
	q.push({{b[0], 0}, 0});
	fill(dist, dist + n, n + 1);
	dist[b[0]] = 0;
	while (!q.empty()) {
		u = q.front().first.first, v = q.front().first.second, w = q.front().second;
		q.pop();
		if (dist[u] != w) continue;
		for (int pos = u + p[v]; pos < n; pos += p[v]) {
			if (dist[pos] == n + 1) {
				pc(pos);
				break;
			}
		}
		
		for (int pos = u - p[v]; pos >= 0; pos -= p[v]) {
			if (dist[pos] == n + 1) {
				pc(pos);
				break;
			}
		}
	}
	
	cout << (dist[b[1]] == n + 1 ? -1 : dist[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...