#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> smallpowers; //on given skyscraper
vector<vector<pair<int,int>>> adjlist; //weight, node
int n;
int INF = 1e9;
int distances[30000][175];
int dijkstra(int startloc, int endloc){
int start = startloc;
for (int i = 0; i<n; i++){
for (int j = 0; j<int(sqrt(n))+1; j++){
distances[i][j]=INF;
}
}
distances[start][0]=0;
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,less<pair<int,pair<int,int>>>> pq;
pq.push({0,{start,0}});
while (pq.size()>0){
auto f = pq.top();
pq.pop();
if (distances[f.second.first][f.second.second]<f.first){
continue;
}
int curdist = f.first;
int curnode = f.second.first;
int curind = f.second.second;
if (curind==0){
// we can go to a doge with cost 0
for (int i : smallpowers[curnode]){
if (distances[curnode][i]>curdist){
distances[curnode][i]=curdist;
pq.push({distances[curnode][i],{curnode,i}});
}
}
// or we can jump skyscrapers
for (auto p : adjlist[curnode]){
if (distances[p.second][0]>curdist+p.first){
distances[p.second][0]=curdist+p.first;
pq.push({distances[p.second][0],{p.second,0}});
}
}
} else {
//we can go to central node with cost 0
if (distances[curnode][0]>curdist){
distances[curnode][0]=curdist;
pq.push({distances[curnode][0],{curnode,0}});
}
//or we can go to an adjacent node
if (curnode-curind>=0&&distances[curnode-curind][curind]>curdist+1){
distances[curnode-curind][curind]=curdist+1;
pq.push({distances[curnode-curind][curind],{curnode-curind,curind}});
}
if (curnode+curind<n&&distances[curnode+curind][curind]>curdist+1){
distances[curnode+curind][curind]=curdist+1;
pq.push({distances[curnode+curind][curind],{curnode+curind,curind}});
}
}
}
return distances[endloc][0];
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int m;
cin>>n>>m;
int sqrtv = sqrt(n);
smallpowers.resize(n);
adjlist.resize(n);
int startloc = 0;
int endloc = 0;
for (int i = 0; i<m; i++){
int b,p;
cin>>b>>p;
if (i==0){
startloc=b;
}
if (i==1){
endloc=b;
}
//skyscraper b, moves p
if (p<sqrtv){
smallpowers[b].push_back(p);
} else {
int cnt = 0;
for (int x = b+p; x<n; x+=p){
cnt++;
adjlist[b].push_back({cnt,x});
}
cnt = 0;
for (int x = b-p; x>=0; x-=p){
cnt++;
adjlist[b].push_back({cnt,x});
}
}
}
int d = dijkstra(startloc,endloc);
if (d==INF){
cout<<-1<<'\n';
} else {
cout<<d<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
108 ms |
1048 KB |
Output is correct |
18 |
Correct |
2 ms |
1664 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
3 ms |
1920 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
1664 KB |
Output is correct |
23 |
Correct |
12 ms |
1536 KB |
Output is correct |
24 |
Correct |
86 ms |
1884 KB |
Output is correct |
25 |
Correct |
11 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
3 ms |
1920 KB |
Output is correct |
28 |
Correct |
4 ms |
1920 KB |
Output is correct |
29 |
Correct |
6 ms |
1792 KB |
Output is correct |
30 |
Correct |
4 ms |
1792 KB |
Output is correct |
31 |
Correct |
4 ms |
1920 KB |
Output is correct |
32 |
Correct |
3 ms |
2048 KB |
Output is correct |
33 |
Correct |
8 ms |
2304 KB |
Output is correct |
34 |
Correct |
23 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
111 ms |
1024 KB |
Output is correct |
18 |
Correct |
2 ms |
1664 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
3 ms |
1920 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
1664 KB |
Output is correct |
23 |
Correct |
12 ms |
1536 KB |
Output is correct |
24 |
Correct |
89 ms |
1792 KB |
Output is correct |
25 |
Correct |
10 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
3 ms |
1920 KB |
Output is correct |
28 |
Correct |
4 ms |
1920 KB |
Output is correct |
29 |
Correct |
6 ms |
1792 KB |
Output is correct |
30 |
Correct |
3 ms |
1792 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
3 ms |
2048 KB |
Output is correct |
33 |
Correct |
7 ms |
2304 KB |
Output is correct |
34 |
Correct |
25 ms |
2304 KB |
Output is correct |
35 |
Execution timed out |
1008 ms |
2048 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
126 ms |
1024 KB |
Output is correct |
18 |
Correct |
2 ms |
1664 KB |
Output is correct |
19 |
Correct |
2 ms |
1792 KB |
Output is correct |
20 |
Correct |
3 ms |
1920 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
1664 KB |
Output is correct |
23 |
Correct |
12 ms |
1536 KB |
Output is correct |
24 |
Correct |
91 ms |
1792 KB |
Output is correct |
25 |
Correct |
15 ms |
1920 KB |
Output is correct |
26 |
Correct |
4 ms |
1920 KB |
Output is correct |
27 |
Correct |
3 ms |
1920 KB |
Output is correct |
28 |
Correct |
4 ms |
1920 KB |
Output is correct |
29 |
Correct |
8 ms |
1792 KB |
Output is correct |
30 |
Correct |
3 ms |
1792 KB |
Output is correct |
31 |
Correct |
4 ms |
1920 KB |
Output is correct |
32 |
Correct |
4 ms |
2048 KB |
Output is correct |
33 |
Correct |
7 ms |
2304 KB |
Output is correct |
34 |
Correct |
23 ms |
2304 KB |
Output is correct |
35 |
Execution timed out |
1029 ms |
2168 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |