// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
const int maxn = 3e4 + 5, maxd = 1e6 + 5;
const int inf = 1e9 + 7;
int n, m, s, t, d[maxn];
vector<int> vec[maxn], asli[maxd];
vector<pii> adj[maxn];
bool mark[maxn];
void dij(){
memset(d, 63, sizeof d);
d[s] = 0;
asli[0].pb(s);
for(int i = 0; i < maxd; i++){
for(auto v: asli[i]){
for(auto [u, w]: adj[v]){
if(d[u] > d[v] + w){
d[u] = d[v] + w;
asli[d[u]].pb(u);
}
}
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
int b, p; cin >> b >> p;
if(p < n) vec[p].pb(b);
if(i == 0) s = b;
if(i == 1) t = b;
}
for(int p = 1; p < n; p++){
for(auto u: vec[p]) mark[u] = 1;
for(int i = 0; i < n; i++){
if(!mark[i]) continue;
for(int j = i - p; j >= 0; j -= p){
adj[i].pb(mp(j, (i - j) / p));
if(mark[j]) break;
}
for(int j = i + p; j < n; j += p){
adj[i].pb(mp(j, (j - i) / p));
if(mark[j]) break;
}
}
for(auto u: vec[p]) mark[u] = 0;
}
dij();
if(d[t] >= inf) cout << -1 << '\n';
else cout << d[t] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25416 KB |
Output is correct |
3 |
Correct |
8 ms |
25432 KB |
Output is correct |
4 |
Correct |
8 ms |
25436 KB |
Output is correct |
5 |
Correct |
9 ms |
25436 KB |
Output is correct |
6 |
Correct |
8 ms |
25592 KB |
Output is correct |
7 |
Correct |
8 ms |
25396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25436 KB |
Output is correct |
3 |
Correct |
8 ms |
25584 KB |
Output is correct |
4 |
Correct |
8 ms |
25436 KB |
Output is correct |
5 |
Correct |
8 ms |
25436 KB |
Output is correct |
6 |
Correct |
8 ms |
25436 KB |
Output is correct |
7 |
Correct |
8 ms |
25592 KB |
Output is correct |
8 |
Correct |
8 ms |
25592 KB |
Output is correct |
9 |
Correct |
8 ms |
25436 KB |
Output is correct |
10 |
Correct |
9 ms |
25436 KB |
Output is correct |
11 |
Correct |
8 ms |
25436 KB |
Output is correct |
12 |
Correct |
8 ms |
25396 KB |
Output is correct |
13 |
Correct |
8 ms |
25580 KB |
Output is correct |
14 |
Correct |
10 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25436 KB |
Output is correct |
3 |
Correct |
8 ms |
25424 KB |
Output is correct |
4 |
Correct |
9 ms |
25688 KB |
Output is correct |
5 |
Correct |
8 ms |
25588 KB |
Output is correct |
6 |
Correct |
8 ms |
25436 KB |
Output is correct |
7 |
Correct |
9 ms |
25688 KB |
Output is correct |
8 |
Correct |
9 ms |
25596 KB |
Output is correct |
9 |
Correct |
9 ms |
25588 KB |
Output is correct |
10 |
Correct |
9 ms |
25436 KB |
Output is correct |
11 |
Correct |
8 ms |
25448 KB |
Output is correct |
12 |
Correct |
9 ms |
25632 KB |
Output is correct |
13 |
Correct |
9 ms |
25436 KB |
Output is correct |
14 |
Correct |
8 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25436 KB |
Output is correct |
16 |
Correct |
8 ms |
25436 KB |
Output is correct |
17 |
Correct |
9 ms |
25692 KB |
Output is correct |
18 |
Correct |
10 ms |
25436 KB |
Output is correct |
19 |
Correct |
10 ms |
25432 KB |
Output is correct |
20 |
Correct |
10 ms |
25692 KB |
Output is correct |
21 |
Correct |
8 ms |
25432 KB |
Output is correct |
22 |
Correct |
9 ms |
25436 KB |
Output is correct |
23 |
Correct |
10 ms |
25692 KB |
Output is correct |
24 |
Correct |
11 ms |
25804 KB |
Output is correct |
25 |
Correct |
16 ms |
25688 KB |
Output is correct |
26 |
Correct |
11 ms |
25692 KB |
Output is correct |
27 |
Correct |
10 ms |
25704 KB |
Output is correct |
28 |
Correct |
11 ms |
25692 KB |
Output is correct |
29 |
Correct |
15 ms |
26200 KB |
Output is correct |
30 |
Correct |
10 ms |
25688 KB |
Output is correct |
31 |
Correct |
10 ms |
26200 KB |
Output is correct |
32 |
Correct |
12 ms |
25936 KB |
Output is correct |
33 |
Correct |
12 ms |
26980 KB |
Output is correct |
34 |
Correct |
18 ms |
27224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
25432 KB |
Output is correct |
2 |
Correct |
8 ms |
25432 KB |
Output is correct |
3 |
Correct |
8 ms |
25436 KB |
Output is correct |
4 |
Correct |
8 ms |
25428 KB |
Output is correct |
5 |
Correct |
9 ms |
25436 KB |
Output is correct |
6 |
Correct |
7 ms |
25432 KB |
Output is correct |
7 |
Correct |
7 ms |
25436 KB |
Output is correct |
8 |
Correct |
8 ms |
25592 KB |
Output is correct |
9 |
Correct |
8 ms |
25584 KB |
Output is correct |
10 |
Correct |
9 ms |
25436 KB |
Output is correct |
11 |
Correct |
8 ms |
25436 KB |
Output is correct |
12 |
Correct |
8 ms |
25616 KB |
Output is correct |
13 |
Correct |
10 ms |
25432 KB |
Output is correct |
14 |
Correct |
8 ms |
25436 KB |
Output is correct |
15 |
Correct |
9 ms |
25676 KB |
Output is correct |
16 |
Correct |
9 ms |
25432 KB |
Output is correct |
17 |
Correct |
8 ms |
25688 KB |
Output is correct |
18 |
Correct |
9 ms |
25436 KB |
Output is correct |
19 |
Correct |
10 ms |
25660 KB |
Output is correct |
20 |
Correct |
10 ms |
25660 KB |
Output is correct |
21 |
Correct |
8 ms |
25608 KB |
Output is correct |
22 |
Correct |
9 ms |
25436 KB |
Output is correct |
23 |
Correct |
10 ms |
25688 KB |
Output is correct |
24 |
Correct |
12 ms |
25688 KB |
Output is correct |
25 |
Correct |
11 ms |
25692 KB |
Output is correct |
26 |
Correct |
10 ms |
25692 KB |
Output is correct |
27 |
Correct |
10 ms |
25688 KB |
Output is correct |
28 |
Correct |
13 ms |
25692 KB |
Output is correct |
29 |
Correct |
11 ms |
26204 KB |
Output is correct |
30 |
Correct |
10 ms |
25916 KB |
Output is correct |
31 |
Correct |
17 ms |
26204 KB |
Output is correct |
32 |
Correct |
10 ms |
25692 KB |
Output is correct |
33 |
Correct |
12 ms |
26948 KB |
Output is correct |
34 |
Correct |
12 ms |
26820 KB |
Output is correct |
35 |
Correct |
14 ms |
26716 KB |
Output is correct |
36 |
Correct |
9 ms |
25784 KB |
Output is correct |
37 |
Correct |
15 ms |
27740 KB |
Output is correct |
38 |
Correct |
17 ms |
27740 KB |
Output is correct |
39 |
Correct |
17 ms |
27736 KB |
Output is correct |
40 |
Correct |
17 ms |
27484 KB |
Output is correct |
41 |
Correct |
19 ms |
27740 KB |
Output is correct |
42 |
Correct |
12 ms |
25820 KB |
Output is correct |
43 |
Correct |
12 ms |
25820 KB |
Output is correct |
44 |
Correct |
12 ms |
25820 KB |
Output is correct |
45 |
Correct |
20 ms |
30296 KB |
Output is correct |
46 |
Correct |
20 ms |
30492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25436 KB |
Output is correct |
3 |
Correct |
9 ms |
25432 KB |
Output is correct |
4 |
Correct |
8 ms |
25436 KB |
Output is correct |
5 |
Correct |
8 ms |
25580 KB |
Output is correct |
6 |
Correct |
8 ms |
25436 KB |
Output is correct |
7 |
Correct |
9 ms |
25436 KB |
Output is correct |
8 |
Correct |
8 ms |
25592 KB |
Output is correct |
9 |
Correct |
8 ms |
25396 KB |
Output is correct |
10 |
Correct |
8 ms |
25436 KB |
Output is correct |
11 |
Correct |
8 ms |
25432 KB |
Output is correct |
12 |
Correct |
8 ms |
25436 KB |
Output is correct |
13 |
Correct |
8 ms |
25432 KB |
Output is correct |
14 |
Correct |
9 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25588 KB |
Output is correct |
16 |
Correct |
8 ms |
25436 KB |
Output is correct |
17 |
Correct |
8 ms |
25692 KB |
Output is correct |
18 |
Correct |
9 ms |
25436 KB |
Output is correct |
19 |
Correct |
10 ms |
25432 KB |
Output is correct |
20 |
Correct |
11 ms |
25692 KB |
Output is correct |
21 |
Correct |
8 ms |
25608 KB |
Output is correct |
22 |
Correct |
9 ms |
25688 KB |
Output is correct |
23 |
Correct |
11 ms |
25692 KB |
Output is correct |
24 |
Correct |
12 ms |
25804 KB |
Output is correct |
25 |
Correct |
13 ms |
25692 KB |
Output is correct |
26 |
Correct |
10 ms |
25692 KB |
Output is correct |
27 |
Correct |
10 ms |
25692 KB |
Output is correct |
28 |
Correct |
11 ms |
25692 KB |
Output is correct |
29 |
Correct |
11 ms |
26200 KB |
Output is correct |
30 |
Correct |
10 ms |
25692 KB |
Output is correct |
31 |
Correct |
10 ms |
25948 KB |
Output is correct |
32 |
Correct |
11 ms |
25692 KB |
Output is correct |
33 |
Correct |
12 ms |
26972 KB |
Output is correct |
34 |
Correct |
13 ms |
26972 KB |
Output is correct |
35 |
Correct |
15 ms |
26716 KB |
Output is correct |
36 |
Correct |
10 ms |
25692 KB |
Output is correct |
37 |
Correct |
18 ms |
27720 KB |
Output is correct |
38 |
Correct |
17 ms |
27728 KB |
Output is correct |
39 |
Correct |
17 ms |
27740 KB |
Output is correct |
40 |
Correct |
20 ms |
27740 KB |
Output is correct |
41 |
Correct |
17 ms |
27720 KB |
Output is correct |
42 |
Correct |
12 ms |
25820 KB |
Output is correct |
43 |
Correct |
13 ms |
25828 KB |
Output is correct |
44 |
Correct |
13 ms |
25824 KB |
Output is correct |
45 |
Correct |
22 ms |
30300 KB |
Output is correct |
46 |
Correct |
20 ms |
30808 KB |
Output is correct |
47 |
Correct |
96 ms |
34644 KB |
Output is correct |
48 |
Correct |
397 ms |
28264 KB |
Output is correct |
49 |
Correct |
307 ms |
27476 KB |
Output is correct |
50 |
Correct |
497 ms |
27212 KB |
Output is correct |
51 |
Correct |
427 ms |
30676 KB |
Output is correct |
52 |
Execution timed out |
1050 ms |
29404 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |