#include <bits/stdc++.h>
using namespace std;
#define int long long
priority_queue<pair<int,pair<int,int>>> q;
vector<int> buildings[30001];
int power[30001];
bool visited[6000000];
const int INF = 1e15;
int n;
const int threshold = 172;
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[174*i-173]) {
q.emplace(dist - (abs(building - i) / power[x]), make_pair(i,0));
}
} else {
if(!visited[building*174-173+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[174*curr.second.first-173+curr.second.second])continue;
visited[174*curr.second.first-173+curr.second.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*174-173])q.emplace(curr.first, make_pair(curr.second.first,0));
if(curr.second.first+curr.second.second<n and !visited[174*(curr.second.first+curr.second.second)-173+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[174*(curr.second.first-curr.second.second)-173+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 |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2904 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2904 KB |
Output is correct |
14 |
Runtime error |
5 ms |
5976 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
1 ms |
3112 KB |
Output is correct |
6 |
Correct |
1 ms |
2908 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
3160 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2932 KB |
Output is correct |
7 |
Correct |
1 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |