#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 30005
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
ll n, m, d[MAXN], b0 = 0, b1 = 0;
vector<ll> p[MAXN];
vector<pair<ll, ll>> adj[MAXN];
int main(){
cin >> n >> m;
FOR(i, 1, m){
ll bi, pi; cin >> bi >> pi;
if(i == 1) b0 = bi;
if(i == 2) b1 = bi;
p[bi].push_back(pi);
}
FOR(i, 0, n - 1){
for(ll pi : p[i]){
ll j = i;
while(0 <= j - pi){
j -= pi;
adj[i].push_back({j, (i - j) / pi});
}
j = i;
while(j + pi <= n){
j += pi;
adj[i].push_back({j, (j - i) / pi});
}
}
d[i] = -1;
}
/*cout << "b0, b1 are " << b0 << " and " << b1 << endl;
FOR(i, 0, n - 1){
cout << "edges from " << i << ": " << endl;
for(auto j : adj[i]){
cout << " goes to " << j.first << " in " << j.second << " jumps" << endl;
}
}*/
priority_queue<pair<ll, ll>> pq;
d[b0] = 0, pq.push({0, b0});
while(!pq.empty()){
auto pqi = pq.top(); pqi.first *= -1;
pq.pop();
if(d[pqi.second] < pqi.first) continue;
if(pqi.second == b1) break;
for(auto i : adj[pqi.second]){
if(d[i.first] == -1 || pqi.first + i.second < d[i.first]){
d[i.first] = pqi.first + i.second;
pq.push({-d[i.first], i.first});
}
}
// cout << "HERE " << pqi.second << " w/ d = " << pqi.first << endl;
}
cout << d[b1] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1624 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
2 ms |
1672 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
6 ms |
6064 KB |
Output is correct |
13 |
Correct |
6 ms |
6088 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1848 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
5 ms |
5936 KB |
Output is correct |
13 |
Correct |
6 ms |
6096 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
73 ms |
66136 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
2 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
3 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
78 ms |
67692 KB |
Output is correct |
27 |
Correct |
79 ms |
67740 KB |
Output is correct |
28 |
Correct |
2 ms |
2392 KB |
Output is correct |
29 |
Correct |
3 ms |
3136 KB |
Output is correct |
30 |
Correct |
2 ms |
2392 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2416 KB |
Output is correct |
33 |
Correct |
4 ms |
4220 KB |
Output is correct |
34 |
Correct |
4 ms |
4312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1916 KB |
Output is correct |
12 |
Correct |
5 ms |
6092 KB |
Output is correct |
13 |
Correct |
6 ms |
6092 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
2392 KB |
Output is correct |
18 |
Correct |
3 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1852 KB |
Output is correct |
20 |
Correct |
71 ms |
65924 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
2 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
77 ms |
67716 KB |
Output is correct |
27 |
Correct |
76 ms |
67620 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
3128 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
5 ms |
4052 KB |
Output is correct |
34 |
Correct |
4 ms |
4332 KB |
Output is correct |
35 |
Correct |
18 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
2140 KB |
Output is correct |
37 |
Correct |
11 ms |
8520 KB |
Output is correct |
38 |
Correct |
14 ms |
7260 KB |
Output is correct |
39 |
Correct |
14 ms |
7448 KB |
Output is correct |
40 |
Correct |
14 ms |
7520 KB |
Output is correct |
41 |
Correct |
15 ms |
7224 KB |
Output is correct |
42 |
Runtime error |
223 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1848 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
5 ms |
6092 KB |
Output is correct |
13 |
Correct |
5 ms |
6096 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
2400 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
71 ms |
66068 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
1908 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
81 ms |
67652 KB |
Output is correct |
27 |
Correct |
73 ms |
67708 KB |
Output is correct |
28 |
Correct |
2 ms |
2392 KB |
Output is correct |
29 |
Correct |
3 ms |
3152 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2392 KB |
Output is correct |
33 |
Correct |
5 ms |
4032 KB |
Output is correct |
34 |
Correct |
4 ms |
4308 KB |
Output is correct |
35 |
Correct |
11 ms |
5208 KB |
Output is correct |
36 |
Correct |
4 ms |
2140 KB |
Output is correct |
37 |
Correct |
12 ms |
8284 KB |
Output is correct |
38 |
Correct |
16 ms |
7424 KB |
Output is correct |
39 |
Correct |
14 ms |
7492 KB |
Output is correct |
40 |
Correct |
15 ms |
7516 KB |
Output is correct |
41 |
Correct |
17 ms |
7256 KB |
Output is correct |
42 |
Runtime error |
241 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |