//Flower_On_Stone
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 30005;
const int INF = 1000000000;
int numSkycrepers, numDoge;
vector<int> doge[MAX_N];
int dist[MAX_N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
int b[MAX_N], p[MAX_N];
void dijkstra(int x)
{
for (int i = 0; i < numSkycrepers; i++)
{
dist[i] = INF;
}
dist[x] = 0;
pq.push(make_pair(0, x));
while (!pq.empty())
{
int node, w;
tie(w, node) = pq.top();
pq.pop();
if (w != dist[node])
{
continue;
}
for (auto &p : doge[node])
{
for (int u = node + p, cnt = 1; u < numSkycrepers; u += p, cnt++)
{
if (w + cnt < dist[u])
{
dist[u] = w + cnt;
pq.push(make_pair(w + cnt, u));
}
}
for (int u = node - p, cnt = 1; u >= 0; u -= p, cnt++)
{
if (w + cnt < dist[u])
{
dist[u] = w + cnt;
pq.push(make_pair(w + cnt, u));
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> numSkycrepers >> numDoge;
for (int i = 0; i < numDoge; i++)
{
cin >> b[i] >> p[i];
doge[b[i]].push_back(p[i]);
}
if (b[0] == b[1])
{
cout << 0;
return 0;
}
for (int i = 0; i < numSkycrepers; i++)
{
sort(doge[i].begin(), doge[i].end(), greater<int>());
doge[i].resize(distance(doge[i].begin(), unique(doge[i].begin(), doge[i].end())));
}
dijkstra(b[0]);
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1032 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
1032 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
1036 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
976 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1044 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
1044 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
5 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1036 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
2 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
2 ms |
1040 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
31 |
Correct |
2 ms |
1100 KB |
Output is correct |
32 |
Correct |
2 ms |
1100 KB |
Output is correct |
33 |
Correct |
2 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
1028 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1028 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
4 ms |
1168 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1164 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1188 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
31 |
Correct |
2 ms |
1156 KB |
Output is correct |
32 |
Correct |
2 ms |
1100 KB |
Output is correct |
33 |
Correct |
2 ms |
1104 KB |
Output is correct |
34 |
Correct |
2 ms |
1100 KB |
Output is correct |
35 |
Correct |
8 ms |
1740 KB |
Output is correct |
36 |
Correct |
2 ms |
1100 KB |
Output is correct |
37 |
Correct |
7 ms |
1612 KB |
Output is correct |
38 |
Correct |
10 ms |
1816 KB |
Output is correct |
39 |
Correct |
9 ms |
1856 KB |
Output is correct |
40 |
Correct |
9 ms |
1840 KB |
Output is correct |
41 |
Correct |
9 ms |
1868 KB |
Output is correct |
42 |
Correct |
6 ms |
1684 KB |
Output is correct |
43 |
Correct |
6 ms |
1676 KB |
Output is correct |
44 |
Correct |
9 ms |
1720 KB |
Output is correct |
45 |
Correct |
10 ms |
1696 KB |
Output is correct |
46 |
Correct |
10 ms |
1776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
976 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1036 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
2 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
5 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
2 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1020 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
2 ms |
1100 KB |
Output is correct |
31 |
Correct |
2 ms |
1100 KB |
Output is correct |
32 |
Correct |
2 ms |
1100 KB |
Output is correct |
33 |
Correct |
2 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
1100 KB |
Output is correct |
35 |
Correct |
7 ms |
1740 KB |
Output is correct |
36 |
Correct |
2 ms |
1100 KB |
Output is correct |
37 |
Correct |
7 ms |
1588 KB |
Output is correct |
38 |
Correct |
9 ms |
1784 KB |
Output is correct |
39 |
Correct |
9 ms |
1740 KB |
Output is correct |
40 |
Correct |
9 ms |
1868 KB |
Output is correct |
41 |
Correct |
9 ms |
1868 KB |
Output is correct |
42 |
Correct |
6 ms |
1708 KB |
Output is correct |
43 |
Correct |
6 ms |
1612 KB |
Output is correct |
44 |
Correct |
9 ms |
1724 KB |
Output is correct |
45 |
Correct |
8 ms |
1740 KB |
Output is correct |
46 |
Correct |
8 ms |
1740 KB |
Output is correct |
47 |
Correct |
18 ms |
2504 KB |
Output is correct |
48 |
Correct |
7 ms |
1872 KB |
Output is correct |
49 |
Correct |
7 ms |
1868 KB |
Output is correct |
50 |
Correct |
7 ms |
1740 KB |
Output is correct |
51 |
Correct |
33 ms |
3380 KB |
Output is correct |
52 |
Correct |
27 ms |
2888 KB |
Output is correct |
53 |
Correct |
16 ms |
2888 KB |
Output is correct |
54 |
Correct |
2 ms |
1356 KB |
Output is correct |
55 |
Correct |
3 ms |
1292 KB |
Output is correct |
56 |
Correct |
661 ms |
2836 KB |
Output is correct |
57 |
Correct |
47 ms |
3392 KB |
Output is correct |
58 |
Correct |
8 ms |
1868 KB |
Output is correct |
59 |
Correct |
9 ms |
1876 KB |
Output is correct |
60 |
Correct |
9 ms |
1892 KB |
Output is correct |
61 |
Correct |
9 ms |
1916 KB |
Output is correct |
62 |
Correct |
11 ms |
2528 KB |
Output is correct |
63 |
Correct |
31 ms |
3016 KB |
Output is correct |
64 |
Correct |
31 ms |
2888 KB |
Output is correct |
65 |
Correct |
36 ms |
4040 KB |
Output is correct |
66 |
Correct |
41 ms |
3932 KB |
Output is correct |
67 |
Correct |
41 ms |
3880 KB |
Output is correct |