이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef pair <int, int> pii;
const int N = 30007, B = 200, inf = 1e9;
int n, m;
int b[N], p[N], dis[N*B];
vector <int> td[N], gt[N];
set <pii> s;
int En(int x, int y = 0) {return y*N + x;}
pii De(int x) {return {x%N, x/N};}
void Vis(int x, int y, int w) {
	if (dis[x] + w < dis[y]) {
		if (dis[y] != inf) s.erase({dis[y], y});
		dis[y] = dis[x] + w;
		s.insert({dis[y], y});
	}
}
int main () {
	FIO;
	cin >> n >> m;
	
	for (int i = 0; i < m; i++) {
		cin >> b[i] >> p[i];
		if (p[i] < B) gt[b[i]].pb(p[i]);
		else td[b[i]].pb(p[i]);
	}
	
	for (int i = 0; i < N*B; i++) dis[i] = inf;
	s.insert({0, b[0]});
	dis[b[0]] = 0;
	while (!s.empty()) {
		auto p = s.begin();
		int x = (*p).S;
		s.erase(p);
		pii de = De(x);
		int A = de.F, C = de.S;
		if (C > 0) {
			if (A-C >= 0) Vis(x, En(A-C, C), 1);
			if (A+C < n) Vis(x, En(A+C, C), 1);
			Vis(x, A, 0);
		}
		else {
			for (auto P : gt[x]) Vis(x, En(x, P), 0);
			for (auto P : td[x]) {
				for (int j = A-P, k = 1; j >= 0; j -= P, k++) Vis(x, j, k);
				for (int j = A+P, k = 1; j < n; j += P, k++) Vis(x, j, k);
			}
		}
	}
	if (dis[b[1]] != inf) cout << dis[b[1]] << "\n";
	else cout << "-1\n";
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |