#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 3e5 + 10;
vector<pair<int,int>> adj[N];
vector<int> dog[N];
bool vis[N];
void solve() {
int n, m; cin >> n >> m;
vector<int> b(m), p(m);
int start = -1, endd = -1;
for(int i = 0;i < m; ++i) {
cin >> b[i] >> p[i];
dog[b[i]].pb(i);
if(i == 0) start = b[i];
if(i == 1) endd = b[i];
}
for(int i = 0;i < n; ++i) {
vector<bool> viss((int)3e4 + 5, false);
for(int f: dog[i]) {
int c = p[f];
if(viss[c]) continue;
viss[c] = true;
int pp = i;
int jumps = 0;
while(pp + c < n) {
++jumps;
adj[i].pb({pp + c, jumps});
pp += c;
}
pp = i, jumps = 0;
while(pp - c >= 0) {
++jumps;
adj[i].pb({pp - c, jumps});
pp -= c;
}
}
}
vector<int> dist(N, INT_MAX);
priority_queue<pair<int,int>> q;
q.push({0, start});
dist[start] = 0;
while(!q.empty()) {
int u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = true;
for(auto x: adj[u]) {
int w = x.second, v = x.first;
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
q.push({-dist[v], v});
}
}
}
if(dist[endd] == INT_MAX) cout << "-1\n";
else
cout << dist[endd] << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15572 KB |
Output is correct |
2 |
Correct |
10 ms |
15560 KB |
Output is correct |
3 |
Correct |
8 ms |
15492 KB |
Output is correct |
4 |
Correct |
9 ms |
15572 KB |
Output is correct |
5 |
Correct |
8 ms |
15572 KB |
Output is correct |
6 |
Correct |
9 ms |
15572 KB |
Output is correct |
7 |
Correct |
8 ms |
15572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15572 KB |
Output is correct |
2 |
Correct |
8 ms |
15572 KB |
Output is correct |
3 |
Correct |
9 ms |
15548 KB |
Output is correct |
4 |
Correct |
9 ms |
15572 KB |
Output is correct |
5 |
Correct |
10 ms |
15528 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
10 ms |
15572 KB |
Output is correct |
8 |
Correct |
10 ms |
15572 KB |
Output is correct |
9 |
Correct |
9 ms |
15572 KB |
Output is correct |
10 |
Correct |
8 ms |
15572 KB |
Output is correct |
11 |
Correct |
9 ms |
15700 KB |
Output is correct |
12 |
Correct |
10 ms |
15624 KB |
Output is correct |
13 |
Correct |
9 ms |
15696 KB |
Output is correct |
14 |
Correct |
9 ms |
15572 KB |
Output is correct |
15 |
Correct |
9 ms |
15572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15572 KB |
Output is correct |
2 |
Correct |
8 ms |
15516 KB |
Output is correct |
3 |
Correct |
9 ms |
15572 KB |
Output is correct |
4 |
Correct |
9 ms |
15572 KB |
Output is correct |
5 |
Correct |
8 ms |
15572 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
8 ms |
15572 KB |
Output is correct |
8 |
Correct |
9 ms |
15644 KB |
Output is correct |
9 |
Correct |
11 ms |
15520 KB |
Output is correct |
10 |
Correct |
12 ms |
15552 KB |
Output is correct |
11 |
Correct |
9 ms |
15644 KB |
Output is correct |
12 |
Correct |
11 ms |
15560 KB |
Output is correct |
13 |
Correct |
9 ms |
15700 KB |
Output is correct |
14 |
Correct |
9 ms |
15572 KB |
Output is correct |
15 |
Correct |
11 ms |
15572 KB |
Output is correct |
16 |
Correct |
8 ms |
15572 KB |
Output is correct |
17 |
Correct |
10 ms |
15828 KB |
Output is correct |
18 |
Correct |
9 ms |
15700 KB |
Output is correct |
19 |
Correct |
9 ms |
15616 KB |
Output is correct |
20 |
Correct |
63 ms |
47728 KB |
Output is correct |
21 |
Correct |
9 ms |
15572 KB |
Output is correct |
22 |
Correct |
9 ms |
15572 KB |
Output is correct |
23 |
Correct |
10 ms |
15700 KB |
Output is correct |
24 |
Correct |
11 ms |
15700 KB |
Output is correct |
25 |
Correct |
10 ms |
15736 KB |
Output is correct |
26 |
Correct |
10 ms |
15700 KB |
Output is correct |
27 |
Correct |
10 ms |
15700 KB |
Output is correct |
28 |
Correct |
10 ms |
15892 KB |
Output is correct |
29 |
Correct |
11 ms |
16212 KB |
Output is correct |
30 |
Correct |
10 ms |
15828 KB |
Output is correct |
31 |
Correct |
10 ms |
15956 KB |
Output is correct |
32 |
Correct |
10 ms |
15840 KB |
Output is correct |
33 |
Correct |
13 ms |
16780 KB |
Output is correct |
34 |
Correct |
12 ms |
16832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15572 KB |
Output is correct |
2 |
Correct |
9 ms |
15572 KB |
Output is correct |
3 |
Correct |
10 ms |
15572 KB |
Output is correct |
4 |
Correct |
11 ms |
15572 KB |
Output is correct |
5 |
Correct |
8 ms |
15572 KB |
Output is correct |
6 |
Correct |
8 ms |
15572 KB |
Output is correct |
7 |
Correct |
8 ms |
15540 KB |
Output is correct |
8 |
Correct |
9 ms |
15536 KB |
Output is correct |
9 |
Correct |
9 ms |
15572 KB |
Output is correct |
10 |
Correct |
8 ms |
15572 KB |
Output is correct |
11 |
Correct |
10 ms |
15700 KB |
Output is correct |
12 |
Correct |
9 ms |
15572 KB |
Output is correct |
13 |
Correct |
9 ms |
15700 KB |
Output is correct |
14 |
Correct |
9 ms |
15692 KB |
Output is correct |
15 |
Correct |
10 ms |
15572 KB |
Output is correct |
16 |
Correct |
10 ms |
15668 KB |
Output is correct |
17 |
Correct |
10 ms |
15824 KB |
Output is correct |
18 |
Correct |
12 ms |
15680 KB |
Output is correct |
19 |
Correct |
11 ms |
15572 KB |
Output is correct |
20 |
Correct |
68 ms |
47824 KB |
Output is correct |
21 |
Correct |
8 ms |
15572 KB |
Output is correct |
22 |
Correct |
9 ms |
15572 KB |
Output is correct |
23 |
Correct |
9 ms |
15640 KB |
Output is correct |
24 |
Correct |
9 ms |
15828 KB |
Output is correct |
25 |
Correct |
10 ms |
15732 KB |
Output is correct |
26 |
Correct |
10 ms |
15700 KB |
Output is correct |
27 |
Correct |
9 ms |
15700 KB |
Output is correct |
28 |
Correct |
11 ms |
15828 KB |
Output is correct |
29 |
Correct |
12 ms |
16288 KB |
Output is correct |
30 |
Correct |
9 ms |
15832 KB |
Output is correct |
31 |
Correct |
10 ms |
15996 KB |
Output is correct |
32 |
Correct |
10 ms |
15828 KB |
Output is correct |
33 |
Correct |
12 ms |
16796 KB |
Output is correct |
34 |
Correct |
13 ms |
16852 KB |
Output is correct |
35 |
Correct |
17 ms |
17460 KB |
Output is correct |
36 |
Correct |
11 ms |
15828 KB |
Output is correct |
37 |
Correct |
20 ms |
18992 KB |
Output is correct |
38 |
Correct |
23 ms |
18512 KB |
Output is correct |
39 |
Correct |
21 ms |
18644 KB |
Output is correct |
40 |
Correct |
19 ms |
18644 KB |
Output is correct |
41 |
Correct |
23 ms |
18488 KB |
Output is correct |
42 |
Correct |
13 ms |
16084 KB |
Output is correct |
43 |
Correct |
13 ms |
15956 KB |
Output is correct |
44 |
Correct |
68 ms |
48080 KB |
Output is correct |
45 |
Correct |
21 ms |
20340 KB |
Output is correct |
46 |
Correct |
20 ms |
20252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15572 KB |
Output is correct |
2 |
Correct |
8 ms |
15572 KB |
Output is correct |
3 |
Correct |
9 ms |
15572 KB |
Output is correct |
4 |
Correct |
10 ms |
15572 KB |
Output is correct |
5 |
Correct |
8 ms |
15572 KB |
Output is correct |
6 |
Correct |
9 ms |
15572 KB |
Output is correct |
7 |
Correct |
8 ms |
15572 KB |
Output is correct |
8 |
Correct |
11 ms |
15572 KB |
Output is correct |
9 |
Correct |
12 ms |
15524 KB |
Output is correct |
10 |
Correct |
9 ms |
15572 KB |
Output is correct |
11 |
Correct |
12 ms |
15700 KB |
Output is correct |
12 |
Correct |
9 ms |
15572 KB |
Output is correct |
13 |
Correct |
8 ms |
15700 KB |
Output is correct |
14 |
Correct |
9 ms |
15560 KB |
Output is correct |
15 |
Correct |
8 ms |
15572 KB |
Output is correct |
16 |
Correct |
8 ms |
15572 KB |
Output is correct |
17 |
Correct |
10 ms |
15828 KB |
Output is correct |
18 |
Correct |
9 ms |
15608 KB |
Output is correct |
19 |
Correct |
10 ms |
15672 KB |
Output is correct |
20 |
Correct |
66 ms |
47692 KB |
Output is correct |
21 |
Correct |
9 ms |
15572 KB |
Output is correct |
22 |
Correct |
9 ms |
15572 KB |
Output is correct |
23 |
Correct |
10 ms |
15700 KB |
Output is correct |
24 |
Correct |
14 ms |
15828 KB |
Output is correct |
25 |
Correct |
13 ms |
15772 KB |
Output is correct |
26 |
Correct |
11 ms |
15700 KB |
Output is correct |
27 |
Correct |
9 ms |
15700 KB |
Output is correct |
28 |
Correct |
12 ms |
15828 KB |
Output is correct |
29 |
Correct |
10 ms |
16304 KB |
Output is correct |
30 |
Correct |
10 ms |
15792 KB |
Output is correct |
31 |
Correct |
11 ms |
15956 KB |
Output is correct |
32 |
Correct |
10 ms |
15816 KB |
Output is correct |
33 |
Correct |
13 ms |
16852 KB |
Output is correct |
34 |
Correct |
13 ms |
16880 KB |
Output is correct |
35 |
Correct |
24 ms |
17456 KB |
Output is correct |
36 |
Correct |
11 ms |
15828 KB |
Output is correct |
37 |
Correct |
19 ms |
19028 KB |
Output is correct |
38 |
Correct |
21 ms |
18516 KB |
Output is correct |
39 |
Correct |
22 ms |
18704 KB |
Output is correct |
40 |
Correct |
19 ms |
18588 KB |
Output is correct |
41 |
Correct |
19 ms |
18412 KB |
Output is correct |
42 |
Correct |
12 ms |
16016 KB |
Output is correct |
43 |
Correct |
13 ms |
15956 KB |
Output is correct |
44 |
Correct |
72 ms |
48004 KB |
Output is correct |
45 |
Correct |
21 ms |
20308 KB |
Output is correct |
46 |
Correct |
22 ms |
20324 KB |
Output is correct |
47 |
Correct |
61 ms |
27200 KB |
Output is correct |
48 |
Correct |
25 ms |
18512 KB |
Output is correct |
49 |
Correct |
23 ms |
17604 KB |
Output is correct |
50 |
Correct |
22 ms |
17520 KB |
Output is correct |
51 |
Correct |
45 ms |
21304 KB |
Output is correct |
52 |
Correct |
46 ms |
21352 KB |
Output is correct |
53 |
Correct |
29 ms |
17956 KB |
Output is correct |
54 |
Correct |
18 ms |
16084 KB |
Output is correct |
55 |
Correct |
30 ms |
16552 KB |
Output is correct |
56 |
Runtime error |
438 ms |
262144 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |