#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;
const int MAXN = 30010;
int B[MAXN], P[MAXN];
int n, m;
vector <pair <int, int> > ve[MAXN];
set <pair <int, int> > se;
int dist[MAXN];
set <int> E[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
REP(i, m) {
cin >> B[i] >> P[i];
E[B[i]].insert(P[i]);
}
REP(i, n) {
for (int t : E[i]) {
int c = 1;
while (i + t * c < n) {
int j = i + t * c;
ve[i].push_back({j, c});
if (E[j].find(c) != E[j].end()) break;
c++;
}
c = 1;
while (i - t * c >= 0) {
int j = i - t * c;
ve[i].push_back({j, c});
if (E[j].find(c) != E[j].end()) break;
c++;
}
}
}
REP(i, n) dist[i] = 1e9;
dist[B[0]] = 0;
REP(i, n) se.insert({dist[i], i});
while (se.size()) {
auto tr = *se.begin();
se.erase(se.begin());
int x = tr.second, D = tr.first;
if (x == B[1]) break;
for (auto t : ve[x]) {
int y = t.first, d = t.second;
if (dist[y] > D + d) {
se.erase({dist[y], y});
dist[y] = D + d;
se.insert({dist[y], y});
}
}
}
if (dist[B[1]] >= 1e9) cout << "-1\n";
else cout << dist[B[1]] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
5 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
4 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
5 ms |
2432 KB |
Output is correct |
8 |
Correct |
4 ms |
2432 KB |
Output is correct |
9 |
Correct |
3 ms |
2432 KB |
Output is correct |
10 |
Correct |
5 ms |
2560 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
2636 KB |
Output is correct |
13 |
Correct |
5 ms |
2560 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
3 |
Correct |
5 ms |
2432 KB |
Output is correct |
4 |
Correct |
4 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
4 ms |
2432 KB |
Output is correct |
7 |
Correct |
5 ms |
2432 KB |
Output is correct |
8 |
Correct |
9 ms |
2432 KB |
Output is correct |
9 |
Correct |
5 ms |
2432 KB |
Output is correct |
10 |
Correct |
5 ms |
2432 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
4 ms |
2432 KB |
Output is correct |
13 |
Correct |
5 ms |
2560 KB |
Output is correct |
14 |
Correct |
5 ms |
2560 KB |
Output is correct |
15 |
Correct |
5 ms |
2560 KB |
Output is correct |
16 |
Correct |
6 ms |
2560 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
9 ms |
2816 KB |
Output is correct |
21 |
Correct |
5 ms |
2560 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2944 KB |
Output is correct |
29 |
Correct |
9 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
14 ms |
3708 KB |
Output is correct |
34 |
Correct |
13 ms |
3708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
5 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
5 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
4 ms |
2432 KB |
Output is correct |
9 |
Correct |
4 ms |
2432 KB |
Output is correct |
10 |
Correct |
4 ms |
2560 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2432 KB |
Output is correct |
13 |
Correct |
4 ms |
2560 KB |
Output is correct |
14 |
Correct |
4 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2560 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
4 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2688 KB |
Output is correct |
20 |
Correct |
9 ms |
2688 KB |
Output is correct |
21 |
Correct |
5 ms |
2560 KB |
Output is correct |
22 |
Correct |
5 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
6 ms |
2816 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2944 KB |
Output is correct |
29 |
Correct |
8 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
12 ms |
3836 KB |
Output is correct |
34 |
Correct |
12 ms |
3708 KB |
Output is correct |
35 |
Correct |
26 ms |
4992 KB |
Output is correct |
36 |
Correct |
6 ms |
2944 KB |
Output is correct |
37 |
Correct |
35 ms |
5624 KB |
Output is correct |
38 |
Correct |
32 ms |
5876 KB |
Output is correct |
39 |
Correct |
26 ms |
5888 KB |
Output is correct |
40 |
Correct |
29 ms |
5888 KB |
Output is correct |
41 |
Correct |
35 ms |
5888 KB |
Output is correct |
42 |
Correct |
12 ms |
2944 KB |
Output is correct |
43 |
Correct |
10 ms |
2944 KB |
Output is correct |
44 |
Correct |
13 ms |
2944 KB |
Output is correct |
45 |
Correct |
41 ms |
8312 KB |
Output is correct |
46 |
Correct |
38 ms |
8312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
2 |
Correct |
4 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
2432 KB |
Output is correct |
4 |
Correct |
5 ms |
2432 KB |
Output is correct |
5 |
Correct |
4 ms |
2432 KB |
Output is correct |
6 |
Correct |
5 ms |
2432 KB |
Output is correct |
7 |
Correct |
5 ms |
2432 KB |
Output is correct |
8 |
Correct |
5 ms |
2432 KB |
Output is correct |
9 |
Correct |
4 ms |
2432 KB |
Output is correct |
10 |
Correct |
4 ms |
2560 KB |
Output is correct |
11 |
Correct |
8 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
4 ms |
2560 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
5 ms |
2560 KB |
Output is correct |
17 |
Correct |
6 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2684 KB |
Output is correct |
21 |
Correct |
4 ms |
2560 KB |
Output is correct |
22 |
Correct |
7 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2816 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
8 ms |
3200 KB |
Output is correct |
30 |
Correct |
6 ms |
2816 KB |
Output is correct |
31 |
Correct |
8 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
16 ms |
3836 KB |
Output is correct |
34 |
Correct |
13 ms |
3836 KB |
Output is correct |
35 |
Correct |
26 ms |
4984 KB |
Output is correct |
36 |
Correct |
9 ms |
2944 KB |
Output is correct |
37 |
Correct |
34 ms |
5632 KB |
Output is correct |
38 |
Correct |
44 ms |
5880 KB |
Output is correct |
39 |
Correct |
45 ms |
6008 KB |
Output is correct |
40 |
Correct |
38 ms |
5880 KB |
Output is correct |
41 |
Correct |
36 ms |
5880 KB |
Output is correct |
42 |
Correct |
15 ms |
2944 KB |
Output is correct |
43 |
Correct |
14 ms |
2816 KB |
Output is correct |
44 |
Correct |
12 ms |
2944 KB |
Output is correct |
45 |
Correct |
40 ms |
8384 KB |
Output is correct |
46 |
Correct |
45 ms |
8312 KB |
Output is correct |
47 |
Correct |
82 ms |
12792 KB |
Output is correct |
48 |
Correct |
32 ms |
7172 KB |
Output is correct |
49 |
Correct |
25 ms |
6400 KB |
Output is correct |
50 |
Correct |
22 ms |
6144 KB |
Output is correct |
51 |
Correct |
96 ms |
9212 KB |
Output is correct |
52 |
Correct |
87 ms |
9400 KB |
Output is correct |
53 |
Correct |
36 ms |
6364 KB |
Output is correct |
54 |
Correct |
15 ms |
4224 KB |
Output is correct |
55 |
Correct |
23 ms |
4732 KB |
Output is correct |
56 |
Correct |
30 ms |
6776 KB |
Output is correct |
57 |
Correct |
127 ms |
54392 KB |
Output is correct |
58 |
Correct |
24 ms |
5112 KB |
Output is correct |
59 |
Correct |
28 ms |
5876 KB |
Output is correct |
60 |
Correct |
30 ms |
6268 KB |
Output is correct |
61 |
Correct |
35 ms |
5732 KB |
Output is correct |
62 |
Correct |
58 ms |
10644 KB |
Output is correct |
63 |
Correct |
157 ms |
25812 KB |
Output is correct |
64 |
Correct |
153 ms |
29464 KB |
Output is correct |
65 |
Correct |
234 ms |
38740 KB |
Output is correct |
66 |
Correct |
480 ms |
63388 KB |
Output is correct |
67 |
Correct |
436 ms |
63332 KB |
Output is correct |