#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define sz size()
const int N = 3e4 + 5;
using namespace std;
int n, m, b[N], p[N];
vector <pii> v[N];
priority_queue <pii> pq;
vector <int> dis(N,1e9), q[N];
map <pii,int> mp;
void f(){
pq.push({0,b[1]});
dis[b[1]] = 0;
while(!pq.empty()){
int a = pq.top().ss;
pq.pop();
for(auto i: v[a]){
if(dis[a] + i.ss < dis[i.ff]){
dis[i.ff] = dis[a] + i.ss;
pq.push({-dis[i.ff],i.ff});
}
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
if(mp.find({b[i],p[i]}) == mp.end()){
mp[{b[i],p[i]}] = 1;
q[b[i]].pb(p[i]);
}
}
for(int i = 0; i < n; i++){
for(auto j: q[i]){
int nd = i, cnt = 0;
while(1){
nd -= j;
if(nd < 0) break;
cnt++;
v[i].pb({nd,cnt});
if(mp.find({nd,j}) != mp.end()) break;
}
nd = i, cnt = 0;
while(1){
nd += j;
if(nd > n-1) break;
cnt++;
v[i].pb({nd,cnt});
if(mp.find({nd,j}) != mp.end()) break;
}
}
}
f();
cout << (dis[b[2]] == 1e9 ? -1 : dis[b[2]]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1996 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1908 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1992 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1996 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1896 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1892 KB |
Output is correct |
8 |
Correct |
1 ms |
1896 KB |
Output is correct |
9 |
Correct |
2 ms |
1896 KB |
Output is correct |
10 |
Correct |
1 ms |
1888 KB |
Output is correct |
11 |
Correct |
2 ms |
2148 KB |
Output is correct |
12 |
Correct |
1 ms |
1892 KB |
Output is correct |
13 |
Correct |
1 ms |
1892 KB |
Output is correct |
14 |
Correct |
2 ms |
2184 KB |
Output is correct |
15 |
Correct |
2 ms |
2144 KB |
Output is correct |
16 |
Correct |
2 ms |
1896 KB |
Output is correct |
17 |
Correct |
3 ms |
2300 KB |
Output is correct |
18 |
Correct |
2 ms |
2156 KB |
Output is correct |
19 |
Correct |
1 ms |
1888 KB |
Output is correct |
20 |
Correct |
2 ms |
2144 KB |
Output is correct |
21 |
Correct |
2 ms |
1892 KB |
Output is correct |
22 |
Correct |
2 ms |
1896 KB |
Output is correct |
23 |
Correct |
3 ms |
2152 KB |
Output is correct |
24 |
Correct |
3 ms |
2156 KB |
Output is correct |
25 |
Correct |
3 ms |
2180 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
4 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2392 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3164 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1880 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
1880 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
2 ms |
2004 KB |
Output is correct |
16 |
Correct |
2 ms |
2000 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
2 ms |
2140 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
2140 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
2 ms |
1884 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2136 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
4 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
2648 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2136 KB |
Output is correct |
33 |
Correct |
5 ms |
3160 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
35 |
Correct |
27 ms |
5260 KB |
Output is correct |
36 |
Correct |
6 ms |
2664 KB |
Output is correct |
37 |
Correct |
35 ms |
5604 KB |
Output is correct |
38 |
Correct |
38 ms |
6356 KB |
Output is correct |
39 |
Correct |
41 ms |
6248 KB |
Output is correct |
40 |
Correct |
40 ms |
6228 KB |
Output is correct |
41 |
Correct |
39 ms |
6352 KB |
Output is correct |
42 |
Correct |
5 ms |
2396 KB |
Output is correct |
43 |
Correct |
5 ms |
2396 KB |
Output is correct |
44 |
Correct |
5 ms |
2516 KB |
Output is correct |
45 |
Correct |
26 ms |
8792 KB |
Output is correct |
46 |
Correct |
26 ms |
8660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 KB |
Output is correct |
3 |
Correct |
2 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
2000 KB |
Output is correct |
9 |
Correct |
2 ms |
2140 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
2136 KB |
Output is correct |
15 |
Correct |
2 ms |
2140 KB |
Output is correct |
16 |
Correct |
2 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
2 ms |
2136 KB |
Output is correct |
19 |
Correct |
2 ms |
1920 KB |
Output is correct |
20 |
Correct |
2 ms |
2140 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
2 ms |
1880 KB |
Output is correct |
23 |
Correct |
2 ms |
2136 KB |
Output is correct |
24 |
Correct |
4 ms |
2392 KB |
Output is correct |
25 |
Correct |
4 ms |
2140 KB |
Output is correct |
26 |
Correct |
2 ms |
2140 KB |
Output is correct |
27 |
Correct |
2 ms |
2136 KB |
Output is correct |
28 |
Correct |
3 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
2504 KB |
Output is correct |
30 |
Correct |
2 ms |
2140 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2140 KB |
Output is correct |
33 |
Correct |
5 ms |
3164 KB |
Output is correct |
34 |
Correct |
5 ms |
3420 KB |
Output is correct |
35 |
Correct |
27 ms |
5208 KB |
Output is correct |
36 |
Correct |
5 ms |
2396 KB |
Output is correct |
37 |
Correct |
36 ms |
5764 KB |
Output is correct |
38 |
Correct |
39 ms |
6236 KB |
Output is correct |
39 |
Correct |
42 ms |
6236 KB |
Output is correct |
40 |
Correct |
43 ms |
6228 KB |
Output is correct |
41 |
Correct |
39 ms |
6380 KB |
Output is correct |
42 |
Correct |
5 ms |
2404 KB |
Output is correct |
43 |
Correct |
4 ms |
2516 KB |
Output is correct |
44 |
Correct |
5 ms |
2648 KB |
Output is correct |
45 |
Correct |
30 ms |
8532 KB |
Output is correct |
46 |
Correct |
26 ms |
8496 KB |
Output is correct |
47 |
Correct |
131 ms |
13196 KB |
Output is correct |
48 |
Correct |
42 ms |
6608 KB |
Output is correct |
49 |
Correct |
31 ms |
5460 KB |
Output is correct |
50 |
Correct |
29 ms |
4956 KB |
Output is correct |
51 |
Correct |
74 ms |
8912 KB |
Output is correct |
52 |
Correct |
83 ms |
9360 KB |
Output is correct |
53 |
Correct |
30 ms |
6616 KB |
Output is correct |
54 |
Correct |
2 ms |
2396 KB |
Output is correct |
55 |
Correct |
3 ms |
2776 KB |
Output is correct |
56 |
Correct |
21 ms |
6088 KB |
Output is correct |
57 |
Correct |
61 ms |
11152 KB |
Output is correct |
58 |
Correct |
7 ms |
3284 KB |
Output is correct |
59 |
Correct |
8 ms |
3800 KB |
Output is correct |
60 |
Correct |
11 ms |
4412 KB |
Output is correct |
61 |
Correct |
11 ms |
3804 KB |
Output is correct |
62 |
Correct |
76 ms |
10460 KB |
Output is correct |
63 |
Correct |
76 ms |
25800 KB |
Output is correct |
64 |
Correct |
84 ms |
29068 KB |
Output is correct |
65 |
Correct |
103 ms |
38676 KB |
Output is correct |
66 |
Correct |
189 ms |
65304 KB |
Output is correct |
67 |
Correct |
184 ms |
64996 KB |
Output is correct |