#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
const int MXN = 3e4 + 5;
int b[MXN], p[MXN], dist[MXN];
vector<int> adj[MXN];
map<int, int> mp[MXN];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) dist[i] = inf;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
if (p[i] > n) continue;
adj[b[i]].push_back(p[i]);
}
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
dist[b[0]] = 0;
pq.push({0, b[0]});
while (!pq.empty())
{
array<int, 2> f = pq.top();
pq.pop();
if (f[0] > dist[f[1]]) continue;
for (int v : adj[f[1]])
{
if (mp[v].find(f[1] % v) != mp[v].end()) continue;
mp[v][f[1] % v];
for (int i = f[1] - v; i >= 0; i -= v)
{
if (dist[i] > dist[f[1]] + ((f[1] - i) / v))
{
dist[i] = dist[f[1]] + ((f[1] - i) / v);
pq.push({dist[i], i});
}
}
for (int i = f[1] + v; i < n; i += v)
{
if (dist[i] > dist[f[1]] + ((i - f[1]) / v))
{
dist[i] = dist[f[1]] + ((i - f[1]) / v);
pq.push({dist[i], i});
}
}
}
}
cout << (dist[b[1]] >= inf ? -1 : dist[b[1]]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 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 |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 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 |
2736 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 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 |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2484 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 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 |
2648 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 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 |
2648 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 |
2848 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2904 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3048 KB |
Output is correct |
34 |
Correct |
3 ms |
3040 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 |
1 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 |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 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 |
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 |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 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 |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2796 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2648 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2904 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3048 KB |
Output is correct |
34 |
Correct |
3 ms |
3036 KB |
Output is correct |
35 |
Correct |
8 ms |
4444 KB |
Output is correct |
36 |
Correct |
2 ms |
2904 KB |
Output is correct |
37 |
Correct |
8 ms |
4444 KB |
Output is correct |
38 |
Correct |
14 ms |
5220 KB |
Output is correct |
39 |
Correct |
13 ms |
5208 KB |
Output is correct |
40 |
Correct |
11 ms |
5468 KB |
Output is correct |
41 |
Correct |
10 ms |
5208 KB |
Output is correct |
42 |
Correct |
4 ms |
3292 KB |
Output is correct |
43 |
Correct |
4 ms |
3292 KB |
Output is correct |
44 |
Correct |
4 ms |
3328 KB |
Output is correct |
45 |
Correct |
11 ms |
5376 KB |
Output is correct |
46 |
Correct |
10 ms |
5384 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 |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2804 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 |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 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 |
2916 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
3 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2836 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3048 KB |
Output is correct |
34 |
Correct |
4 ms |
2984 KB |
Output is correct |
35 |
Correct |
8 ms |
4444 KB |
Output is correct |
36 |
Correct |
3 ms |
2908 KB |
Output is correct |
37 |
Correct |
9 ms |
4444 KB |
Output is correct |
38 |
Correct |
11 ms |
5464 KB |
Output is correct |
39 |
Correct |
11 ms |
5216 KB |
Output is correct |
40 |
Correct |
10 ms |
5464 KB |
Output is correct |
41 |
Correct |
10 ms |
5212 KB |
Output is correct |
42 |
Correct |
4 ms |
3292 KB |
Output is correct |
43 |
Correct |
4 ms |
3292 KB |
Output is correct |
44 |
Correct |
4 ms |
3332 KB |
Output is correct |
45 |
Correct |
10 ms |
5372 KB |
Output is correct |
46 |
Correct |
10 ms |
5384 KB |
Output is correct |
47 |
Correct |
19 ms |
5592 KB |
Output is correct |
48 |
Correct |
5 ms |
3420 KB |
Output is correct |
49 |
Correct |
5 ms |
3420 KB |
Output is correct |
50 |
Correct |
4 ms |
3164 KB |
Output is correct |
51 |
Correct |
31 ms |
6616 KB |
Output is correct |
52 |
Correct |
24 ms |
6608 KB |
Output is correct |
53 |
Correct |
18 ms |
6356 KB |
Output is correct |
54 |
Correct |
3 ms |
3160 KB |
Output is correct |
55 |
Correct |
3 ms |
3160 KB |
Output is correct |
56 |
Correct |
8 ms |
4660 KB |
Output is correct |
57 |
Correct |
27 ms |
5072 KB |
Output is correct |
58 |
Correct |
6 ms |
3808 KB |
Output is correct |
59 |
Correct |
7 ms |
3808 KB |
Output is correct |
60 |
Correct |
7 ms |
3900 KB |
Output is correct |
61 |
Correct |
10 ms |
3928 KB |
Output is correct |
62 |
Correct |
14 ms |
5868 KB |
Output is correct |
63 |
Correct |
43 ms |
7416 KB |
Output is correct |
64 |
Correct |
47 ms |
9428 KB |
Output is correct |
65 |
Correct |
58 ms |
8652 KB |
Output is correct |
66 |
Correct |
91 ms |
9424 KB |
Output is correct |
67 |
Correct |
90 ms |
9164 KB |
Output is correct |