#include<bits/stdc++.h>
using namespace std;
#define xx first
#define yy second
int n,m;
const int MAXN=30001;
const int lim=50;
int b[MAXN];
int p[MAXN];
int dst[MAXN][lim+1];
int kesz[MAXN];
vector<int> lst_small[MAXN];
vector<int> lst_big[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=0;i<m;++i) {
cin>>b[i]>>p[i];
if(p[i]<=lim) lst_small[b[i]].push_back(p[i]);
else lst_big[b[i]].push_back(p[i]);
}
for(int i=0;i<n;++i) {
lst_small[b[i]].push_back(0);
sort(lst_small[b[i]].begin(), lst_small[b[i]].end());
lst_small[b[i]].resize(distance(lst_small[b[i]].begin(), unique(lst_small[b[i]].begin(), lst_small[b[i]].end())));
sort(lst_big[b[i]].begin(), lst_big[b[i]].end());
lst_big[b[i]].resize(distance(lst_big[b[i]].begin(), unique(lst_big[b[i]].begin(), lst_big[b[i]].end())));
}
for(int i=0;i<n;++i) {
for(int j=0;j<=lim;++j) {
dst[i][j]=1e9;
}
}
dst[b[0]][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, {b[0], 0}});
while(!pq.empty()) {
auto tp=pq.top();
pq.pop();
if(tp.xx>dst[tp.yy.xx][tp.yy.yy]) continue ;
if(!kesz[tp.yy.xx]) {
for(auto i:lst_small[tp.yy.xx]) {
if(i<=lim) {
if(tp.xx<dst[tp.yy.xx][i]) {
dst[tp.yy.xx][i]=tp.xx;
pq.push({dst[tp.yy.xx][i], {tp.yy.xx, i}});
}
}
}
for(auto i:lst_big[tp.yy.xx]) {
for(int j=tp.yy.xx%i;j<n;j+=i) {
int diff=abs(j-tp.yy.xx)/i;
if(tp.xx+diff<dst[j][0]) {
dst[j][0]=tp.xx+diff;
pq.push({dst[j][0], {j,0}});
}
}
}
}
kesz[tp.yy.xx]=1;
if(tp.yy.xx-tp.yy.yy>=0 && tp.xx+1<dst[tp.yy.xx-tp.yy.yy][tp.yy.yy]) {
dst[tp.yy.xx-tp.yy.yy][tp.yy.yy]=tp.xx+1;
pq.push({dst[tp.yy.xx-tp.yy.yy][tp.yy.yy], {tp.yy.xx-tp.yy.yy, tp.yy.yy}});
}
if(tp.yy.xx+tp.yy.yy<n && tp.xx+1<dst[tp.yy.xx+tp.yy.yy][tp.yy.yy]) {
dst[tp.yy.xx+tp.yy.yy][tp.yy.yy]=tp.xx+1;
pq.push({dst[tp.yy.xx+tp.yy.yy][tp.yy.yy], {tp.yy.xx+tp.yy.yy, tp.yy.yy}});
}
}
if(dst[b[1]][0]==1e9) cout<<-1;
else cout<<dst[b[1]][0];
cout<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1784 KB |
Output is correct |
2 |
Correct |
5 ms |
1784 KB |
Output is correct |
3 |
Correct |
6 ms |
1784 KB |
Output is correct |
4 |
Correct |
5 ms |
1784 KB |
Output is correct |
5 |
Correct |
5 ms |
1784 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1784 KB |
Output is correct |
2 |
Correct |
6 ms |
1788 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1788 KB |
Output is correct |
8 |
Correct |
5 ms |
1784 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
7 ms |
1836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1784 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
5 ms |
1784 KB |
Output is correct |
5 |
Correct |
5 ms |
1784 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
5 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
7 ms |
1912 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1912 KB |
Output is correct |
15 |
Correct |
7 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1788 KB |
Output is correct |
17 |
Correct |
7 ms |
1912 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
7 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
7 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2296 KB |
Output is correct |
25 |
Correct |
7 ms |
2296 KB |
Output is correct |
26 |
Correct |
7 ms |
2168 KB |
Output is correct |
27 |
Correct |
10 ms |
2168 KB |
Output is correct |
28 |
Correct |
7 ms |
2296 KB |
Output is correct |
29 |
Correct |
13 ms |
2168 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
10 ms |
2168 KB |
Output is correct |
32 |
Correct |
9 ms |
2168 KB |
Output is correct |
33 |
Correct |
20 ms |
2424 KB |
Output is correct |
34 |
Correct |
20 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1788 KB |
Output is correct |
2 |
Correct |
5 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
5 ms |
1784 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
7 ms |
1912 KB |
Output is correct |
15 |
Correct |
7 ms |
1912 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
6 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
6 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2296 KB |
Output is correct |
25 |
Correct |
7 ms |
2296 KB |
Output is correct |
26 |
Correct |
6 ms |
2168 KB |
Output is correct |
27 |
Correct |
10 ms |
2168 KB |
Output is correct |
28 |
Correct |
7 ms |
2296 KB |
Output is correct |
29 |
Correct |
13 ms |
2296 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
10 ms |
2168 KB |
Output is correct |
32 |
Correct |
8 ms |
2172 KB |
Output is correct |
33 |
Correct |
20 ms |
2296 KB |
Output is correct |
34 |
Correct |
20 ms |
2424 KB |
Output is correct |
35 |
Correct |
18 ms |
2760 KB |
Output is correct |
36 |
Correct |
8 ms |
2168 KB |
Output is correct |
37 |
Correct |
20 ms |
2552 KB |
Output is correct |
38 |
Correct |
20 ms |
2680 KB |
Output is correct |
39 |
Correct |
21 ms |
2680 KB |
Output is correct |
40 |
Correct |
21 ms |
2680 KB |
Output is correct |
41 |
Correct |
21 ms |
2680 KB |
Output is correct |
42 |
Correct |
12 ms |
2680 KB |
Output is correct |
43 |
Correct |
17 ms |
2680 KB |
Output is correct |
44 |
Correct |
11 ms |
2680 KB |
Output is correct |
45 |
Correct |
31 ms |
2876 KB |
Output is correct |
46 |
Correct |
31 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1788 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
5 ms |
1784 KB |
Output is correct |
5 |
Correct |
5 ms |
1784 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
5 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
7 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
8 ms |
2040 KB |
Output is correct |
18 |
Correct |
6 ms |
2168 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
7 ms |
2296 KB |
Output is correct |
21 |
Correct |
6 ms |
1912 KB |
Output is correct |
22 |
Correct |
6 ms |
2168 KB |
Output is correct |
23 |
Correct |
6 ms |
2168 KB |
Output is correct |
24 |
Correct |
7 ms |
2296 KB |
Output is correct |
25 |
Correct |
7 ms |
2296 KB |
Output is correct |
26 |
Correct |
7 ms |
2168 KB |
Output is correct |
27 |
Correct |
10 ms |
2168 KB |
Output is correct |
28 |
Correct |
7 ms |
2296 KB |
Output is correct |
29 |
Correct |
15 ms |
2168 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
10 ms |
2168 KB |
Output is correct |
32 |
Correct |
8 ms |
2168 KB |
Output is correct |
33 |
Correct |
20 ms |
2364 KB |
Output is correct |
34 |
Correct |
20 ms |
2296 KB |
Output is correct |
35 |
Correct |
18 ms |
2552 KB |
Output is correct |
36 |
Correct |
8 ms |
2168 KB |
Output is correct |
37 |
Correct |
20 ms |
2552 KB |
Output is correct |
38 |
Correct |
21 ms |
2680 KB |
Output is correct |
39 |
Correct |
22 ms |
2680 KB |
Output is correct |
40 |
Correct |
21 ms |
2680 KB |
Output is correct |
41 |
Correct |
23 ms |
2680 KB |
Output is correct |
42 |
Correct |
11 ms |
2680 KB |
Output is correct |
43 |
Correct |
14 ms |
2680 KB |
Output is correct |
44 |
Correct |
11 ms |
2680 KB |
Output is correct |
45 |
Correct |
31 ms |
2872 KB |
Output is correct |
46 |
Correct |
30 ms |
2936 KB |
Output is correct |
47 |
Correct |
68 ms |
5560 KB |
Output is correct |
48 |
Correct |
18 ms |
7544 KB |
Output is correct |
49 |
Correct |
18 ms |
7928 KB |
Output is correct |
50 |
Correct |
15 ms |
8184 KB |
Output is correct |
51 |
Correct |
57 ms |
9784 KB |
Output is correct |
52 |
Correct |
59 ms |
9784 KB |
Output is correct |
53 |
Correct |
38 ms |
9208 KB |
Output is correct |
54 |
Correct |
11 ms |
7800 KB |
Output is correct |
55 |
Correct |
11 ms |
7932 KB |
Output is correct |
56 |
Correct |
23 ms |
8952 KB |
Output is correct |
57 |
Correct |
101 ms |
8376 KB |
Output is correct |
58 |
Correct |
19 ms |
8312 KB |
Output is correct |
59 |
Correct |
22 ms |
8312 KB |
Output is correct |
60 |
Correct |
27 ms |
8440 KB |
Output is correct |
61 |
Correct |
634 ms |
8440 KB |
Output is correct |
62 |
Correct |
36 ms |
9976 KB |
Output is correct |
63 |
Correct |
79 ms |
9840 KB |
Output is correct |
64 |
Correct |
91 ms |
9836 KB |
Output is correct |
65 |
Correct |
112 ms |
11368 KB |
Output is correct |
66 |
Correct |
375 ms |
11368 KB |
Output is correct |
67 |
Correct |
374 ms |
11496 KB |
Output is correct |