#include <bits/stdc++.h>
using namespace std;
/*
we run dijkstra
let B = 100
for all doges that have p >= b, just simulate the jumps
how do we deal with the "small" doges??
note that the answer is never more than 30000...
*/
const int SMALL = 100;
const int MAXN = 30000;
const int INF = 1e9;
int mintime[MAXN][SMALL + 1];
int dist[MAXN];
vector<int> doges[MAXN];
priority_queue<pair<int, int>> pq;
int main() {
int n; cin >> n;
int m; cin >> m;
for (int i = 0; i < n; i ++)
for (int j = 1; j <= SMALL; j ++)
mintime[i][j] = INF;
for (int i = 0; i < n; i ++)
dist[i] = INF;
int startnode = -1, target = -1;
for (int i = 0; i < m; i ++) {
int b, p; cin >> b >> p;
if (i == 0)
startnode = b;
else if (i == 1)
target = b;
doges[b].push_back(p);
}
if (startnode == target) {
cout << 0 << endl; return 0;
}
pq.push({0, startnode});
dist[startnode] = 0;
while (pq.size()) {
pair<int, int> xx = pq.top(); pq.pop();
int t = -xx.first;
int curnode = xx.second;
if (dist[curnode] != t)
continue;
for (auto doge : doges[curnode]) {
// big doge
if (doge > SMALL) {
int extratime = 0;
for (int j = curnode - doge; j >= 0; j -= doge) {
extratime ++;
if (dist[j] > extratime + t) {
dist[j] = extratime + t;
pq.push({-dist[j], j});
}
}
extratime = 0;
for (int j = curnode + doge; j < n; j += doge) {
extratime ++;
if (dist[j] > extratime + t) {
dist[j] = extratime + t;
pq.push({-dist[j], j});
}
}
}
// small doge
else {
// ignore entirely
if (mintime[curnode][doge] <= t)
continue;
mintime[curnode][doge] = t;
int extratime = 0;
for (int j = curnode - doge; j >= 0; j -= doge) {
extratime ++;
// stop
if (mintime[j][doge] <= t + extratime)
break;
mintime[j][doge] = t + extratime;
if (dist[j] > extratime + t) {
dist[j] = extratime + t;
pq.push({-dist[j], j});
}
}
extratime = 0;
for (int j = curnode + doge; j < n; j += doge) {
extratime ++;
// stop
if (mintime[j][doge] <= t + extratime)
break;
mintime[j][doge] = t + extratime;
if (dist[j] > extratime + t) {
dist[j] = extratime + t;
pq.push({-dist[j], j});
}
}
}
}
if (curnode == target) {
break;
}
}
if (dist[target] == INF) {
cout << -1 << endl;
} else cout << dist[target] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2652 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2808 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2648 KB |
Output is correct |
2 |
Correct |
0 ms |
2648 KB |
Output is correct |
3 |
Correct |
0 ms |
2648 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2700 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2648 KB |
Output is correct |
2 |
Correct |
0 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2648 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2656 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
0 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2664 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2904 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2776 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
2756 KB |
Output is correct |
34 |
Correct |
1 ms |
2908 KB |
Output is correct |
35 |
Correct |
7 ms |
2920 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
5 ms |
2908 KB |
Output is correct |
38 |
Correct |
8 ms |
3164 KB |
Output is correct |
39 |
Correct |
7 ms |
3036 KB |
Output is correct |
40 |
Correct |
8 ms |
3164 KB |
Output is correct |
41 |
Correct |
10 ms |
3032 KB |
Output is correct |
42 |
Correct |
7 ms |
2908 KB |
Output is correct |
43 |
Correct |
6 ms |
2984 KB |
Output is correct |
44 |
Correct |
6 ms |
2904 KB |
Output is correct |
45 |
Correct |
8 ms |
3160 KB |
Output is correct |
46 |
Correct |
7 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
0 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
0 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
7 ms |
3064 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
6 ms |
2908 KB |
Output is correct |
38 |
Correct |
8 ms |
3164 KB |
Output is correct |
39 |
Correct |
8 ms |
3164 KB |
Output is correct |
40 |
Correct |
8 ms |
3200 KB |
Output is correct |
41 |
Correct |
8 ms |
3164 KB |
Output is correct |
42 |
Correct |
6 ms |
2908 KB |
Output is correct |
43 |
Correct |
6 ms |
3000 KB |
Output is correct |
44 |
Correct |
6 ms |
2908 KB |
Output is correct |
45 |
Correct |
7 ms |
3196 KB |
Output is correct |
46 |
Correct |
11 ms |
3164 KB |
Output is correct |
47 |
Correct |
13 ms |
7900 KB |
Output is correct |
48 |
Correct |
8 ms |
11540 KB |
Output is correct |
49 |
Correct |
10 ms |
13400 KB |
Output is correct |
50 |
Correct |
7 ms |
13404 KB |
Output is correct |
51 |
Correct |
17 ms |
15112 KB |
Output is correct |
52 |
Correct |
17 ms |
14556 KB |
Output is correct |
53 |
Correct |
12 ms |
14428 KB |
Output is correct |
54 |
Correct |
3 ms |
13148 KB |
Output is correct |
55 |
Correct |
3 ms |
13148 KB |
Output is correct |
56 |
Correct |
11 ms |
14552 KB |
Output is correct |
57 |
Correct |
4 ms |
14300 KB |
Output is correct |
58 |
Correct |
8 ms |
13404 KB |
Output is correct |
59 |
Correct |
9 ms |
13660 KB |
Output is correct |
60 |
Correct |
9 ms |
13660 KB |
Output is correct |
61 |
Correct |
10 ms |
13620 KB |
Output is correct |
62 |
Correct |
12 ms |
14172 KB |
Output is correct |
63 |
Correct |
19 ms |
14644 KB |
Output is correct |
64 |
Correct |
18 ms |
14556 KB |
Output is correct |
65 |
Correct |
20 ms |
14556 KB |
Output is correct |
66 |
Correct |
31 ms |
15572 KB |
Output is correct |
67 |
Correct |
25 ms |
15568 KB |
Output is correct |