// Only GOD
// believe in yourself
// Nemidam Del Be In Darde Donya!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#define bit(x, y) ((x >> y)&1)
#define sz(x) (int)x.size()
#define kill(x) return cout << x << '\n', void()
#define KILL(x) return cout << x << '\n', 0
#define int ll
const int N = 1e5+100;
const int INF = 1e9+10;
set <int> s[N];
vector<pii> G[N];
int dis[N];
int32_t main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, m;
cin >> n >> m;
int start = -1, end = -1;
for(int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
s[b].insert(p);
if(i==0)
start = b;
if(i==1)
end = b;
}
memset(dis, 63, sizeof dis);
for(int i = 0; i < n; i++){
for(int u : s[i]){
for(int j = i+u;j < n; j+= u){
G[i].pb({j, (j-i)/u});
if(s[j].find(u) != s[j].end()){
break;
}
}
for(int j = i - u; j >= 0; j-=u){
G[i].pb({j, (i-j)/u});
if(s[j].find(u) != s[j].end()){
break;
}
}
}
}
dis[start] = 0;
priority_queue<pii> q;
q.push({-dis[start], start});
while(!q.empty()){
int a = q.top().S;
q.pop();
for(auto B : G[a]){
int v = B.F, w = B.S;
if(dis[a] + w < dis[v]){
dis[v] = dis[a]+w;
q.push({-dis[v], v});
}
}
}
cout << (dis[end] > INF ? -1 : dis[end]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
5 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8084 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8160 KB |
Output is correct |
13 |
Correct |
5 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8196 KB |
Output is correct |
15 |
Correct |
5 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
6 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8020 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8124 KB |
Output is correct |
14 |
Correct |
4 ms |
8276 KB |
Output is correct |
15 |
Correct |
5 ms |
8276 KB |
Output is correct |
16 |
Correct |
5 ms |
8152 KB |
Output is correct |
17 |
Correct |
5 ms |
8380 KB |
Output is correct |
18 |
Correct |
5 ms |
8276 KB |
Output is correct |
19 |
Correct |
6 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8276 KB |
Output is correct |
21 |
Correct |
4 ms |
8148 KB |
Output is correct |
22 |
Correct |
4 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8276 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
4 ms |
8156 KB |
Output is correct |
27 |
Correct |
4 ms |
8148 KB |
Output is correct |
28 |
Correct |
5 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8788 KB |
Output is correct |
30 |
Correct |
5 ms |
8404 KB |
Output is correct |
31 |
Correct |
6 ms |
8544 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
8 ms |
9556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8124 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8144 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8276 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8276 KB |
Output is correct |
15 |
Correct |
4 ms |
8276 KB |
Output is correct |
16 |
Correct |
5 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8404 KB |
Output is correct |
18 |
Correct |
5 ms |
8276 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8276 KB |
Output is correct |
21 |
Correct |
5 ms |
8148 KB |
Output is correct |
22 |
Correct |
5 ms |
8252 KB |
Output is correct |
23 |
Correct |
6 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8292 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
4 ms |
8276 KB |
Output is correct |
27 |
Correct |
6 ms |
8240 KB |
Output is correct |
28 |
Correct |
5 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8788 KB |
Output is correct |
30 |
Correct |
5 ms |
8432 KB |
Output is correct |
31 |
Correct |
6 ms |
8532 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
8 ms |
9556 KB |
Output is correct |
35 |
Correct |
18 ms |
10324 KB |
Output is correct |
36 |
Correct |
6 ms |
8532 KB |
Output is correct |
37 |
Correct |
20 ms |
11000 KB |
Output is correct |
38 |
Correct |
23 ms |
11220 KB |
Output is correct |
39 |
Correct |
30 ms |
11320 KB |
Output is correct |
40 |
Correct |
23 ms |
11220 KB |
Output is correct |
41 |
Correct |
25 ms |
11284 KB |
Output is correct |
42 |
Correct |
7 ms |
8276 KB |
Output is correct |
43 |
Correct |
8 ms |
8164 KB |
Output is correct |
44 |
Correct |
7 ms |
8276 KB |
Output is correct |
45 |
Correct |
23 ms |
13780 KB |
Output is correct |
46 |
Correct |
23 ms |
13836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
6 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
6 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8152 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8276 KB |
Output is correct |
12 |
Correct |
5 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
4 ms |
8276 KB |
Output is correct |
15 |
Correct |
5 ms |
8276 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
5 ms |
8404 KB |
Output is correct |
18 |
Correct |
5 ms |
8276 KB |
Output is correct |
19 |
Correct |
4 ms |
8148 KB |
Output is correct |
20 |
Correct |
5 ms |
8276 KB |
Output is correct |
21 |
Correct |
6 ms |
8148 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
5 ms |
8276 KB |
Output is correct |
24 |
Correct |
6 ms |
8408 KB |
Output is correct |
25 |
Correct |
6 ms |
8388 KB |
Output is correct |
26 |
Correct |
6 ms |
8236 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
5 ms |
8532 KB |
Output is correct |
29 |
Correct |
6 ms |
8788 KB |
Output is correct |
30 |
Correct |
5 ms |
8404 KB |
Output is correct |
31 |
Correct |
5 ms |
8532 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
8 ms |
9556 KB |
Output is correct |
35 |
Correct |
17 ms |
10312 KB |
Output is correct |
36 |
Correct |
6 ms |
8500 KB |
Output is correct |
37 |
Correct |
18 ms |
11012 KB |
Output is correct |
38 |
Correct |
30 ms |
11220 KB |
Output is correct |
39 |
Correct |
21 ms |
11348 KB |
Output is correct |
40 |
Correct |
23 ms |
11212 KB |
Output is correct |
41 |
Correct |
22 ms |
11176 KB |
Output is correct |
42 |
Correct |
10 ms |
8148 KB |
Output is correct |
43 |
Correct |
8 ms |
8148 KB |
Output is correct |
44 |
Correct |
7 ms |
8308 KB |
Output is correct |
45 |
Correct |
24 ms |
13780 KB |
Output is correct |
46 |
Correct |
24 ms |
13908 KB |
Output is correct |
47 |
Correct |
45 ms |
18344 KB |
Output is correct |
48 |
Correct |
19 ms |
11492 KB |
Output is correct |
49 |
Correct |
18 ms |
10548 KB |
Output is correct |
50 |
Correct |
13 ms |
10288 KB |
Output is correct |
51 |
Correct |
39 ms |
13536 KB |
Output is correct |
52 |
Correct |
38 ms |
14036 KB |
Output is correct |
53 |
Correct |
19 ms |
11104 KB |
Output is correct |
54 |
Correct |
6 ms |
8580 KB |
Output is correct |
55 |
Correct |
7 ms |
9040 KB |
Output is correct |
56 |
Correct |
18 ms |
10452 KB |
Output is correct |
57 |
Correct |
38 ms |
17148 KB |
Output is correct |
58 |
Correct |
11 ms |
9164 KB |
Output is correct |
59 |
Correct |
11 ms |
9424 KB |
Output is correct |
60 |
Correct |
13 ms |
10076 KB |
Output is correct |
61 |
Correct |
12 ms |
9532 KB |
Output is correct |
62 |
Correct |
22 ms |
14580 KB |
Output is correct |
63 |
Correct |
62 ms |
30988 KB |
Output is correct |
64 |
Correct |
70 ms |
34188 KB |
Output is correct |
65 |
Correct |
96 ms |
43828 KB |
Output is correct |
66 |
Correct |
199 ms |
70792 KB |
Output is correct |
67 |
Correct |
177 ms |
70940 KB |
Output is correct |