//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
priority_queue<pair<int,pair<int,int>>> q;
vector<int> buildings[30001];
int power[30001];
int dist[30000][201];
bool visited[30000][201];
const int INF = 1e15;
int threshold = 200;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for(auto&i:dist)for(int&j:i)j=INF;
int n,m;
cin >> n >> m;
threshold = sqrt(n);
int base,tar;
cin>>base>>power[0]>>tar>>power[1];
buildings[base].emplace_back(power[0]);
buildings[tar].emplace_back(power[1]);
for(int i=2;i<m;i++){
int b;cin>>b>>power[i];
buildings[b].emplace_back(power[i]);
}
q.emplace(0, make_pair(base,0));
dist[base][0] = 0;
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.second.first][curr.second.second])continue;
visited[curr.second.first][curr.second.second]=true;
if(curr.second.first==tar){
cout << -curr.first << '\n';
return 0;
}
if(curr.second.second==0)for(int&i:buildings[curr.second.first]) {
if(i>threshold){
for (int j = 1; curr.second.first-j*i>= 0; ++j) {
int posnext = curr.second.first - j*i;
if (dist[posnext][0] > j-curr.first) {
dist[posnext][0] = j-curr.first;
q.emplace(curr.first - j, make_pair(posnext, 0));
}
}
for (int j = 1; curr.second.first+j*i < n; ++j) {
int posnext = curr.second.first + j*i;
if (dist[posnext][0] > j-curr.first) {
dist[posnext][0] = j-curr.first;
q.emplace(curr.first - j, make_pair(posnext, 0));
}
}
} else {
if(dist[curr.second.first][i] > -curr.first) {
dist[curr.second.first][i] = -curr.first;
q.emplace(curr.first, make_pair(curr.second.first, i));
}
}
}
else {
if(dist[curr.second.first][0] > -curr.first) {
dist[curr.second.first][0] = -curr.first;
q.emplace(curr.first, make_pair(curr.second.first, 0));
}
if(curr.second.first+curr.second.second<n and dist[curr.second.first+curr.second.second][curr.second.second]>1-curr.first) {
dist[curr.second.first+curr.second.second][curr.second.second] = 1-curr.first;
q.emplace(curr.first - 1, make_pair(curr.second.first + curr.second.second, curr.second.second));
}
if(curr.second.first-curr.second.second>=0 and dist[curr.second.first-curr.second.second][curr.second.second]>1-curr.first) {
dist[curr.second.first-curr.second.second][curr.second.second] = 1-curr.first;
q.emplace(curr.first - 1, make_pair(curr.second.first - curr.second.second, curr.second.second));
}
}
}
cout << "-1\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
50000 KB |
Output is correct |
2 |
Correct |
10 ms |
50012 KB |
Output is correct |
3 |
Correct |
11 ms |
50012 KB |
Output is correct |
4 |
Correct |
10 ms |
50160 KB |
Output is correct |
5 |
Correct |
10 ms |
50012 KB |
Output is correct |
6 |
Correct |
10 ms |
50012 KB |
Output is correct |
7 |
Correct |
10 ms |
50008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50012 KB |
Output is correct |
2 |
Correct |
10 ms |
50012 KB |
Output is correct |
3 |
Correct |
10 ms |
50008 KB |
Output is correct |
4 |
Correct |
10 ms |
50012 KB |
Output is correct |
5 |
Correct |
10 ms |
50008 KB |
Output is correct |
6 |
Correct |
10 ms |
50012 KB |
Output is correct |
7 |
Correct |
10 ms |
50172 KB |
Output is correct |
8 |
Correct |
10 ms |
50012 KB |
Output is correct |
9 |
Correct |
10 ms |
50012 KB |
Output is correct |
10 |
Correct |
10 ms |
50012 KB |
Output is correct |
11 |
Correct |
10 ms |
50268 KB |
Output is correct |
12 |
Correct |
10 ms |
50156 KB |
Output is correct |
13 |
Correct |
10 ms |
50268 KB |
Output is correct |
14 |
Correct |
10 ms |
50268 KB |
Output is correct |
15 |
Correct |
10 ms |
50268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50008 KB |
Output is correct |
2 |
Correct |
10 ms |
50012 KB |
Output is correct |
3 |
Correct |
10 ms |
50012 KB |
Output is correct |
4 |
Correct |
10 ms |
50160 KB |
Output is correct |
5 |
Correct |
15 ms |
50012 KB |
Output is correct |
6 |
Correct |
9 ms |
50264 KB |
Output is correct |
7 |
Correct |
11 ms |
50012 KB |
Output is correct |
8 |
Correct |
10 ms |
50012 KB |
Output is correct |
9 |
Correct |
10 ms |
50172 KB |
Output is correct |
10 |
Correct |
10 ms |
50012 KB |
Output is correct |
11 |
Correct |
11 ms |
50268 KB |
Output is correct |
12 |
Correct |
10 ms |
50268 KB |
Output is correct |
13 |
Correct |
10 ms |
50268 KB |
Output is correct |
14 |
Correct |
10 ms |
50268 KB |
Output is correct |
15 |
Correct |
10 ms |
50268 KB |
Output is correct |
16 |
Correct |
11 ms |
50172 KB |
Output is correct |
17 |
Correct |
11 ms |
50268 KB |
Output is correct |
18 |
Correct |
10 ms |
50012 KB |
Output is correct |
19 |
Correct |
10 ms |
50268 KB |
Output is correct |
20 |
Correct |
11 ms |
50524 KB |
Output is correct |
21 |
Correct |
10 ms |
50012 KB |
Output is correct |
22 |
Correct |
10 ms |
50200 KB |
Output is correct |
23 |
Correct |
11 ms |
50524 KB |
Output is correct |
24 |
Correct |
13 ms |
50520 KB |
Output is correct |
25 |
Correct |
10 ms |
50524 KB |
Output is correct |
26 |
Correct |
10 ms |
50524 KB |
Output is correct |
27 |
Correct |
11 ms |
50576 KB |
Output is correct |
28 |
Correct |
12 ms |
50524 KB |
Output is correct |
29 |
Correct |
15 ms |
50520 KB |
Output is correct |
30 |
Correct |
11 ms |
50520 KB |
Output is correct |
31 |
Correct |
14 ms |
50524 KB |
Output is correct |
32 |
Correct |
12 ms |
50524 KB |
Output is correct |
33 |
Correct |
18 ms |
50780 KB |
Output is correct |
34 |
Correct |
14 ms |
50636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
50012 KB |
Output is correct |
2 |
Correct |
10 ms |
50152 KB |
Output is correct |
3 |
Correct |
10 ms |
50084 KB |
Output is correct |
4 |
Correct |
10 ms |
50008 KB |
Output is correct |
5 |
Correct |
10 ms |
50012 KB |
Output is correct |
6 |
Correct |
11 ms |
50208 KB |
Output is correct |
7 |
Correct |
10 ms |
50012 KB |
Output is correct |
8 |
Correct |
10 ms |
50012 KB |
Output is correct |
9 |
Correct |
10 ms |
50012 KB |
Output is correct |
10 |
Correct |
10 ms |
50012 KB |
Output is correct |
11 |
Correct |
10 ms |
50268 KB |
Output is correct |
12 |
Correct |
10 ms |
50264 KB |
Output is correct |
13 |
Correct |
10 ms |
50216 KB |
Output is correct |
14 |
Correct |
11 ms |
50268 KB |
Output is correct |
15 |
Correct |
10 ms |
50268 KB |
Output is correct |
16 |
Correct |
11 ms |
50012 KB |
Output is correct |
17 |
Correct |
10 ms |
50408 KB |
Output is correct |
18 |
Correct |
10 ms |
50012 KB |
Output is correct |
19 |
Correct |
10 ms |
50264 KB |
Output is correct |
20 |
Correct |
10 ms |
50524 KB |
Output is correct |
21 |
Correct |
10 ms |
50008 KB |
Output is correct |
22 |
Correct |
10 ms |
50012 KB |
Output is correct |
23 |
Correct |
10 ms |
50524 KB |
Output is correct |
24 |
Correct |
11 ms |
50456 KB |
Output is correct |
25 |
Correct |
10 ms |
50520 KB |
Output is correct |
26 |
Correct |
10 ms |
50524 KB |
Output is correct |
27 |
Correct |
11 ms |
50524 KB |
Output is correct |
28 |
Correct |
12 ms |
50524 KB |
Output is correct |
29 |
Correct |
16 ms |
50476 KB |
Output is correct |
30 |
Correct |
12 ms |
50524 KB |
Output is correct |
31 |
Correct |
21 ms |
50520 KB |
Output is correct |
32 |
Correct |
12 ms |
50524 KB |
Output is correct |
33 |
Correct |
19 ms |
50780 KB |
Output is correct |
34 |
Correct |
15 ms |
50780 KB |
Output is correct |
35 |
Correct |
15 ms |
50780 KB |
Output is correct |
36 |
Correct |
11 ms |
50432 KB |
Output is correct |
37 |
Correct |
14 ms |
51036 KB |
Output is correct |
38 |
Correct |
16 ms |
51036 KB |
Output is correct |
39 |
Correct |
14 ms |
50668 KB |
Output is correct |
40 |
Correct |
15 ms |
51288 KB |
Output is correct |
41 |
Correct |
15 ms |
51036 KB |
Output is correct |
42 |
Correct |
14 ms |
50904 KB |
Output is correct |
43 |
Correct |
14 ms |
50908 KB |
Output is correct |
44 |
Correct |
13 ms |
50944 KB |
Output is correct |
45 |
Correct |
22 ms |
51152 KB |
Output is correct |
46 |
Correct |
16 ms |
51228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
50012 KB |
Output is correct |
2 |
Correct |
10 ms |
50220 KB |
Output is correct |
3 |
Correct |
11 ms |
50012 KB |
Output is correct |
4 |
Correct |
10 ms |
50012 KB |
Output is correct |
5 |
Correct |
10 ms |
50012 KB |
Output is correct |
6 |
Correct |
10 ms |
50216 KB |
Output is correct |
7 |
Correct |
10 ms |
50140 KB |
Output is correct |
8 |
Correct |
10 ms |
50176 KB |
Output is correct |
9 |
Correct |
12 ms |
50220 KB |
Output is correct |
10 |
Correct |
10 ms |
50164 KB |
Output is correct |
11 |
Correct |
12 ms |
50144 KB |
Output is correct |
12 |
Correct |
10 ms |
50268 KB |
Output is correct |
13 |
Correct |
10 ms |
50196 KB |
Output is correct |
14 |
Correct |
11 ms |
50268 KB |
Output is correct |
15 |
Correct |
10 ms |
50268 KB |
Output is correct |
16 |
Correct |
10 ms |
50180 KB |
Output is correct |
17 |
Correct |
15 ms |
50436 KB |
Output is correct |
18 |
Correct |
11 ms |
50012 KB |
Output is correct |
19 |
Correct |
11 ms |
50268 KB |
Output is correct |
20 |
Correct |
11 ms |
50524 KB |
Output is correct |
21 |
Correct |
11 ms |
50012 KB |
Output is correct |
22 |
Correct |
12 ms |
50160 KB |
Output is correct |
23 |
Correct |
11 ms |
50448 KB |
Output is correct |
24 |
Correct |
11 ms |
50464 KB |
Output is correct |
25 |
Correct |
11 ms |
50412 KB |
Output is correct |
26 |
Correct |
11 ms |
50524 KB |
Output is correct |
27 |
Correct |
12 ms |
50460 KB |
Output is correct |
28 |
Correct |
11 ms |
50448 KB |
Output is correct |
29 |
Correct |
16 ms |
50660 KB |
Output is correct |
30 |
Correct |
12 ms |
50528 KB |
Output is correct |
31 |
Correct |
15 ms |
50524 KB |
Output is correct |
32 |
Correct |
12 ms |
50524 KB |
Output is correct |
33 |
Correct |
21 ms |
50684 KB |
Output is correct |
34 |
Correct |
17 ms |
50784 KB |
Output is correct |
35 |
Correct |
15 ms |
51020 KB |
Output is correct |
36 |
Correct |
13 ms |
50524 KB |
Output is correct |
37 |
Correct |
16 ms |
50972 KB |
Output is correct |
38 |
Correct |
18 ms |
51240 KB |
Output is correct |
39 |
Correct |
16 ms |
50712 KB |
Output is correct |
40 |
Correct |
15 ms |
51040 KB |
Output is correct |
41 |
Correct |
16 ms |
51192 KB |
Output is correct |
42 |
Correct |
14 ms |
50912 KB |
Output is correct |
43 |
Correct |
14 ms |
50908 KB |
Output is correct |
44 |
Correct |
15 ms |
50948 KB |
Output is correct |
45 |
Correct |
31 ms |
51180 KB |
Output is correct |
46 |
Correct |
17 ms |
51028 KB |
Output is correct |
47 |
Correct |
18 ms |
53208 KB |
Output is correct |
48 |
Correct |
14 ms |
50524 KB |
Output is correct |
49 |
Correct |
14 ms |
50568 KB |
Output is correct |
50 |
Correct |
19 ms |
50444 KB |
Output is correct |
51 |
Correct |
27 ms |
55864 KB |
Output is correct |
52 |
Correct |
37 ms |
55992 KB |
Output is correct |
53 |
Correct |
17 ms |
51548 KB |
Output is correct |
54 |
Correct |
18 ms |
54364 KB |
Output is correct |
55 |
Correct |
16 ms |
54372 KB |
Output is correct |
56 |
Correct |
19 ms |
55224 KB |
Output is correct |
57 |
Correct |
11 ms |
50268 KB |
Output is correct |
58 |
Correct |
20 ms |
54944 KB |
Output is correct |
59 |
Correct |
22 ms |
54856 KB |
Output is correct |
60 |
Correct |
23 ms |
54752 KB |
Output is correct |
61 |
Correct |
21 ms |
54692 KB |
Output is correct |
62 |
Correct |
54 ms |
55388 KB |
Output is correct |
63 |
Correct |
45 ms |
56472 KB |
Output is correct |
64 |
Correct |
62 ms |
56376 KB |
Output is correct |
65 |
Correct |
249 ms |
56548 KB |
Output is correct |
66 |
Correct |
674 ms |
57916 KB |
Output is correct |
67 |
Correct |
318 ms |
56744 KB |
Output is correct |