#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 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';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
1792 KB |
Output is correct |
6 |
Correct |
1 ms |
1792 KB |
Output is correct |
7 |
Correct |
2 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 |
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 |
3 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 |
3 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1792 KB |
Output is correct |
2 |
Correct |
2 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 |
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 |
4 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 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 |
3 ms |
1920 KB |
Output is correct |
16 |
Correct |
1 ms |
1920 KB |
Output is correct |
17 |
Correct |
113 ms |
2428 KB |
Output is correct |
18 |
Correct |
3 ms |
2944 KB |
Output is correct |
19 |
Correct |
3 ms |
3200 KB |
Output is correct |
20 |
Correct |
4 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
2048 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
91 ms |
3200 KB |
Output is correct |
25 |
Correct |
11 ms |
3200 KB |
Output is correct |
26 |
Correct |
5 ms |
3200 KB |
Output is correct |
27 |
Correct |
4 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3292 KB |
Output is correct |
29 |
Correct |
6 ms |
3072 KB |
Output is correct |
30 |
Correct |
5 ms |
3072 KB |
Output is correct |
31 |
Correct |
4 ms |
3200 KB |
Output is correct |
32 |
Correct |
4 ms |
3328 KB |
Output is correct |
33 |
Correct |
9 ms |
3584 KB |
Output is correct |
34 |
Correct |
22 ms |
3584 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 |
2 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 |
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 |
1 ms |
1792 KB |
Output is correct |
13 |
Correct |
2 ms |
1792 KB |
Output is correct |
14 |
Correct |
1 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 |
107 ms |
2424 KB |
Output is correct |
18 |
Correct |
3 ms |
2944 KB |
Output is correct |
19 |
Correct |
3 ms |
3200 KB |
Output is correct |
20 |
Correct |
4 ms |
3200 KB |
Output is correct |
21 |
Correct |
1 ms |
2048 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
12 ms |
2944 KB |
Output is correct |
24 |
Correct |
86 ms |
3200 KB |
Output is correct |
25 |
Correct |
10 ms |
3200 KB |
Output is correct |
26 |
Correct |
4 ms |
3200 KB |
Output is correct |
27 |
Correct |
4 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
6 ms |
3072 KB |
Output is correct |
30 |
Correct |
4 ms |
3072 KB |
Output is correct |
31 |
Correct |
5 ms |
3200 KB |
Output is correct |
32 |
Correct |
4 ms |
3328 KB |
Output is correct |
33 |
Correct |
8 ms |
3584 KB |
Output is correct |
34 |
Correct |
23 ms |
3584 KB |
Output is correct |
35 |
Execution timed out |
1018 ms |
3576 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
1792 KB |
Output is correct |
5 |
Correct |
2 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 |
1792 KB |
Output is correct |
9 |
Correct |
5 ms |
1792 KB |
Output is correct |
10 |
Correct |
2 ms |
1792 KB |
Output is correct |
11 |
Correct |
3 ms |
1920 KB |
Output is correct |
12 |
Correct |
2 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 |
16 |
Correct |
1 ms |
1920 KB |
Output is correct |
17 |
Correct |
110 ms |
2304 KB |
Output is correct |
18 |
Correct |
3 ms |
2944 KB |
Output is correct |
19 |
Correct |
3 ms |
3200 KB |
Output is correct |
20 |
Correct |
4 ms |
3200 KB |
Output is correct |
21 |
Correct |
2 ms |
2048 KB |
Output is correct |
22 |
Correct |
3 ms |
2944 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
82 ms |
3200 KB |
Output is correct |
25 |
Correct |
11 ms |
3200 KB |
Output is correct |
26 |
Correct |
5 ms |
3200 KB |
Output is correct |
27 |
Correct |
4 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
3200 KB |
Output is correct |
29 |
Correct |
7 ms |
3072 KB |
Output is correct |
30 |
Correct |
4 ms |
3072 KB |
Output is correct |
31 |
Correct |
5 ms |
3200 KB |
Output is correct |
32 |
Correct |
5 ms |
3328 KB |
Output is correct |
33 |
Correct |
8 ms |
3584 KB |
Output is correct |
34 |
Correct |
24 ms |
3584 KB |
Output is correct |
35 |
Correct |
995 ms |
3576 KB |
Output is correct |
36 |
Correct |
131 ms |
2688 KB |
Output is correct |
37 |
Execution timed out |
1090 ms |
4608 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |