#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define sz size()
const int N = 2e5 + 1;
using namespace std;
int n, m, git[N], b[N], p[N];
vector <pii> v[N];
priority_queue <pii> pq;
vector <int> dis(N,1e9);
void f(){
pq.push({0,b[1]});
dis[b[1]] = 0;
while(!pq.empty()){
int a = pq.top().ss;
pq.pop();
for(pii i : v[a]){
if(dis[a] + i.ss < dis[i.ff]){
dis[i.ff] = dis[a] + i.ss;
pq.push({-dis[i.ff], i.ff});
}
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
git[b[i]] = p[i];
}
for(int i = 1; i <= m; i++){
int nd = b[i], cnt = 0;
while(1) {
nd -= p[i];
if(nd < 0) break;
cnt++;
v[b[i]].pb({nd,cnt});
if(git[nd] == p[i]) break;
}
nd = b[i], cnt = 0;
while(1) {
nd += p[i];
if(nd > n-1) break;
cnt++;
v[b[i]].pb({nd,cnt});
if(git[nd] == p[i]) break;
}
}
// for(int i = 0; i < n; i++){
// cout << i << '\n';
// for(auto j: v[i]) cout << j.ff << ' ' << j.ss << '\n';
// }
f();
cout << (dis[b[2]] == 1e9 ? -1 : dis[b[2]]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7532 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7512 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7536 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7596 KB |
Output is correct |
10 |
Correct |
2 ms |
7924 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7512 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5724 KB |
Output is correct |
9 |
Correct |
2 ms |
5724 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
5 ms |
8152 KB |
Output is correct |
13 |
Correct |
2 ms |
5980 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
6036 KB |
Output is correct |
16 |
Correct |
2 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5968 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5960 KB |
Output is correct |
23 |
Correct |
3 ms |
5936 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
54 ms |
39040 KB |
Output is correct |
27 |
Correct |
53 ms |
39008 KB |
Output is correct |
28 |
Correct |
3 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6712 KB |
Output is correct |
30 |
Correct |
3 ms |
6236 KB |
Output is correct |
31 |
Correct |
4 ms |
6236 KB |
Output is correct |
32 |
Correct |
4 ms |
6236 KB |
Output is correct |
33 |
Correct |
5 ms |
7260 KB |
Output is correct |
34 |
Correct |
6 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5724 KB |
Output is correct |
3 |
Correct |
3 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5720 KB |
Output is correct |
10 |
Correct |
2 ms |
5956 KB |
Output is correct |
11 |
Correct |
2 ms |
5968 KB |
Output is correct |
12 |
Correct |
5 ms |
8152 KB |
Output is correct |
13 |
Correct |
3 ms |
5968 KB |
Output is correct |
14 |
Correct |
3 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
2 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6044 KB |
Output is correct |
18 |
Correct |
2 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
6068 KB |
Output is correct |
24 |
Correct |
4 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5968 KB |
Output is correct |
26 |
Correct |
55 ms |
38848 KB |
Output is correct |
27 |
Correct |
51 ms |
38844 KB |
Output is correct |
28 |
Correct |
2 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
6344 KB |
Output is correct |
32 |
Correct |
3 ms |
7768 KB |
Output is correct |
33 |
Correct |
4 ms |
9048 KB |
Output is correct |
34 |
Correct |
5 ms |
9052 KB |
Output is correct |
35 |
Correct |
7 ms |
7772 KB |
Output is correct |
36 |
Correct |
3 ms |
6236 KB |
Output is correct |
37 |
Correct |
10 ms |
9304 KB |
Output is correct |
38 |
Correct |
9 ms |
8796 KB |
Output is correct |
39 |
Correct |
10 ms |
8792 KB |
Output is correct |
40 |
Correct |
9 ms |
8792 KB |
Output is correct |
41 |
Correct |
9 ms |
8792 KB |
Output is correct |
42 |
Runtime error |
280 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7344 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7512 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7596 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
5 ms |
9688 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
2 ms |
7768 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7772 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7608 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
2 ms |
7512 KB |
Output is correct |
23 |
Correct |
2 ms |
7516 KB |
Output is correct |
24 |
Correct |
3 ms |
7772 KB |
Output is correct |
25 |
Correct |
2 ms |
7512 KB |
Output is correct |
26 |
Correct |
53 ms |
40688 KB |
Output is correct |
27 |
Correct |
51 ms |
40400 KB |
Output is correct |
28 |
Correct |
2 ms |
7768 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
30 |
Correct |
2 ms |
7768 KB |
Output is correct |
31 |
Correct |
3 ms |
8028 KB |
Output is correct |
32 |
Correct |
3 ms |
7772 KB |
Output is correct |
33 |
Correct |
4 ms |
8796 KB |
Output is correct |
34 |
Correct |
5 ms |
9308 KB |
Output is correct |
35 |
Correct |
8 ms |
9052 KB |
Output is correct |
36 |
Correct |
3 ms |
7632 KB |
Output is correct |
37 |
Correct |
9 ms |
10844 KB |
Output is correct |
38 |
Correct |
10 ms |
10072 KB |
Output is correct |
39 |
Correct |
9 ms |
10072 KB |
Output is correct |
40 |
Correct |
9 ms |
10332 KB |
Output is correct |
41 |
Correct |
10 ms |
10332 KB |
Output is correct |
42 |
Runtime error |
276 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |