// 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;
const int inf = 1e9 + 7;
int n, m, s, t, d[maxn];
vector<int> vec[maxn];
vector<pii> adj[maxn];
bool mark[maxn];
set<pll> st;
void dij(){
memset(d, 63, sizeof d);
d[s] = 0;
st.insert(mp(0, s));
while(st.size()){
int v = (*st.begin()).S, dist = (*st.begin()).F;
st.erase(st.begin());
for(auto [u, w]: adj[v]){
if(d[u] > dist + w){
st.erase(mp(d[u], u));
d[u] = dist + w;
st.insert(mp(d[u], 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 |
1 ms |
1880 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 |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1880 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 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 |
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 |
2 ms |
2036 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 |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
2140 KB |
Output is correct |
14 |
Correct |
1 ms |
2136 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 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 |
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 |
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 |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
2 ms |
2136 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2140 KB |
Output is correct |
20 |
Correct |
6 ms |
2140 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2340 KB |
Output is correct |
24 |
Correct |
4 ms |
2140 KB |
Output is correct |
25 |
Correct |
8 ms |
2140 KB |
Output is correct |
26 |
Correct |
4 ms |
2140 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
4 ms |
2168 KB |
Output is correct |
29 |
Correct |
7 ms |
2652 KB |
Output is correct |
30 |
Correct |
4 ms |
2368 KB |
Output is correct |
31 |
Correct |
5 ms |
2396 KB |
Output is correct |
32 |
Correct |
4 ms |
2396 KB |
Output is correct |
33 |
Correct |
9 ms |
3164 KB |
Output is correct |
34 |
Correct |
9 ms |
3164 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 |
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 |
1880 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 |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
1 ms |
2140 KB |
Output is correct |
14 |
Correct |
1 ms |
2136 KB |
Output is correct |
15 |
Correct |
1 ms |
2136 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2392 KB |
Output is correct |
20 |
Correct |
3 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
3 ms |
2136 KB |
Output is correct |
23 |
Correct |
3 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2140 KB |
Output is correct |
25 |
Correct |
5 ms |
2140 KB |
Output is correct |
26 |
Correct |
4 ms |
2140 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2848 KB |
Output is correct |
30 |
Correct |
4 ms |
2396 KB |
Output is correct |
31 |
Correct |
5 ms |
2592 KB |
Output is correct |
32 |
Correct |
5 ms |
2396 KB |
Output is correct |
33 |
Correct |
8 ms |
3416 KB |
Output is correct |
34 |
Correct |
9 ms |
3316 KB |
Output is correct |
35 |
Correct |
8 ms |
3164 KB |
Output is correct |
36 |
Correct |
3 ms |
2140 KB |
Output is correct |
37 |
Correct |
10 ms |
4188 KB |
Output is correct |
38 |
Correct |
11 ms |
4188 KB |
Output is correct |
39 |
Correct |
11 ms |
4188 KB |
Output is correct |
40 |
Correct |
10 ms |
4188 KB |
Output is correct |
41 |
Correct |
11 ms |
4188 KB |
Output is correct |
42 |
Correct |
6 ms |
2516 KB |
Output is correct |
43 |
Correct |
6 ms |
2268 KB |
Output is correct |
44 |
Correct |
6 ms |
2272 KB |
Output is correct |
45 |
Correct |
15 ms |
6748 KB |
Output is correct |
46 |
Correct |
15 ms |
6748 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 |
1884 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 |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
2140 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1960 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2124 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
3 ms |
2140 KB |
Output is correct |
19 |
Correct |
3 ms |
2180 KB |
Output is correct |
20 |
Correct |
4 ms |
2140 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
3 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2140 KB |
Output is correct |
24 |
Correct |
4 ms |
2140 KB |
Output is correct |
25 |
Correct |
4 ms |
2140 KB |
Output is correct |
26 |
Correct |
4 ms |
2392 KB |
Output is correct |
27 |
Correct |
3 ms |
2140 KB |
Output is correct |
28 |
Correct |
8 ms |
2140 KB |
Output is correct |
29 |
Correct |
6 ms |
2648 KB |
Output is correct |
30 |
Correct |
4 ms |
2392 KB |
Output is correct |
31 |
Correct |
5 ms |
2396 KB |
Output is correct |
32 |
Correct |
4 ms |
2396 KB |
Output is correct |
33 |
Correct |
8 ms |
3164 KB |
Output is correct |
34 |
Correct |
8 ms |
3164 KB |
Output is correct |
35 |
Correct |
8 ms |
3164 KB |
Output is correct |
36 |
Correct |
3 ms |
2140 KB |
Output is correct |
37 |
Correct |
9 ms |
4184 KB |
Output is correct |
38 |
Correct |
11 ms |
4184 KB |
Output is correct |
39 |
Correct |
11 ms |
4532 KB |
Output is correct |
40 |
Correct |
11 ms |
4188 KB |
Output is correct |
41 |
Correct |
10 ms |
4188 KB |
Output is correct |
42 |
Correct |
6 ms |
2520 KB |
Output is correct |
43 |
Correct |
6 ms |
2276 KB |
Output is correct |
44 |
Correct |
6 ms |
2272 KB |
Output is correct |
45 |
Correct |
15 ms |
6796 KB |
Output is correct |
46 |
Correct |
15 ms |
6744 KB |
Output is correct |
47 |
Correct |
206 ms |
11600 KB |
Output is correct |
48 |
Correct |
251 ms |
4816 KB |
Output is correct |
49 |
Correct |
315 ms |
3916 KB |
Output is correct |
50 |
Correct |
838 ms |
3900 KB |
Output is correct |
51 |
Execution timed out |
1075 ms |
6048 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |