//#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];
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(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 |
21 ms |
48728 KB |
Output is correct |
2 |
Correct |
12 ms |
48476 KB |
Output is correct |
3 |
Correct |
10 ms |
48476 KB |
Output is correct |
4 |
Correct |
10 ms |
48472 KB |
Output is correct |
5 |
Correct |
10 ms |
48448 KB |
Output is correct |
6 |
Correct |
10 ms |
48520 KB |
Output is correct |
7 |
Correct |
10 ms |
48476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
48472 KB |
Output is correct |
2 |
Correct |
10 ms |
48476 KB |
Output is correct |
3 |
Correct |
10 ms |
48448 KB |
Output is correct |
4 |
Correct |
10 ms |
48472 KB |
Output is correct |
5 |
Correct |
11 ms |
48528 KB |
Output is correct |
6 |
Correct |
11 ms |
48732 KB |
Output is correct |
7 |
Correct |
10 ms |
48472 KB |
Output is correct |
8 |
Correct |
10 ms |
48476 KB |
Output is correct |
9 |
Correct |
10 ms |
48476 KB |
Output is correct |
10 |
Correct |
11 ms |
48476 KB |
Output is correct |
11 |
Correct |
10 ms |
48476 KB |
Output is correct |
12 |
Correct |
10 ms |
48476 KB |
Output is correct |
13 |
Correct |
10 ms |
48476 KB |
Output is correct |
14 |
Correct |
10 ms |
48472 KB |
Output is correct |
15 |
Correct |
10 ms |
48472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
48476 KB |
Output is correct |
2 |
Correct |
11 ms |
48476 KB |
Output is correct |
3 |
Correct |
10 ms |
48532 KB |
Output is correct |
4 |
Correct |
10 ms |
48476 KB |
Output is correct |
5 |
Correct |
10 ms |
48476 KB |
Output is correct |
6 |
Correct |
10 ms |
48476 KB |
Output is correct |
7 |
Correct |
11 ms |
48476 KB |
Output is correct |
8 |
Correct |
12 ms |
48476 KB |
Output is correct |
9 |
Correct |
10 ms |
48476 KB |
Output is correct |
10 |
Correct |
10 ms |
48540 KB |
Output is correct |
11 |
Correct |
10 ms |
48476 KB |
Output is correct |
12 |
Correct |
10 ms |
48472 KB |
Output is correct |
13 |
Correct |
10 ms |
48492 KB |
Output is correct |
14 |
Correct |
11 ms |
48500 KB |
Output is correct |
15 |
Correct |
12 ms |
48732 KB |
Output is correct |
16 |
Correct |
10 ms |
48476 KB |
Output is correct |
17 |
Correct |
10 ms |
48476 KB |
Output is correct |
18 |
Correct |
10 ms |
48476 KB |
Output is correct |
19 |
Correct |
10 ms |
48472 KB |
Output is correct |
20 |
Correct |
12 ms |
48472 KB |
Output is correct |
21 |
Correct |
10 ms |
48472 KB |
Output is correct |
22 |
Correct |
10 ms |
48476 KB |
Output is correct |
23 |
Correct |
11 ms |
48468 KB |
Output is correct |
24 |
Correct |
11 ms |
48544 KB |
Output is correct |
25 |
Correct |
10 ms |
48476 KB |
Output is correct |
26 |
Correct |
10 ms |
48476 KB |
Output is correct |
27 |
Correct |
11 ms |
48476 KB |
Output is correct |
28 |
Correct |
11 ms |
48476 KB |
Output is correct |
29 |
Correct |
16 ms |
48600 KB |
Output is correct |
30 |
Correct |
11 ms |
48476 KB |
Output is correct |
31 |
Correct |
14 ms |
48476 KB |
Output is correct |
32 |
Correct |
11 ms |
48476 KB |
Output is correct |
33 |
Correct |
18 ms |
48728 KB |
Output is correct |
34 |
Correct |
14 ms |
48732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
48476 KB |
Output is correct |
2 |
Correct |
10 ms |
48484 KB |
Output is correct |
3 |
Correct |
10 ms |
48476 KB |
Output is correct |
4 |
Correct |
10 ms |
48480 KB |
Output is correct |
5 |
Correct |
10 ms |
48528 KB |
Output is correct |
6 |
Correct |
9 ms |
48472 KB |
Output is correct |
7 |
Correct |
10 ms |
48472 KB |
Output is correct |
8 |
Correct |
10 ms |
48476 KB |
Output is correct |
9 |
Correct |
10 ms |
48532 KB |
Output is correct |
10 |
Correct |
10 ms |
48472 KB |
Output is correct |
11 |
Correct |
10 ms |
48472 KB |
Output is correct |
12 |
Correct |
10 ms |
48476 KB |
Output is correct |
13 |
Correct |
11 ms |
48552 KB |
Output is correct |
14 |
Correct |
10 ms |
48476 KB |
Output is correct |
15 |
Correct |
10 ms |
48472 KB |
Output is correct |
16 |
Correct |
10 ms |
48476 KB |
Output is correct |
17 |
Correct |
10 ms |
48532 KB |
Output is correct |
18 |
Correct |
10 ms |
48548 KB |
Output is correct |
19 |
Correct |
10 ms |
48476 KB |
Output is correct |
20 |
Correct |
10 ms |
48476 KB |
Output is correct |
21 |
Correct |
12 ms |
48476 KB |
Output is correct |
22 |
Correct |
10 ms |
48476 KB |
Output is correct |
23 |
Correct |
10 ms |
48492 KB |
Output is correct |
24 |
Correct |
11 ms |
48476 KB |
Output is correct |
25 |
Correct |
10 ms |
48472 KB |
Output is correct |
26 |
Correct |
10 ms |
48476 KB |
Output is correct |
27 |
Correct |
10 ms |
48476 KB |
Output is correct |
28 |
Correct |
11 ms |
48476 KB |
Output is correct |
29 |
Correct |
16 ms |
48600 KB |
Output is correct |
30 |
Correct |
11 ms |
48476 KB |
Output is correct |
31 |
Correct |
14 ms |
48532 KB |
Output is correct |
32 |
Correct |
12 ms |
48472 KB |
Output is correct |
33 |
Correct |
18 ms |
48732 KB |
Output is correct |
34 |
Correct |
14 ms |
48732 KB |
Output is correct |
35 |
Correct |
14 ms |
49196 KB |
Output is correct |
36 |
Correct |
15 ms |
48732 KB |
Output is correct |
37 |
Correct |
13 ms |
48988 KB |
Output is correct |
38 |
Correct |
16 ms |
49288 KB |
Output is correct |
39 |
Correct |
14 ms |
49228 KB |
Output is correct |
40 |
Correct |
15 ms |
49244 KB |
Output is correct |
41 |
Correct |
15 ms |
49244 KB |
Output is correct |
42 |
Correct |
14 ms |
49092 KB |
Output is correct |
43 |
Correct |
13 ms |
49116 KB |
Output is correct |
44 |
Correct |
13 ms |
49152 KB |
Output is correct |
45 |
Correct |
22 ms |
49244 KB |
Output is correct |
46 |
Correct |
15 ms |
49496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
48476 KB |
Output is correct |
2 |
Correct |
10 ms |
48476 KB |
Output is correct |
3 |
Correct |
10 ms |
48476 KB |
Output is correct |
4 |
Correct |
10 ms |
48476 KB |
Output is correct |
5 |
Correct |
9 ms |
48476 KB |
Output is correct |
6 |
Correct |
10 ms |
48476 KB |
Output is correct |
7 |
Correct |
10 ms |
48468 KB |
Output is correct |
8 |
Correct |
11 ms |
48500 KB |
Output is correct |
9 |
Correct |
10 ms |
48528 KB |
Output is correct |
10 |
Correct |
10 ms |
48536 KB |
Output is correct |
11 |
Correct |
10 ms |
48476 KB |
Output is correct |
12 |
Correct |
10 ms |
48472 KB |
Output is correct |
13 |
Correct |
10 ms |
48476 KB |
Output is correct |
14 |
Correct |
10 ms |
48476 KB |
Output is correct |
15 |
Correct |
10 ms |
48476 KB |
Output is correct |
16 |
Correct |
10 ms |
48472 KB |
Output is correct |
17 |
Correct |
11 ms |
48476 KB |
Output is correct |
18 |
Correct |
12 ms |
48536 KB |
Output is correct |
19 |
Correct |
10 ms |
48472 KB |
Output is correct |
20 |
Correct |
10 ms |
48476 KB |
Output is correct |
21 |
Correct |
10 ms |
48476 KB |
Output is correct |
22 |
Correct |
11 ms |
48476 KB |
Output is correct |
23 |
Correct |
11 ms |
48476 KB |
Output is correct |
24 |
Correct |
10 ms |
48460 KB |
Output is correct |
25 |
Correct |
10 ms |
48476 KB |
Output is correct |
26 |
Correct |
10 ms |
48476 KB |
Output is correct |
27 |
Correct |
10 ms |
48532 KB |
Output is correct |
28 |
Correct |
11 ms |
48556 KB |
Output is correct |
29 |
Correct |
15 ms |
48476 KB |
Output is correct |
30 |
Correct |
11 ms |
48476 KB |
Output is correct |
31 |
Correct |
13 ms |
48496 KB |
Output is correct |
32 |
Correct |
12 ms |
48476 KB |
Output is correct |
33 |
Correct |
18 ms |
48732 KB |
Output is correct |
34 |
Correct |
16 ms |
48716 KB |
Output is correct |
35 |
Correct |
14 ms |
49244 KB |
Output is correct |
36 |
Correct |
11 ms |
48476 KB |
Output is correct |
37 |
Correct |
13 ms |
48920 KB |
Output is correct |
38 |
Correct |
15 ms |
49244 KB |
Output is correct |
39 |
Correct |
14 ms |
49240 KB |
Output is correct |
40 |
Correct |
14 ms |
49244 KB |
Output is correct |
41 |
Correct |
15 ms |
49476 KB |
Output is correct |
42 |
Correct |
13 ms |
49116 KB |
Output is correct |
43 |
Correct |
13 ms |
49116 KB |
Output is correct |
44 |
Correct |
13 ms |
49196 KB |
Output is correct |
45 |
Correct |
21 ms |
49240 KB |
Output is correct |
46 |
Correct |
21 ms |
49292 KB |
Output is correct |
47 |
Correct |
19 ms |
50012 KB |
Output is correct |
48 |
Correct |
14 ms |
49244 KB |
Output is correct |
49 |
Correct |
14 ms |
49244 KB |
Output is correct |
50 |
Correct |
13 ms |
48988 KB |
Output is correct |
51 |
Correct |
22 ms |
50264 KB |
Output is correct |
52 |
Correct |
24 ms |
50264 KB |
Output is correct |
53 |
Correct |
16 ms |
49752 KB |
Output is correct |
54 |
Correct |
10 ms |
48476 KB |
Output is correct |
55 |
Correct |
11 ms |
48476 KB |
Output is correct |
56 |
Correct |
18 ms |
49760 KB |
Output is correct |
57 |
Correct |
11 ms |
48476 KB |
Output is correct |
58 |
Correct |
17 ms |
48988 KB |
Output is correct |
59 |
Correct |
20 ms |
49120 KB |
Output is correct |
60 |
Correct |
22 ms |
49100 KB |
Output is correct |
61 |
Correct |
17 ms |
49116 KB |
Output is correct |
62 |
Correct |
44 ms |
49848 KB |
Output is correct |
63 |
Correct |
40 ms |
50896 KB |
Output is correct |
64 |
Correct |
42 ms |
50904 KB |
Output is correct |
65 |
Correct |
232 ms |
52432 KB |
Output is correct |
66 |
Correct |
648 ms |
53204 KB |
Output is correct |
67 |
Correct |
331 ms |
53448 KB |
Output is correct |