#include <bits/stdc++.h>
using namespace std;
vector<int> smallpowers[30000]; //on given skyscraper
vector<pair<int,int>> adjlist[30000]; //weight, node
int n;
int sqv;
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(sqv)+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;
set<pair<int,pair<int,int>>> pq;
pq.insert({0,{start,0}});
while (pq.size()>0){
//auto f = pq.top();
//pq.pop();
auto f = *pq.begin();
pq.erase(pq.begin());
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.insert({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.insert({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.insert({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.insert({distances[curnode-curind][curind],{curnode-curind,curind}});
}
if (curnode+curind<n&&distances[curnode+curind][curind]>curdist+1){
distances[curnode+curind][curind]=curdist+1;
pq.insert({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;
sqv = 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<sqv){
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 |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
2 ms |
1792 KB |
Output is correct |
4 |
Correct |
2 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
2 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
1 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
1 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
2 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
2 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
2 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
1 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
1 ms |
1792 KB |
Output is correct |
14 |
Correct |
2 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1920 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
5 ms |
2432 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
3 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
2048 KB |
Output is correct |
22 |
Correct |
2 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
4 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
3 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
12 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
3072 KB |
Output is correct |
31 |
Correct |
8 ms |
3328 KB |
Output is correct |
32 |
Correct |
4 ms |
3456 KB |
Output is correct |
33 |
Correct |
17 ms |
3968 KB |
Output is correct |
34 |
Correct |
17 ms |
3968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
2 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
1 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
2 ms |
1792 KB |
Output is correct |
9 |
Correct |
1 ms |
1792 KB |
Output is correct |
10 |
Correct |
1 ms |
1792 KB |
Output is correct |
11 |
Correct |
3 ms |
1920 KB |
Output is correct |
12 |
Correct |
1 ms |
1792 KB |
Output is correct |
13 |
Correct |
1 ms |
1792 KB |
Output is correct |
14 |
Correct |
2 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1920 KB |
Output is correct |
16 |
Correct |
1 ms |
1920 KB |
Output is correct |
17 |
Correct |
3 ms |
2560 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
2 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
2048 KB |
Output is correct |
22 |
Correct |
2 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
4 ms |
3200 KB |
Output is correct |
27 |
Correct |
3 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
11 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
3072 KB |
Output is correct |
31 |
Correct |
11 ms |
3328 KB |
Output is correct |
32 |
Correct |
7 ms |
3456 KB |
Output is correct |
33 |
Correct |
16 ms |
3968 KB |
Output is correct |
34 |
Correct |
17 ms |
3968 KB |
Output is correct |
35 |
Correct |
14 ms |
3456 KB |
Output is correct |
36 |
Correct |
5 ms |
2688 KB |
Output is correct |
37 |
Correct |
23 ms |
4736 KB |
Output is correct |
38 |
Correct |
23 ms |
4608 KB |
Output is correct |
39 |
Correct |
21 ms |
4608 KB |
Output is correct |
40 |
Correct |
22 ms |
4608 KB |
Output is correct |
41 |
Correct |
22 ms |
4608 KB |
Output is correct |
42 |
Correct |
8 ms |
3584 KB |
Output is correct |
43 |
Correct |
8 ms |
3584 KB |
Output is correct |
44 |
Correct |
7 ms |
3584 KB |
Output is correct |
45 |
Correct |
29 ms |
7288 KB |
Output is correct |
46 |
Correct |
26 ms |
7552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1792 KB |
Output is correct |
2 |
Correct |
1 ms |
1792 KB |
Output is correct |
3 |
Correct |
1 ms |
1792 KB |
Output is correct |
4 |
Correct |
2 ms |
1792 KB |
Output is correct |
5 |
Correct |
1 ms |
1792 KB |
Output is correct |
6 |
Correct |
2 ms |
1792 KB |
Output is correct |
7 |
Correct |
1 ms |
1792 KB |
Output is correct |
8 |
Correct |
2 ms |
1792 KB |
Output is correct |
9 |
Correct |
2 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
2 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 ms |
1792 KB |
Output is correct |
13 |
Correct |
1 ms |
1792 KB |
Output is correct |
14 |
Correct |
3 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1920 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
3 ms |
2432 KB |
Output is correct |
18 |
Correct |
2 ms |
2944 KB |
Output is correct |
19 |
Correct |
2 ms |
3200 KB |
Output is correct |
20 |
Correct |
2 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
2048 KB |
Output is correct |
22 |
Correct |
2 ms |
2944 KB |
Output is correct |
23 |
Correct |
3 ms |
2944 KB |
Output is correct |
24 |
Correct |
4 ms |
3200 KB |
Output is correct |
25 |
Correct |
3 ms |
3200 KB |
Output is correct |
26 |
Correct |
4 ms |
3200 KB |
Output is correct |
27 |
Correct |
2 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
11 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
3072 KB |
Output is correct |
31 |
Correct |
7 ms |
3328 KB |
Output is correct |
32 |
Correct |
5 ms |
3456 KB |
Output is correct |
33 |
Correct |
17 ms |
3968 KB |
Output is correct |
34 |
Correct |
16 ms |
3968 KB |
Output is correct |
35 |
Correct |
16 ms |
3456 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
19 ms |
4736 KB |
Output is correct |
38 |
Correct |
19 ms |
4608 KB |
Output is correct |
39 |
Correct |
20 ms |
4608 KB |
Output is correct |
40 |
Correct |
19 ms |
4608 KB |
Output is correct |
41 |
Correct |
19 ms |
4608 KB |
Output is correct |
42 |
Correct |
8 ms |
3584 KB |
Output is correct |
43 |
Correct |
7 ms |
3584 KB |
Output is correct |
44 |
Correct |
8 ms |
3584 KB |
Output is correct |
45 |
Correct |
25 ms |
7296 KB |
Output is correct |
46 |
Correct |
30 ms |
7680 KB |
Output is correct |
47 |
Correct |
103 ms |
16120 KB |
Output is correct |
48 |
Correct |
20 ms |
19064 KB |
Output is correct |
49 |
Correct |
20 ms |
20352 KB |
Output is correct |
50 |
Correct |
18 ms |
21760 KB |
Output is correct |
51 |
Correct |
86 ms |
25464 KB |
Output is correct |
52 |
Correct |
85 ms |
25592 KB |
Output is correct |
53 |
Correct |
28 ms |
22784 KB |
Output is correct |
54 |
Correct |
23 ms |
22272 KB |
Output is correct |
55 |
Correct |
17 ms |
22272 KB |
Output is correct |
56 |
Correct |
24 ms |
23424 KB |
Output is correct |
57 |
Correct |
98 ms |
22520 KB |
Output is correct |
58 |
Correct |
23 ms |
22656 KB |
Output is correct |
59 |
Correct |
28 ms |
22784 KB |
Output is correct |
60 |
Correct |
32 ms |
23040 KB |
Output is correct |
61 |
Correct |
29 ms |
23040 KB |
Output is correct |
62 |
Correct |
59 ms |
24952 KB |
Output is correct |
63 |
Correct |
126 ms |
50424 KB |
Output is correct |
64 |
Correct |
140 ms |
56056 KB |
Output is correct |
65 |
Correct |
399 ms |
56876 KB |
Output is correct |
66 |
Correct |
920 ms |
52344 KB |
Output is correct |
67 |
Correct |
950 ms |
52084 KB |
Output is correct |