#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][1750];
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>>>,greater<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;
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';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
1920 KB |
Output is correct |
9 |
Correct |
1 ms |
1920 KB |
Output is correct |
10 |
Correct |
2 ms |
2176 KB |
Output is correct |
11 |
Correct |
2 ms |
2176 KB |
Output is correct |
12 |
Correct |
2 ms |
2176 KB |
Output is correct |
13 |
Correct |
1 ms |
2176 KB |
Output is correct |
14 |
Correct |
2 ms |
2176 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1920 KB |
Output is correct |
9 |
Correct |
1 ms |
2048 KB |
Output is correct |
10 |
Correct |
1 ms |
2176 KB |
Output is correct |
11 |
Correct |
2 ms |
2176 KB |
Output is correct |
12 |
Correct |
2 ms |
2176 KB |
Output is correct |
13 |
Correct |
2 ms |
2176 KB |
Output is correct |
14 |
Correct |
2 ms |
2176 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
16 |
Correct |
2 ms |
2560 KB |
Output is correct |
17 |
Correct |
4 ms |
4864 KB |
Output is correct |
18 |
Correct |
5 ms |
9088 KB |
Output is correct |
19 |
Correct |
6 ms |
10112 KB |
Output is correct |
20 |
Correct |
6 ms |
10240 KB |
Output is correct |
21 |
Correct |
2 ms |
3328 KB |
Output is correct |
22 |
Correct |
5 ms |
9216 KB |
Output is correct |
23 |
Correct |
5 ms |
8448 KB |
Output is correct |
24 |
Correct |
6 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
10112 KB |
Output is correct |
26 |
Correct |
6 ms |
10112 KB |
Output is correct |
27 |
Correct |
7 ms |
10112 KB |
Output is correct |
28 |
Correct |
6 ms |
10240 KB |
Output is correct |
29 |
Correct |
12 ms |
10112 KB |
Output is correct |
30 |
Correct |
7 ms |
10112 KB |
Output is correct |
31 |
Correct |
9 ms |
10240 KB |
Output is correct |
32 |
Correct |
7 ms |
10368 KB |
Output is correct |
33 |
Correct |
25 ms |
10752 KB |
Output is correct |
34 |
Correct |
18 ms |
10752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1 ms |
1920 KB |
Output is correct |
9 |
Correct |
1 ms |
2048 KB |
Output is correct |
10 |
Correct |
1 ms |
2176 KB |
Output is correct |
11 |
Correct |
2 ms |
2176 KB |
Output is correct |
12 |
Correct |
2 ms |
2176 KB |
Output is correct |
13 |
Correct |
2 ms |
2176 KB |
Output is correct |
14 |
Correct |
3 ms |
2176 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
16 |
Correct |
2 ms |
2560 KB |
Output is correct |
17 |
Correct |
4 ms |
4864 KB |
Output is correct |
18 |
Correct |
5 ms |
9088 KB |
Output is correct |
19 |
Correct |
5 ms |
10112 KB |
Output is correct |
20 |
Correct |
6 ms |
10240 KB |
Output is correct |
21 |
Correct |
3 ms |
3328 KB |
Output is correct |
22 |
Correct |
5 ms |
9216 KB |
Output is correct |
23 |
Correct |
5 ms |
8448 KB |
Output is correct |
24 |
Correct |
6 ms |
9728 KB |
Output is correct |
25 |
Correct |
6 ms |
10112 KB |
Output is correct |
26 |
Correct |
6 ms |
10112 KB |
Output is correct |
27 |
Correct |
6 ms |
10112 KB |
Output is correct |
28 |
Correct |
6 ms |
10240 KB |
Output is correct |
29 |
Correct |
13 ms |
10112 KB |
Output is correct |
30 |
Correct |
7 ms |
10112 KB |
Output is correct |
31 |
Correct |
10 ms |
10240 KB |
Output is correct |
32 |
Correct |
10 ms |
10368 KB |
Output is correct |
33 |
Correct |
17 ms |
10752 KB |
Output is correct |
34 |
Correct |
25 ms |
10752 KB |
Output is correct |
35 |
Correct |
16 ms |
8448 KB |
Output is correct |
36 |
Correct |
5 ms |
6400 KB |
Output is correct |
37 |
Correct |
20 ms |
11264 KB |
Output is correct |
38 |
Correct |
21 ms |
11264 KB |
Output is correct |
39 |
Correct |
21 ms |
11264 KB |
Output is correct |
40 |
Correct |
21 ms |
11264 KB |
Output is correct |
41 |
Correct |
22 ms |
11392 KB |
Output is correct |
42 |
Correct |
10 ms |
10368 KB |
Output is correct |
43 |
Correct |
11 ms |
10368 KB |
Output is correct |
44 |
Correct |
10 ms |
10368 KB |
Output is correct |
45 |
Correct |
28 ms |
14204 KB |
Output is correct |
46 |
Correct |
27 ms |
14336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
1920 KB |
Output is correct |
9 |
Correct |
2 ms |
1920 KB |
Output is correct |
10 |
Correct |
1 ms |
2176 KB |
Output is correct |
11 |
Correct |
2 ms |
2176 KB |
Output is correct |
12 |
Correct |
2 ms |
2176 KB |
Output is correct |
13 |
Correct |
2 ms |
2176 KB |
Output is correct |
14 |
Correct |
2 ms |
2176 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
16 |
Correct |
2 ms |
2560 KB |
Output is correct |
17 |
Correct |
4 ms |
4864 KB |
Output is correct |
18 |
Correct |
5 ms |
9088 KB |
Output is correct |
19 |
Correct |
5 ms |
10112 KB |
Output is correct |
20 |
Correct |
6 ms |
10112 KB |
Output is correct |
21 |
Correct |
2 ms |
3328 KB |
Output is correct |
22 |
Correct |
5 ms |
9216 KB |
Output is correct |
23 |
Correct |
5 ms |
8448 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
6 ms |
10112 KB |
Output is correct |
26 |
Correct |
6 ms |
10112 KB |
Output is correct |
27 |
Correct |
6 ms |
10112 KB |
Output is correct |
28 |
Correct |
6 ms |
10240 KB |
Output is correct |
29 |
Correct |
12 ms |
10112 KB |
Output is correct |
30 |
Correct |
7 ms |
10112 KB |
Output is correct |
31 |
Correct |
10 ms |
10240 KB |
Output is correct |
32 |
Correct |
7 ms |
10368 KB |
Output is correct |
33 |
Correct |
17 ms |
10752 KB |
Output is correct |
34 |
Correct |
18 ms |
10752 KB |
Output is correct |
35 |
Correct |
14 ms |
8448 KB |
Output is correct |
36 |
Correct |
5 ms |
6400 KB |
Output is correct |
37 |
Correct |
20 ms |
11264 KB |
Output is correct |
38 |
Correct |
21 ms |
11264 KB |
Output is correct |
39 |
Correct |
23 ms |
11264 KB |
Output is correct |
40 |
Correct |
21 ms |
11264 KB |
Output is correct |
41 |
Correct |
21 ms |
11392 KB |
Output is correct |
42 |
Correct |
11 ms |
10368 KB |
Output is correct |
43 |
Correct |
11 ms |
10368 KB |
Output is correct |
44 |
Correct |
11 ms |
10368 KB |
Output is correct |
45 |
Correct |
28 ms |
14208 KB |
Output is correct |
46 |
Correct |
28 ms |
14336 KB |
Output is correct |
47 |
Correct |
123 ms |
61496 KB |
Output is correct |
48 |
Correct |
63 ms |
109816 KB |
Output is correct |
49 |
Correct |
66 ms |
120184 KB |
Output is correct |
50 |
Correct |
70 ms |
132600 KB |
Output is correct |
51 |
Correct |
126 ms |
144444 KB |
Output is correct |
52 |
Correct |
131 ms |
144440 KB |
Output is correct |
53 |
Correct |
80 ms |
142456 KB |
Output is correct |
54 |
Correct |
72 ms |
142328 KB |
Output is correct |
55 |
Correct |
66 ms |
142332 KB |
Output is correct |
56 |
Correct |
80 ms |
143224 KB |
Output is correct |
57 |
Correct |
155 ms |
142328 KB |
Output is correct |
58 |
Correct |
75 ms |
142584 KB |
Output is correct |
59 |
Correct |
86 ms |
142584 KB |
Output is correct |
60 |
Correct |
86 ms |
142840 KB |
Output is correct |
61 |
Correct |
77 ms |
142792 KB |
Output is correct |
62 |
Correct |
107 ms |
144632 KB |
Output is correct |
63 |
Correct |
161 ms |
167216 KB |
Output is correct |
64 |
Correct |
177 ms |
175348 KB |
Output is correct |
65 |
Correct |
429 ms |
174128 KB |
Output is correct |
66 |
Correct |
989 ms |
170028 KB |
Output is correct |
67 |
Correct |
974 ms |
169900 KB |
Output is correct |