#include <bits/stdc++.h>
using namespace std;
#define int long long
struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<int>()(x.first^(x.second<<32));
}
};
priority_queue<pair<int,pair<int,int>>> q;
vector<int> buildings[30001];
int power[30001];
unordered_map<pair<int,int>,bool,HASH> visited;
const int INF = 1e15;
int n;
const int threshold = 173;
inline void adddoge(int x,int building,int dist){
if(power[x]>threshold){
int minimum = building - power[x] * (building / power[x]);
for (int i = minimum; i < n; i += power[x])
if (!visited[{i, 0}]) {
q.emplace(dist - (abs(building - i) / power[x]), make_pair(i,0));
}
} else {
if(!visited[{building,power[x]}])q.emplace(dist,make_pair(building,power[x]));
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
int base,tar;
cin>>base>>power[0]>>tar>>power[1];
buildings[base].emplace_back(0);
buildings[tar].emplace_back(1);
for(int i=2;i<m;i++){
int b;cin>>b>>power[i];
buildings[b].emplace_back(i);
}
q.emplace(0, make_pair(base,0));
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.second])continue;
visited[curr.second]=true;
if(curr.second.first==tar){
cout << -curr.first << '\n';
return 0;
}
if(curr.second.second==0)for(int&i:buildings[curr.second.first])adddoge(i,curr.second.first,curr.first);
else {
if(!visited[{curr.second.first,0}])q.emplace(curr.first, make_pair(curr.second.first,0));
if(curr.second.first+curr.second.second<n and !visited[{curr.second.first+curr.second.second,curr.second.second}])q.emplace(curr.first-1,make_pair(curr.second.first+curr.second.second,curr.second.second));
if(curr.second.first-curr.second.second>=0 and !visited[{curr.second.first-curr.second.second,curr.second.second}])q.emplace(curr.first-1,make_pair(curr.second.first-curr.second.second,curr.second.second));
}
}
cout << "-1\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1368 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1268 KB |
Output is correct |
23 |
Correct |
2 ms |
1368 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
14 ms |
4716 KB |
Output is correct |
30 |
Correct |
4 ms |
2140 KB |
Output is correct |
31 |
Correct |
8 ms |
3156 KB |
Output is correct |
32 |
Correct |
6 ms |
2680 KB |
Output is correct |
33 |
Correct |
29 ms |
8160 KB |
Output is correct |
34 |
Correct |
17 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1368 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1112 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1384 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
5 ms |
2136 KB |
Output is correct |
29 |
Correct |
15 ms |
4716 KB |
Output is correct |
30 |
Correct |
4 ms |
2140 KB |
Output is correct |
31 |
Correct |
8 ms |
3000 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
29 ms |
8136 KB |
Output is correct |
34 |
Correct |
17 ms |
4972 KB |
Output is correct |
35 |
Correct |
8 ms |
2456 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
6 ms |
2708 KB |
Output is correct |
38 |
Correct |
11 ms |
2832 KB |
Output is correct |
39 |
Correct |
6 ms |
1628 KB |
Output is correct |
40 |
Correct |
6 ms |
2140 KB |
Output is correct |
41 |
Correct |
10 ms |
2760 KB |
Output is correct |
42 |
Correct |
4 ms |
1752 KB |
Output is correct |
43 |
Correct |
4 ms |
1896 KB |
Output is correct |
44 |
Correct |
4 ms |
1800 KB |
Output is correct |
45 |
Correct |
139 ms |
23716 KB |
Output is correct |
46 |
Correct |
77 ms |
13020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
944 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1196 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
2 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1620 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1216 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1460 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
15 ms |
4716 KB |
Output is correct |
30 |
Correct |
4 ms |
2140 KB |
Output is correct |
31 |
Correct |
8 ms |
3000 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
29 ms |
8136 KB |
Output is correct |
34 |
Correct |
18 ms |
4972 KB |
Output is correct |
35 |
Correct |
8 ms |
2456 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
6 ms |
2512 KB |
Output is correct |
38 |
Correct |
11 ms |
2828 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
6 ms |
2140 KB |
Output is correct |
41 |
Correct |
10 ms |
2828 KB |
Output is correct |
42 |
Correct |
4 ms |
1752 KB |
Output is correct |
43 |
Correct |
4 ms |
2008 KB |
Output is correct |
44 |
Correct |
4 ms |
1796 KB |
Output is correct |
45 |
Correct |
140 ms |
24360 KB |
Output is correct |
46 |
Correct |
73 ms |
14036 KB |
Output is correct |
47 |
Correct |
12 ms |
3860 KB |
Output is correct |
48 |
Correct |
5 ms |
1628 KB |
Output is correct |
49 |
Correct |
4 ms |
1628 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
17 ms |
4192 KB |
Output is correct |
52 |
Correct |
20 ms |
5136 KB |
Output is correct |
53 |
Correct |
7 ms |
2304 KB |
Output is correct |
54 |
Correct |
3 ms |
2768 KB |
Output is correct |
55 |
Correct |
6 ms |
4020 KB |
Output is correct |
56 |
Correct |
12 ms |
5740 KB |
Output is correct |
57 |
Correct |
1 ms |
1116 KB |
Output is correct |
58 |
Correct |
18 ms |
8212 KB |
Output is correct |
59 |
Correct |
15 ms |
7664 KB |
Output is correct |
60 |
Correct |
16 ms |
7664 KB |
Output is correct |
61 |
Correct |
12 ms |
5612 KB |
Output is correct |
62 |
Correct |
60 ms |
17024 KB |
Output is correct |
63 |
Correct |
347 ms |
29956 KB |
Output is correct |
64 |
Correct |
367 ms |
29640 KB |
Output is correct |
65 |
Execution timed out |
1075 ms |
66884 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |