#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
int n, m;
cin >> n >> m;
pair < int , int > first, second;
vector < vector < int >> arr(1 + n);
for(int i = 0 ; i < m ; i++) {
int a, b;
cin >> a >> b;
if(i == 0) {
first = {a, b};
} else if(i == 1) {
second = {a, b};
}
arr[a].push_back(b);
}
int nodes = ((n + 1) * (n + 1)) + 5;
vector < vector < pair < int , int >>> adj(nodes);
for(int i = 0 ; i < n ; i++) {
for(auto j : arr[i]) {
int node = (i * (n + 1) + j);
adj[i * (n + 1) + n + 1].push_back({node, 0});
adj[node].push_back({i * (n + 1) + n + 1, 0});
for(int k = 1 ; k <= n ; k++) {
if(i + j * k < n) {
adj[node].push_back({(i + j * k) * (n + 1) + n + 1, k});
}
if(i - j * k >= 0) {
adj[node].push_back({(i - j * k) * (n + 1) + n + 1, k});
}
}
}
}
int st = first.first * (n + 1) + first.second;
vector < int > dis(nodes, 1e12);
priority_queue < pair < int , int >> pq;
pq.push({0, st});
dis[st] = 0;
while(pq.size()) {
auto top = pq.top();
pq.pop();
top.first *= -1;
if(top.first != dis[top.second])
continue;
for(auto ch : adj[top.second]) {
if(dis[ch.first] > top.first + ch.second) {
dis[ch.first] = top.first + ch.second;
pq.push({-dis[ch.first], ch.first});
}
}
}
int ed = second.first * (n + 1) + second.second;
if(dis[ed] >= 1e10) dis[ed] = -1;
cout << dis[ed] << endl;
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
372 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
5336 KB |
Output is correct |
13 |
Correct |
5 ms |
5812 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
5732 KB |
Output is correct |
13 |
Correct |
6 ms |
5320 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
8 ms |
18368 KB |
Output is correct |
18 |
Correct |
57 ms |
96352 KB |
Output is correct |
19 |
Correct |
44 ms |
125788 KB |
Output is correct |
20 |
Correct |
97 ms |
190292 KB |
Output is correct |
21 |
Runtime error |
5 ms |
8024 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
708 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
6616 KB |
Output is correct |
13 |
Correct |
4 ms |
6348 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
8 ms |
18264 KB |
Output is correct |
18 |
Correct |
25 ms |
96348 KB |
Output is correct |
19 |
Correct |
34 ms |
125820 KB |
Output is correct |
20 |
Correct |
99 ms |
190020 KB |
Output is correct |
21 |
Runtime error |
5 ms |
8024 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
5336 KB |
Output is correct |
13 |
Correct |
5 ms |
6344 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
1884 KB |
Output is correct |
17 |
Correct |
9 ms |
18264 KB |
Output is correct |
18 |
Correct |
25 ms |
96240 KB |
Output is correct |
19 |
Correct |
30 ms |
125776 KB |
Output is correct |
20 |
Correct |
95 ms |
190036 KB |
Output is correct |
21 |
Runtime error |
5 ms |
8024 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |