This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 <ll, ll> pii;
const int N = 30007, B = 200, inf = 1e9;
int n, m;
int b[N], p[N], dis[N*B];
vector <pii> adj[N*B];
int En(int x, int y = 0) {return y*N + x;}
int main () {
	FIO;
	cin >> n >> m;
	
	for (int i = 1; i < B; i++) {
		for (int j = 0; j < n; j++) {
			if (j-i >= 0) adj[En(j, i)].pb({En(j-i, i), 1});
			if (j+i < n) adj[En(j, i)].pb({En(j+i, i), 1});
			adj[En(j, i)].pb({j, 0});
		}
	}
	
	for (int i = 0; i < m; i++) {
		cin >> b[i] >> p[i];
		if (p[i] < B) adj[b[i]].pb({En(b[i], p[i]), 0});
		else {
			for (int j = b[i]-p[i], k = 1; j >= 0; j -= p[i], k++) adj[b[i]].pb({j, k});
			for (int j = b[i]+p[i], k = 1; j < n; j += p[i], k++) adj[b[i]].pb({j, k});
		}
	}
	
	fill(dis, dis+N*B, inf);
	set <pii> s; s.insert({0, b[0]});
	dis[b[0]] = 0;
	while (!s.empty()) {
		auto p = s.begin();
		int x = (*p).S;
		s.erase(p);
		for (auto it : adj[x]) {
			int y = it.F, w = it.S;
			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});
			}
		}
	}
	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... |