// 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<pii> 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 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 |
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 |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1888 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
8 ms |
1884 KB |
Output is correct |
19 |
Correct |
10 ms |
1884 KB |
Output is correct |
20 |
Correct |
10 ms |
1884 KB |
Output is correct |
21 |
Correct |
2 ms |
1884 KB |
Output is correct |
22 |
Correct |
8 ms |
1884 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
10 ms |
2136 KB |
Output is correct |
25 |
Correct |
10 ms |
2140 KB |
Output is correct |
26 |
Correct |
10 ms |
2140 KB |
Output is correct |
27 |
Correct |
10 ms |
2140 KB |
Output is correct |
28 |
Correct |
10 ms |
2140 KB |
Output is correct |
29 |
Correct |
14 ms |
2544 KB |
Output is correct |
30 |
Correct |
11 ms |
2140 KB |
Output is correct |
31 |
Correct |
11 ms |
2240 KB |
Output is correct |
32 |
Correct |
12 ms |
2140 KB |
Output is correct |
33 |
Correct |
16 ms |
3160 KB |
Output is correct |
34 |
Correct |
15 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 |
2 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 |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1884 KB |
Output is correct |
12 |
Correct |
2 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
8 ms |
1884 KB |
Output is correct |
19 |
Correct |
10 ms |
1940 KB |
Output is correct |
20 |
Correct |
10 ms |
1896 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
8 ms |
1956 KB |
Output is correct |
23 |
Correct |
7 ms |
1880 KB |
Output is correct |
24 |
Correct |
10 ms |
2140 KB |
Output is correct |
25 |
Correct |
10 ms |
2016 KB |
Output is correct |
26 |
Correct |
10 ms |
2140 KB |
Output is correct |
27 |
Correct |
12 ms |
1992 KB |
Output is correct |
28 |
Correct |
10 ms |
2140 KB |
Output is correct |
29 |
Correct |
14 ms |
2652 KB |
Output is correct |
30 |
Correct |
11 ms |
2112 KB |
Output is correct |
31 |
Correct |
11 ms |
2384 KB |
Output is correct |
32 |
Correct |
11 ms |
2140 KB |
Output is correct |
33 |
Correct |
15 ms |
3160 KB |
Output is correct |
34 |
Correct |
16 ms |
3164 KB |
Output is correct |
35 |
Correct |
11 ms |
3164 KB |
Output is correct |
36 |
Correct |
5 ms |
2188 KB |
Output is correct |
37 |
Correct |
18 ms |
4188 KB |
Output is correct |
38 |
Correct |
17 ms |
4184 KB |
Output is correct |
39 |
Correct |
17 ms |
4188 KB |
Output is correct |
40 |
Correct |
17 ms |
4012 KB |
Output is correct |
41 |
Correct |
17 ms |
3984 KB |
Output is correct |
42 |
Correct |
13 ms |
2396 KB |
Output is correct |
43 |
Correct |
13 ms |
2396 KB |
Output is correct |
44 |
Correct |
12 ms |
2268 KB |
Output is correct |
45 |
Correct |
22 ms |
6488 KB |
Output is correct |
46 |
Correct |
22 ms |
6688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
2136 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 |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1780 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
2 ms |
1884 KB |
Output is correct |
17 |
Correct |
3 ms |
2140 KB |
Output is correct |
18 |
Correct |
8 ms |
1880 KB |
Output is correct |
19 |
Correct |
10 ms |
1884 KB |
Output is correct |
20 |
Correct |
10 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
8 ms |
1884 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
10 ms |
2140 KB |
Output is correct |
25 |
Correct |
11 ms |
2140 KB |
Output is correct |
26 |
Correct |
10 ms |
1956 KB |
Output is correct |
27 |
Correct |
10 ms |
1884 KB |
Output is correct |
28 |
Correct |
10 ms |
2136 KB |
Output is correct |
29 |
Correct |
13 ms |
2652 KB |
Output is correct |
30 |
Correct |
11 ms |
2140 KB |
Output is correct |
31 |
Correct |
11 ms |
2236 KB |
Output is correct |
32 |
Correct |
11 ms |
2140 KB |
Output is correct |
33 |
Correct |
15 ms |
3164 KB |
Output is correct |
34 |
Correct |
16 ms |
3124 KB |
Output is correct |
35 |
Correct |
11 ms |
3164 KB |
Output is correct |
36 |
Correct |
6 ms |
2140 KB |
Output is correct |
37 |
Correct |
18 ms |
4188 KB |
Output is correct |
38 |
Correct |
18 ms |
4432 KB |
Output is correct |
39 |
Correct |
17 ms |
4188 KB |
Output is correct |
40 |
Correct |
17 ms |
4032 KB |
Output is correct |
41 |
Correct |
17 ms |
4068 KB |
Output is correct |
42 |
Correct |
14 ms |
2396 KB |
Output is correct |
43 |
Correct |
13 ms |
2392 KB |
Output is correct |
44 |
Correct |
12 ms |
2396 KB |
Output is correct |
45 |
Correct |
23 ms |
6616 KB |
Output is correct |
46 |
Correct |
22 ms |
6488 KB |
Output is correct |
47 |
Correct |
359 ms |
11348 KB |
Output is correct |
48 |
Execution timed out |
1094 ms |
4900 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |