#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
set <int> s[n + 1];
int st = -1, en = -1;
for ( int i = 1; i <= m; i++ ){
int pos, v; cin >> pos >> v;
st = i == 1 ? pos : st;
en = i == 2 ? pos : en;
s[pos].insert(v);
}
vector <pair<int,int>> g[n];
for ( int i = 0; i < n; i++ ){
for ( auto d: s[i] ){
for ( int j = i - d; j >= 0; j -= d ){
g[i].pb({j, (i - j) / d});
if ( s[j].count(d) ) break;
}
for ( int j = i + d; j < n; j += d ){
g[i].pb({j, (j - i) / d});
if ( s[j].count(d) ) break;
}
}
}
const int inf = 1e17 + 1;
vector <int> dis(n, inf);
dis[st] = 0;
priority_queue <pair<int,int>> q;
q.push({0, st});
while ( !q.empty() ){
auto [val, x] = q.top();
q.pop(); val = -val;
if ( val > dis[x] ) continue;
for ( auto [to, w]: g[x] ){
if ( chmin(dis[to], w + val) ){
q.push({-dis[to], to});
}
}
}
int res = dis[en];
cout << (res == inf ? -1 : res);
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
228 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
456 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
5 ms |
2912 KB |
Output is correct |
34 |
Correct |
5 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
3 ms |
1084 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1084 KB |
Output is correct |
33 |
Correct |
5 ms |
2912 KB |
Output is correct |
34 |
Correct |
5 ms |
2912 KB |
Output is correct |
35 |
Correct |
17 ms |
3812 KB |
Output is correct |
36 |
Correct |
3 ms |
980 KB |
Output is correct |
37 |
Correct |
17 ms |
5580 KB |
Output is correct |
38 |
Correct |
19 ms |
5568 KB |
Output is correct |
39 |
Correct |
18 ms |
5704 KB |
Output is correct |
40 |
Correct |
18 ms |
5584 KB |
Output is correct |
41 |
Correct |
22 ms |
5588 KB |
Output is correct |
42 |
Correct |
4 ms |
844 KB |
Output is correct |
43 |
Correct |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
4 ms |
908 KB |
Output is correct |
45 |
Correct |
21 ms |
10216 KB |
Output is correct |
46 |
Correct |
22 ms |
10284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
580 KB |
Output is correct |
24 |
Correct |
2 ms |
852 KB |
Output is correct |
25 |
Correct |
2 ms |
772 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
1876 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1112 KB |
Output is correct |
33 |
Correct |
5 ms |
2916 KB |
Output is correct |
34 |
Correct |
4 ms |
3016 KB |
Output is correct |
35 |
Correct |
13 ms |
3796 KB |
Output is correct |
36 |
Correct |
3 ms |
980 KB |
Output is correct |
37 |
Correct |
17 ms |
5652 KB |
Output is correct |
38 |
Correct |
19 ms |
5588 KB |
Output is correct |
39 |
Correct |
20 ms |
5692 KB |
Output is correct |
40 |
Correct |
18 ms |
5552 KB |
Output is correct |
41 |
Correct |
19 ms |
5588 KB |
Output is correct |
42 |
Correct |
5 ms |
848 KB |
Output is correct |
43 |
Correct |
5 ms |
792 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
21 ms |
10292 KB |
Output is correct |
46 |
Correct |
21 ms |
10236 KB |
Output is correct |
47 |
Correct |
44 ms |
20020 KB |
Output is correct |
48 |
Correct |
15 ms |
7760 KB |
Output is correct |
49 |
Correct |
12 ms |
6168 KB |
Output is correct |
50 |
Correct |
12 ms |
6116 KB |
Output is correct |
51 |
Correct |
39 ms |
11692 KB |
Output is correct |
52 |
Correct |
37 ms |
12748 KB |
Output is correct |
53 |
Correct |
17 ms |
7004 KB |
Output is correct |
54 |
Correct |
4 ms |
3416 KB |
Output is correct |
55 |
Correct |
5 ms |
4432 KB |
Output is correct |
56 |
Correct |
13 ms |
5608 KB |
Output is correct |
57 |
Correct |
36 ms |
19736 KB |
Output is correct |
58 |
Correct |
9 ms |
4812 KB |
Output is correct |
59 |
Correct |
11 ms |
5412 KB |
Output is correct |
60 |
Correct |
12 ms |
6664 KB |
Output is correct |
61 |
Correct |
11 ms |
5452 KB |
Output is correct |
62 |
Correct |
24 ms |
14028 KB |
Output is correct |
63 |
Correct |
68 ms |
45288 KB |
Output is correct |
64 |
Correct |
74 ms |
51908 KB |
Output is correct |
65 |
Correct |
98 ms |
71640 KB |
Output is correct |
66 |
Correct |
170 ms |
125944 KB |
Output is correct |
67 |
Correct |
170 ms |
126108 KB |
Output is correct |