#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
priority_queue<pair<int,int>> q1;
const int MAXN = 30010;
set<int> v1[MAXN];
vector<pair<int,int>> g[MAXN];
bool visited[MAXN];
int dp[MAXN];
int target;
int first,jump;
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int b,p;
cin>>b>>p;
if(i== 0){
first = b;
jump = p;
}
if(i == 1){
target = b;
}
v1[b].insert(p);
}
for(int i=0;i<=n;i++){
for(int x:v1[i]){
int b = i;
int p = x;
int count = 1;
for(int j=b+p;j<=n;j+=p){
// cout<<i<<" "<<j<<endl;
if(v1[j].find(p) != v1[j].end()){
g[b].push_back(make_pair(j,count));
break;
}
g[b].push_back(make_pair(j,count));
count++;
}
count = 1;
for(int j=b-p;j>=0;j-=p){
if(v1[j].find(p) != v1[j].end()){
g[b].push_back(make_pair(j,count));
break;
}
g[b].push_back(make_pair(j,count));
count++;
}
}
}
q1.push(make_pair(0,first));
for(int i=0;i<=n;i++){
dp[i] = 1e9;
}
dp[first] = 0;
while(!q1.empty()){
auto hold = q1.top();
int skyscraper = hold.second;
int dist = -1*hold.first;
q1.pop();
if(skyscraper == target){
cout<<dist<<endl;
return 0;
}
if(visited[skyscraper]){
continue;
}
visited[skyscraper] = true;
for(auto x:g[skyscraper]){
int w = x.second;
int to = x.first;
if(dp[to]>dp[skyscraper]+w){
dp[to] = dp[skyscraper]+w;
q1.push(make_pair(-1*dp[to],to));
}
}
}
cout<<-1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
4 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2560 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
3 ms |
2432 KB |
Output is correct |
5 |
Correct |
5 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
4 ms |
2432 KB |
Output is correct |
9 |
Correct |
5 ms |
2432 KB |
Output is correct |
10 |
Correct |
4 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2560 KB |
Output is correct |
12 |
Correct |
4 ms |
2432 KB |
Output is correct |
13 |
Correct |
5 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2608 KB |
Output is correct |
15 |
Correct |
5 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
4 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
4 ms |
2404 KB |
Output is correct |
9 |
Correct |
4 ms |
2432 KB |
Output is correct |
10 |
Correct |
5 ms |
2560 KB |
Output is correct |
11 |
Correct |
5 ms |
2560 KB |
Output is correct |
12 |
Correct |
5 ms |
2432 KB |
Output is correct |
13 |
Correct |
6 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2664 KB |
Output is correct |
15 |
Correct |
6 ms |
2560 KB |
Output is correct |
16 |
Correct |
6 ms |
2560 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
5 ms |
2560 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
8 ms |
2688 KB |
Output is correct |
26 |
Correct |
7 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2560 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
9 ms |
3840 KB |
Output is correct |
34 |
Correct |
9 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
5 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
5 ms |
2432 KB |
Output is correct |
7 |
Correct |
3 ms |
2432 KB |
Output is correct |
8 |
Correct |
5 ms |
2432 KB |
Output is correct |
9 |
Correct |
4 ms |
2432 KB |
Output is correct |
10 |
Correct |
5 ms |
2432 KB |
Output is correct |
11 |
Correct |
5 ms |
2560 KB |
Output is correct |
12 |
Correct |
5 ms |
2432 KB |
Output is correct |
13 |
Correct |
5 ms |
2432 KB |
Output is correct |
14 |
Correct |
5 ms |
2560 KB |
Output is correct |
15 |
Correct |
5 ms |
2560 KB |
Output is correct |
16 |
Correct |
5 ms |
2560 KB |
Output is correct |
17 |
Correct |
6 ms |
2688 KB |
Output is correct |
18 |
Correct |
4 ms |
2560 KB |
Output is correct |
19 |
Correct |
4 ms |
2560 KB |
Output is correct |
20 |
Correct |
5 ms |
2560 KB |
Output is correct |
21 |
Correct |
5 ms |
2560 KB |
Output is correct |
22 |
Correct |
5 ms |
2560 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2560 KB |
Output is correct |
26 |
Correct |
6 ms |
2560 KB |
Output is correct |
27 |
Correct |
6 ms |
2560 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
5 ms |
2816 KB |
Output is correct |
33 |
Correct |
11 ms |
3840 KB |
Output is correct |
34 |
Correct |
10 ms |
3840 KB |
Output is correct |
35 |
Correct |
61 ms |
4616 KB |
Output is correct |
36 |
Correct |
9 ms |
2816 KB |
Output is correct |
37 |
Correct |
38 ms |
5496 KB |
Output is correct |
38 |
Correct |
49 ms |
5584 KB |
Output is correct |
39 |
Correct |
55 ms |
5624 KB |
Output is correct |
40 |
Correct |
46 ms |
5624 KB |
Output is correct |
41 |
Correct |
48 ms |
5624 KB |
Output is correct |
42 |
Correct |
29 ms |
2808 KB |
Output is correct |
43 |
Correct |
18 ms |
2688 KB |
Output is correct |
44 |
Correct |
18 ms |
2816 KB |
Output is correct |
45 |
Correct |
47 ms |
8440 KB |
Output is correct |
46 |
Correct |
45 ms |
8440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
4 ms |
2432 KB |
Output is correct |
5 |
Correct |
5 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
4 ms |
2432 KB |
Output is correct |
9 |
Correct |
5 ms |
2432 KB |
Output is correct |
10 |
Correct |
5 ms |
2432 KB |
Output is correct |
11 |
Correct |
5 ms |
2560 KB |
Output is correct |
12 |
Correct |
4 ms |
2432 KB |
Output is correct |
13 |
Correct |
5 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2560 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
5 ms |
2560 KB |
Output is correct |
17 |
Correct |
5 ms |
2688 KB |
Output is correct |
18 |
Correct |
4 ms |
2560 KB |
Output is correct |
19 |
Correct |
5 ms |
2560 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
4 ms |
2560 KB |
Output is correct |
23 |
Correct |
5 ms |
2560 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
7 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2560 KB |
Output is correct |
27 |
Correct |
7 ms |
2560 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
9 ms |
3840 KB |
Output is correct |
34 |
Correct |
8 ms |
3840 KB |
Output is correct |
35 |
Correct |
35 ms |
4728 KB |
Output is correct |
36 |
Correct |
10 ms |
2816 KB |
Output is correct |
37 |
Correct |
37 ms |
5372 KB |
Output is correct |
38 |
Correct |
48 ms |
5624 KB |
Output is correct |
39 |
Correct |
42 ms |
5624 KB |
Output is correct |
40 |
Correct |
52 ms |
5624 KB |
Output is correct |
41 |
Correct |
45 ms |
5624 KB |
Output is correct |
42 |
Correct |
18 ms |
2816 KB |
Output is correct |
43 |
Correct |
18 ms |
2788 KB |
Output is correct |
44 |
Correct |
21 ms |
2816 KB |
Output is correct |
45 |
Correct |
39 ms |
8376 KB |
Output is correct |
46 |
Correct |
41 ms |
8440 KB |
Output is correct |
47 |
Correct |
62 ms |
12796 KB |
Output is correct |
48 |
Correct |
41 ms |
6260 KB |
Output is correct |
49 |
Correct |
37 ms |
5240 KB |
Output is correct |
50 |
Correct |
27 ms |
4856 KB |
Output is correct |
51 |
Correct |
61 ms |
8324 KB |
Output is correct |
52 |
Correct |
68 ms |
8768 KB |
Output is correct |
53 |
Correct |
46 ms |
5504 KB |
Output is correct |
54 |
Correct |
8 ms |
3072 KB |
Output is correct |
55 |
Correct |
9 ms |
3580 KB |
Output is correct |
56 |
Correct |
44 ms |
5124 KB |
Output is correct |
57 |
Correct |
26 ms |
11384 KB |
Output is correct |
58 |
Correct |
19 ms |
3704 KB |
Output is correct |
59 |
Correct |
29 ms |
4212 KB |
Output is correct |
60 |
Correct |
25 ms |
4852 KB |
Output is correct |
61 |
Correct |
26 ms |
4224 KB |
Output is correct |
62 |
Correct |
50 ms |
9464 KB |
Output is correct |
63 |
Correct |
93 ms |
25688 KB |
Output is correct |
64 |
Correct |
102 ms |
28500 KB |
Output is correct |
65 |
Correct |
126 ms |
38440 KB |
Output is correct |
66 |
Correct |
216 ms |
65544 KB |
Output is correct |
67 |
Correct |
225 ms |
65636 KB |
Output is correct |