#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
const int B = 200;
vector<ll> dijkstra(int n, int s, vector<vector<pair<ll, ll>>> &adj){
vector<ll> dist(n+1, INF);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.first;
ll w = it.second;
if(vis[v]) continue;
dist[v] = min(dist[v], d_u+w);
pq.push({dist[v], v});
}
}
return dist;
}
void solve(){
int n, m;
cin >> n >> m;
vector<ll> b(m+1), p(m+1);
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
b[i]++;
}
int c = 0;
vector<vector<int>> cv(n+1, vector<int>(B+1));
vector<vector<bool>> have_doge(n+1, vector<bool>(B+1, false));
for(int i = 1; i <= m; i++){
if(p[i] <= B){
have_doge[b[i]][p[i]] = true;
}
}
vector<pair<int, int>> dc;
dc.push_back({-1, -1});
for(int i = 1; i <= n; i++){
for(int j = 0; j <= B; j++){
c++;
cv[i][j] = c;
dc.push_back({i, j});
}
}
int nb = c;
auto valid = [&](int x) -> bool{
return (x >= 1 && x <= n);
};
vector<vector<pair<ll, ll>>> adj(nb+1);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= B; j++){
int u, v;
u = cv[i][0];
v = cv[i][j];
if(have_doge[i][j]){
adj[u].push_back({v, 0});
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= B; j++){
int u, v;
if(valid(i+j)){
u = cv[i][j];
v = cv[i+j][0];
adj[u].push_back({v, 1});
v = cv[i+j][j];
adj[u].push_back({v, 1});
}
if(valid(i-j)){
u = cv[i][j];
v = cv[i-j][0];
adj[u].push_back({v, 1});
v = cv[i-j][j];
adj[u].push_back({v, 1});
}
}
}
for(int i = 1; i <= m; i++){
int pos, u, v;
pos = b[i];
if(p[i] <= B) continue;
for(int j = -B; j <= B; j++){
if(valid(pos+j*p[i]) && j != 0){
u = cv[pos][0];
v = cv[pos+j*p[i]][0];
adj[u].push_back({v, abs(j)});
}
}
}
for(int j = 1; j <= n; j++){
if(abs(b[1]-j)%p[1] == 0){
ll w = abs(b[1]-j)/p[1];
adj[0].push_back({cv[j][0], w});
}
}
int S = 0, T = cv[b[2]][0];
vector<ll> dist = dijkstra(nb, S, adj);
ll ans = dist[T];
if(ans == INF){
ans = -1;
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
684 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1768 KB |
Output is correct |
11 |
Correct |
2 ms |
1752 KB |
Output is correct |
12 |
Correct |
2 ms |
1756 KB |
Output is correct |
13 |
Correct |
2 ms |
1756 KB |
Output is correct |
14 |
Correct |
3 ms |
1756 KB |
Output is correct |
15 |
Correct |
2 ms |
1756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
684 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1752 KB |
Output is correct |
11 |
Correct |
2 ms |
1756 KB |
Output is correct |
12 |
Correct |
2 ms |
1752 KB |
Output is correct |
13 |
Correct |
2 ms |
1616 KB |
Output is correct |
14 |
Correct |
2 ms |
1756 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
5 ms |
3796 KB |
Output is correct |
17 |
Correct |
21 ms |
17472 KB |
Output is correct |
18 |
Correct |
38 ms |
41864 KB |
Output is correct |
19 |
Correct |
49 ms |
48056 KB |
Output is correct |
20 |
Correct |
43 ms |
48328 KB |
Output is correct |
21 |
Correct |
10 ms |
8664 KB |
Output is correct |
22 |
Correct |
41 ms |
43216 KB |
Output is correct |
23 |
Correct |
36 ms |
39616 KB |
Output is correct |
24 |
Correct |
47 ms |
45756 KB |
Output is correct |
25 |
Correct |
48 ms |
48568 KB |
Output is correct |
26 |
Correct |
47 ms |
48056 KB |
Output is correct |
27 |
Correct |
46 ms |
48052 KB |
Output is correct |
28 |
Correct |
53 ms |
48060 KB |
Output is correct |
29 |
Correct |
53 ms |
48056 KB |
Output is correct |
30 |
Correct |
48 ms |
48056 KB |
Output is correct |
31 |
Correct |
52 ms |
48236 KB |
Output is correct |
32 |
Correct |
47 ms |
49084 KB |
Output is correct |
33 |
Correct |
59 ms |
48308 KB |
Output is correct |
34 |
Correct |
62 ms |
48308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1752 KB |
Output is correct |
11 |
Correct |
2 ms |
2012 KB |
Output is correct |
12 |
Correct |
1 ms |
1752 KB |
Output is correct |
13 |
Correct |
1 ms |
1756 KB |
Output is correct |
14 |
Correct |
2 ms |
1756 KB |
Output is correct |
15 |
Correct |
2 ms |
1756 KB |
Output is correct |
16 |
Correct |
4 ms |
3796 KB |
Output is correct |
17 |
Correct |
18 ms |
17356 KB |
Output is correct |
18 |
Correct |
38 ms |
42692 KB |
Output is correct |
19 |
Correct |
46 ms |
48112 KB |
Output is correct |
20 |
Correct |
45 ms |
48572 KB |
Output is correct |
21 |
Correct |
8 ms |
8660 KB |
Output is correct |
22 |
Correct |
39 ms |
43972 KB |
Output is correct |
23 |
Correct |
39 ms |
38896 KB |
Output is correct |
24 |
Correct |
48 ms |
45752 KB |
Output is correct |
25 |
Correct |
48 ms |
48060 KB |
Output is correct |
26 |
Correct |
43 ms |
48564 KB |
Output is correct |
27 |
Correct |
42 ms |
48824 KB |
Output is correct |
28 |
Correct |
49 ms |
48820 KB |
Output is correct |
29 |
Correct |
51 ms |
48312 KB |
Output is correct |
30 |
Correct |
55 ms |
48568 KB |
Output is correct |
31 |
Correct |
51 ms |
48056 KB |
Output is correct |
32 |
Correct |
52 ms |
48052 KB |
Output is correct |
33 |
Correct |
64 ms |
48276 KB |
Output is correct |
34 |
Correct |
58 ms |
48564 KB |
Output is correct |
35 |
Correct |
48 ms |
37312 KB |
Output is correct |
36 |
Correct |
27 ms |
26064 KB |
Output is correct |
37 |
Correct |
85 ms |
49996 KB |
Output is correct |
38 |
Correct |
76 ms |
50676 KB |
Output is correct |
39 |
Correct |
85 ms |
50616 KB |
Output is correct |
40 |
Correct |
86 ms |
51232 KB |
Output is correct |
41 |
Correct |
84 ms |
51280 KB |
Output is correct |
42 |
Correct |
53 ms |
48828 KB |
Output is correct |
43 |
Correct |
55 ms |
48828 KB |
Output is correct |
44 |
Correct |
52 ms |
48828 KB |
Output is correct |
45 |
Correct |
132 ms |
55064 KB |
Output is correct |
46 |
Correct |
122 ms |
55224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1756 KB |
Output is correct |
12 |
Correct |
1 ms |
1756 KB |
Output is correct |
13 |
Correct |
1 ms |
1752 KB |
Output is correct |
14 |
Correct |
2 ms |
1832 KB |
Output is correct |
15 |
Correct |
3 ms |
1752 KB |
Output is correct |
16 |
Correct |
4 ms |
3796 KB |
Output is correct |
17 |
Correct |
21 ms |
17356 KB |
Output is correct |
18 |
Correct |
43 ms |
41936 KB |
Output is correct |
19 |
Correct |
48 ms |
48620 KB |
Output is correct |
20 |
Correct |
56 ms |
48740 KB |
Output is correct |
21 |
Correct |
10 ms |
8660 KB |
Output is correct |
22 |
Correct |
46 ms |
43704 KB |
Output is correct |
23 |
Correct |
40 ms |
39616 KB |
Output is correct |
24 |
Correct |
43 ms |
46004 KB |
Output is correct |
25 |
Correct |
65 ms |
48188 KB |
Output is correct |
26 |
Correct |
56 ms |
48052 KB |
Output is correct |
27 |
Correct |
51 ms |
48000 KB |
Output is correct |
28 |
Correct |
55 ms |
48052 KB |
Output is correct |
29 |
Correct |
59 ms |
48064 KB |
Output is correct |
30 |
Correct |
52 ms |
48820 KB |
Output is correct |
31 |
Correct |
56 ms |
48268 KB |
Output is correct |
32 |
Correct |
52 ms |
48308 KB |
Output is correct |
33 |
Correct |
67 ms |
48312 KB |
Output is correct |
34 |
Correct |
72 ms |
48312 KB |
Output is correct |
35 |
Correct |
56 ms |
37052 KB |
Output is correct |
36 |
Correct |
30 ms |
26044 KB |
Output is correct |
37 |
Correct |
89 ms |
49632 KB |
Output is correct |
38 |
Correct |
89 ms |
50580 KB |
Output is correct |
39 |
Correct |
83 ms |
50620 KB |
Output is correct |
40 |
Correct |
85 ms |
51068 KB |
Output is correct |
41 |
Correct |
84 ms |
51188 KB |
Output is correct |
42 |
Correct |
60 ms |
48744 KB |
Output is correct |
43 |
Correct |
53 ms |
48820 KB |
Output is correct |
44 |
Correct |
54 ms |
48708 KB |
Output is correct |
45 |
Correct |
124 ms |
54888 KB |
Output is correct |
46 |
Correct |
128 ms |
54972 KB |
Output is correct |
47 |
Runtime error |
249 ms |
262144 KB |
Execution killed with signal 9 |
48 |
Halted |
0 ms |
0 KB |
- |