#include <bits/stdc++.h>
using namespace std;
/// 12:08
const int Nmax = 30005;
const int base = 30007;
typedef pair<int,int> Pair;
vector<int> v[Nmax];
vector<Pair> edge[Nmax];
int salt[Nmax], pos[Nmax], dist[Nmax], last[Nmax];
int n, m;
int solve(int start, int finish)
{
priority_queue< Pair, vector<Pair>, greater<Pair> > heap;
heap.push({0, start});
int i;
for(i=0; i<n; ++i) dist[i] = 1e9;
dist[start] = 0;
while(heap.size())
{
int node, dst;
tie(dst, node) = heap.top();
heap.pop();
if(dist[node] != dst) continue;
if(node == finish) return dst;
for(auto &it : edge[node])
if(dist[node] + it.second < dist[it.first])
{
dist[it.first] = dist[node] + it.second;
heap.push({dist[it.first], it.first});
}
}
return -1;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
int i;
for(i=0; i<m; ++i)
{
cin >> pos[i] >> salt[i];
v[salt[i]].push_back(pos[i]);
}
for(i=0; i<n; ++i)
{
sort(v[i].begin(), v[i].end());
v[i].erase( unique(v[i].begin(), v[i].end()) , v[i].end() );
}
for(i=1; i<=30000; ++i)
{
for(auto it : v[i]) last[it] = i;
for(auto it : v[i])
{
int p, dst;
for(p = it - i, dst = 1; p >= 0; p -= i, ++dst)
{
edge[it].push_back({p, dst});
if(last[p] == i) break;
}
for(p = it + i, dst = 1; p<n; p += i, ++dst)
{
edge[it].push_back({p, dst});
if(last[p] == i) break;
}
}
}
cout << solve(pos[0], pos[1]) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1788 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
6 ms |
1656 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1784 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1912 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1784 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
6 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
7 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1784 KB |
Output is correct |
20 |
Correct |
6 ms |
1784 KB |
Output is correct |
21 |
Correct |
7 ms |
1784 KB |
Output is correct |
22 |
Correct |
6 ms |
1784 KB |
Output is correct |
23 |
Correct |
7 ms |
1912 KB |
Output is correct |
24 |
Correct |
7 ms |
1912 KB |
Output is correct |
25 |
Correct |
7 ms |
1912 KB |
Output is correct |
26 |
Correct |
6 ms |
1912 KB |
Output is correct |
27 |
Correct |
7 ms |
1912 KB |
Output is correct |
28 |
Correct |
7 ms |
2040 KB |
Output is correct |
29 |
Correct |
9 ms |
2552 KB |
Output is correct |
30 |
Correct |
7 ms |
2040 KB |
Output is correct |
31 |
Correct |
8 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2040 KB |
Output is correct |
33 |
Correct |
11 ms |
3320 KB |
Output is correct |
34 |
Correct |
10 ms |
3324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1780 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
6 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1788 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1912 KB |
Output is correct |
15 |
Correct |
7 ms |
1912 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
1912 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1784 KB |
Output is correct |
20 |
Correct |
7 ms |
1912 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
6 ms |
1784 KB |
Output is correct |
23 |
Correct |
6 ms |
1912 KB |
Output is correct |
24 |
Correct |
7 ms |
1912 KB |
Output is correct |
25 |
Correct |
7 ms |
1912 KB |
Output is correct |
26 |
Correct |
7 ms |
1912 KB |
Output is correct |
27 |
Correct |
7 ms |
1888 KB |
Output is correct |
28 |
Correct |
7 ms |
2040 KB |
Output is correct |
29 |
Correct |
9 ms |
2680 KB |
Output is correct |
30 |
Correct |
7 ms |
2040 KB |
Output is correct |
31 |
Correct |
8 ms |
2300 KB |
Output is correct |
32 |
Correct |
7 ms |
2040 KB |
Output is correct |
33 |
Correct |
12 ms |
3320 KB |
Output is correct |
34 |
Correct |
11 ms |
3352 KB |
Output is correct |
35 |
Correct |
15 ms |
3192 KB |
Output is correct |
36 |
Correct |
8 ms |
2040 KB |
Output is correct |
37 |
Correct |
16 ms |
4216 KB |
Output is correct |
38 |
Correct |
18 ms |
4088 KB |
Output is correct |
39 |
Correct |
17 ms |
3964 KB |
Output is correct |
40 |
Correct |
18 ms |
3960 KB |
Output is correct |
41 |
Correct |
17 ms |
3960 KB |
Output is correct |
42 |
Correct |
13 ms |
2296 KB |
Output is correct |
43 |
Correct |
12 ms |
2296 KB |
Output is correct |
44 |
Correct |
12 ms |
2296 KB |
Output is correct |
45 |
Correct |
25 ms |
6776 KB |
Output is correct |
46 |
Correct |
22 ms |
6904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1784 KB |
Output is correct |
2 |
Correct |
5 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1784 KB |
Output is correct |
5 |
Correct |
6 ms |
1784 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
2040 KB |
Output is correct |
18 |
Correct |
6 ms |
1912 KB |
Output is correct |
19 |
Correct |
6 ms |
1784 KB |
Output is correct |
20 |
Correct |
7 ms |
1784 KB |
Output is correct |
21 |
Correct |
6 ms |
1784 KB |
Output is correct |
22 |
Correct |
6 ms |
1784 KB |
Output is correct |
23 |
Correct |
6 ms |
1912 KB |
Output is correct |
24 |
Correct |
7 ms |
1912 KB |
Output is correct |
25 |
Correct |
7 ms |
1912 KB |
Output is correct |
26 |
Correct |
7 ms |
1912 KB |
Output is correct |
27 |
Correct |
6 ms |
1912 KB |
Output is correct |
28 |
Correct |
7 ms |
2040 KB |
Output is correct |
29 |
Correct |
9 ms |
2552 KB |
Output is correct |
30 |
Correct |
7 ms |
2168 KB |
Output is correct |
31 |
Correct |
8 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2044 KB |
Output is correct |
33 |
Correct |
11 ms |
3320 KB |
Output is correct |
34 |
Correct |
11 ms |
3352 KB |
Output is correct |
35 |
Correct |
15 ms |
3192 KB |
Output is correct |
36 |
Correct |
7 ms |
2040 KB |
Output is correct |
37 |
Correct |
15 ms |
4216 KB |
Output is correct |
38 |
Correct |
18 ms |
4088 KB |
Output is correct |
39 |
Correct |
17 ms |
3960 KB |
Output is correct |
40 |
Correct |
18 ms |
3960 KB |
Output is correct |
41 |
Correct |
18 ms |
4216 KB |
Output is correct |
42 |
Correct |
11 ms |
2296 KB |
Output is correct |
43 |
Correct |
11 ms |
2300 KB |
Output is correct |
44 |
Correct |
11 ms |
2300 KB |
Output is correct |
45 |
Correct |
24 ms |
6776 KB |
Output is correct |
46 |
Correct |
22 ms |
6904 KB |
Output is correct |
47 |
Correct |
35 ms |
11384 KB |
Output is correct |
48 |
Correct |
21 ms |
4980 KB |
Output is correct |
49 |
Correct |
18 ms |
4088 KB |
Output is correct |
50 |
Correct |
15 ms |
3960 KB |
Output is correct |
51 |
Correct |
34 ms |
6712 KB |
Output is correct |
52 |
Correct |
35 ms |
7220 KB |
Output is correct |
53 |
Correct |
18 ms |
3572 KB |
Output is correct |
54 |
Correct |
8 ms |
2296 KB |
Output is correct |
55 |
Correct |
9 ms |
2804 KB |
Output is correct |
56 |
Correct |
18 ms |
3192 KB |
Output is correct |
57 |
Correct |
22 ms |
10744 KB |
Output is correct |
58 |
Correct |
14 ms |
3308 KB |
Output is correct |
59 |
Correct |
17 ms |
3572 KB |
Output is correct |
60 |
Correct |
17 ms |
4340 KB |
Output is correct |
61 |
Correct |
17 ms |
3700 KB |
Output is correct |
62 |
Correct |
28 ms |
7412 KB |
Output is correct |
63 |
Correct |
79 ms |
25852 KB |
Output is correct |
64 |
Correct |
91 ms |
34404 KB |
Output is correct |
65 |
Correct |
123 ms |
44524 KB |
Output is correct |
66 |
Correct |
226 ms |
72292 KB |
Output is correct |
67 |
Correct |
183 ms |
72292 KB |
Output is correct |