#include <bits/stdc++.h>
using namespace std;
int n, m, dist[30002], poz;
set<int>s[30002];
bool viz[30002];
int main()
{
cin >> n >> m;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int> > > q;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if (i == 1)
poz = b;
s[b].insert(p);
if (i == 0)
q.push({0, b});
}
for (int i = 0; i < n; i++)
dist[i] = 1e9;
dist[q.top().second] = 0;
while (!q.empty()) {
auto x = q.top();
q.pop();
if (dist[x.second] != x.first)
continue;
if (x.second == poz)
break;
for (auto it : s[x.second]) {
for (int i = x.second+it; i < n; i += it) {
if (s[i].size() && dist[i] > dist[x.second] + (i - x.second) / it) {
dist[i] = dist[x.second] + (i - x.second) / it;
q.push({dist[i], i});
}
if (s[i].count(it))
break;
}
for (int i = x.second-it; i >= 0; i -= it) {
if (s[i].size() && dist[i] > dist[x.second] + (x.second - i) / it) {
dist[i] = dist[x.second] + (x.second - i) / it;
q.push({dist[i], i});
}
if (s[i].count(it))
break;
}
}
}
if (dist[poz] == 1e9)
dist[poz] = -1;
cout << dist[poz];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
2 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
2 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
3 ms |
1772 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
1772 KB |
Output is correct |
15 |
Correct |
3 ms |
1772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
2 ms |
1772 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1792 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
1772 KB |
Output is correct |
15 |
Correct |
3 ms |
1772 KB |
Output is correct |
16 |
Correct |
2 ms |
1920 KB |
Output is correct |
17 |
Correct |
3 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1772 KB |
Output is correct |
19 |
Correct |
2 ms |
1772 KB |
Output is correct |
20 |
Correct |
3 ms |
1772 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1772 KB |
Output is correct |
24 |
Correct |
3 ms |
1900 KB |
Output is correct |
25 |
Correct |
3 ms |
1900 KB |
Output is correct |
26 |
Correct |
2 ms |
1772 KB |
Output is correct |
27 |
Correct |
2 ms |
1772 KB |
Output is correct |
28 |
Correct |
3 ms |
1772 KB |
Output is correct |
29 |
Correct |
2 ms |
1772 KB |
Output is correct |
30 |
Correct |
2 ms |
1772 KB |
Output is correct |
31 |
Correct |
2 ms |
1772 KB |
Output is correct |
32 |
Correct |
2 ms |
1772 KB |
Output is correct |
33 |
Correct |
3 ms |
1900 KB |
Output is correct |
34 |
Correct |
3 ms |
1900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
2 ms |
1772 KB |
Output is correct |
6 |
Correct |
2 ms |
1920 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
2 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
3 ms |
1772 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
1772 KB |
Output is correct |
15 |
Correct |
3 ms |
1772 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
3 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1772 KB |
Output is correct |
19 |
Correct |
2 ms |
1772 KB |
Output is correct |
20 |
Correct |
2 ms |
1772 KB |
Output is correct |
21 |
Correct |
2 ms |
1772 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1772 KB |
Output is correct |
24 |
Correct |
3 ms |
1900 KB |
Output is correct |
25 |
Correct |
3 ms |
1900 KB |
Output is correct |
26 |
Correct |
2 ms |
1772 KB |
Output is correct |
27 |
Correct |
2 ms |
1772 KB |
Output is correct |
28 |
Correct |
3 ms |
1772 KB |
Output is correct |
29 |
Correct |
2 ms |
1772 KB |
Output is correct |
30 |
Correct |
2 ms |
1772 KB |
Output is correct |
31 |
Correct |
2 ms |
1772 KB |
Output is correct |
32 |
Correct |
2 ms |
1772 KB |
Output is correct |
33 |
Correct |
3 ms |
1900 KB |
Output is correct |
34 |
Correct |
3 ms |
1900 KB |
Output is correct |
35 |
Correct |
22 ms |
3052 KB |
Output is correct |
36 |
Correct |
5 ms |
2028 KB |
Output is correct |
37 |
Correct |
18 ms |
2796 KB |
Output is correct |
38 |
Correct |
24 ms |
3180 KB |
Output is correct |
39 |
Correct |
21 ms |
3180 KB |
Output is correct |
40 |
Correct |
23 ms |
3180 KB |
Output is correct |
41 |
Correct |
29 ms |
3308 KB |
Output is correct |
42 |
Correct |
14 ms |
1772 KB |
Output is correct |
43 |
Correct |
15 ms |
1792 KB |
Output is correct |
44 |
Correct |
15 ms |
1772 KB |
Output is correct |
45 |
Correct |
25 ms |
3180 KB |
Output is correct |
46 |
Correct |
25 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
2 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1772 KB |
Output is correct |
11 |
Correct |
2 ms |
1772 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
3 ms |
1772 KB |
Output is correct |
15 |
Correct |
3 ms |
1772 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
3 ms |
1900 KB |
Output is correct |
18 |
Correct |
2 ms |
1772 KB |
Output is correct |
19 |
Correct |
2 ms |
1772 KB |
Output is correct |
20 |
Correct |
2 ms |
1772 KB |
Output is correct |
21 |
Correct |
2 ms |
1792 KB |
Output is correct |
22 |
Correct |
2 ms |
1772 KB |
Output is correct |
23 |
Correct |
2 ms |
1772 KB |
Output is correct |
24 |
Correct |
3 ms |
1900 KB |
Output is correct |
25 |
Correct |
3 ms |
1900 KB |
Output is correct |
26 |
Correct |
2 ms |
1772 KB |
Output is correct |
27 |
Correct |
2 ms |
1772 KB |
Output is correct |
28 |
Correct |
3 ms |
1772 KB |
Output is correct |
29 |
Correct |
2 ms |
1772 KB |
Output is correct |
30 |
Correct |
2 ms |
1772 KB |
Output is correct |
31 |
Correct |
2 ms |
1772 KB |
Output is correct |
32 |
Correct |
2 ms |
1772 KB |
Output is correct |
33 |
Correct |
3 ms |
1900 KB |
Output is correct |
34 |
Correct |
3 ms |
1900 KB |
Output is correct |
35 |
Correct |
23 ms |
3052 KB |
Output is correct |
36 |
Correct |
4 ms |
2028 KB |
Output is correct |
37 |
Correct |
19 ms |
2796 KB |
Output is correct |
38 |
Correct |
24 ms |
3180 KB |
Output is correct |
39 |
Correct |
21 ms |
3180 KB |
Output is correct |
40 |
Correct |
22 ms |
3180 KB |
Output is correct |
41 |
Correct |
22 ms |
3180 KB |
Output is correct |
42 |
Correct |
14 ms |
1920 KB |
Output is correct |
43 |
Correct |
17 ms |
1772 KB |
Output is correct |
44 |
Correct |
14 ms |
1772 KB |
Output is correct |
45 |
Correct |
30 ms |
3184 KB |
Output is correct |
46 |
Correct |
25 ms |
3180 KB |
Output is correct |
47 |
Correct |
27 ms |
3308 KB |
Output is correct |
48 |
Correct |
17 ms |
2924 KB |
Output is correct |
49 |
Correct |
16 ms |
2796 KB |
Output is correct |
50 |
Correct |
12 ms |
2540 KB |
Output is correct |
51 |
Correct |
31 ms |
3948 KB |
Output is correct |
52 |
Correct |
32 ms |
3948 KB |
Output is correct |
53 |
Correct |
23 ms |
3436 KB |
Output is correct |
54 |
Correct |
2 ms |
1900 KB |
Output is correct |
55 |
Correct |
2 ms |
1900 KB |
Output is correct |
56 |
Correct |
19 ms |
3308 KB |
Output is correct |
57 |
Correct |
3 ms |
2028 KB |
Output is correct |
58 |
Correct |
12 ms |
1900 KB |
Output is correct |
59 |
Correct |
17 ms |
2028 KB |
Output is correct |
60 |
Correct |
19 ms |
2156 KB |
Output is correct |
61 |
Correct |
16 ms |
2284 KB |
Output is correct |
62 |
Correct |
26 ms |
3584 KB |
Output is correct |
63 |
Correct |
35 ms |
3436 KB |
Output is correct |
64 |
Correct |
36 ms |
3436 KB |
Output is correct |
65 |
Correct |
42 ms |
3436 KB |
Output is correct |
66 |
Correct |
52 ms |
3436 KB |
Output is correct |
67 |
Correct |
55 ms |
3436 KB |
Output is correct |