#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e4+10;
const int INF = 1e9+10;
typedef pair<int, int> pii;
int pos[MAXN], dist[MAXN], n;
bool mark[MAXN];
vector<pii> grafo[MAXN];
set<int> S[MAXN];
void dijkstra(int u)
{
for (int i = 0; i < n; i++)
dist[i] = INF;
dist[u] = 0;
priority_queue<pii, vector<pii>, greater<pii> > fila;
fila.push({0, u});
while (!fila.empty())
{
int u = fila.top().second;
fila.pop();
if (mark[u]) continue;
for (auto P: grafo[u])
{
int v = P.second, d = P.first;
if (dist[v] > dist[u]+d)
{
dist[v] = dist[u]+d;
fila.push({dist[v], v});
}
}
}
}
int main(void)
{
ios::sync_with_stdio(false); cin.tie(0);
int m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x;
cin >> pos[i] >> x;
S[pos[i]].insert(x);
}
for (int i = 0; i < n; i++)
{
for (set<int>::iterator it = S[i].begin(); it != S[i].end(); it++)
{
int k = *it;
for (int j = i+k; j < n; j+=k)
{
if (S[j].size() == 0) continue;
int d = (j-i)/k;
grafo[i].push_back({d, j});
if (S[j].find(k) != S[j].end()) break;
}
for (int j = i-k; j >= 0; j-=k)
{
if (S[j].size() == 0) continue;
int d = (i-j)/k;
grafo[i].push_back({d, j});
if (S[j].find(k) != S[j].end()) break;
}
}
}
dijkstra(pos[0]);
if (dist[pos[1]] == INF) cout << "-1\n";
else cout << dist[pos[1]] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2892 KB |
Output is correct |
4 |
Correct |
4 ms |
2892 KB |
Output is correct |
5 |
Correct |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
4 ms |
3036 KB |
Output is correct |
7 |
Correct |
4 ms |
3036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3036 KB |
Output is correct |
2 |
Correct |
4 ms |
3036 KB |
Output is correct |
3 |
Correct |
4 ms |
3036 KB |
Output is correct |
4 |
Correct |
4 ms |
3036 KB |
Output is correct |
5 |
Correct |
4 ms |
3036 KB |
Output is correct |
6 |
Correct |
4 ms |
3036 KB |
Output is correct |
7 |
Correct |
4 ms |
3036 KB |
Output is correct |
8 |
Correct |
4 ms |
3052 KB |
Output is correct |
9 |
Correct |
4 ms |
3052 KB |
Output is correct |
10 |
Correct |
4 ms |
3156 KB |
Output is correct |
11 |
Correct |
5 ms |
3164 KB |
Output is correct |
12 |
Correct |
4 ms |
3164 KB |
Output is correct |
13 |
Correct |
4 ms |
3200 KB |
Output is correct |
14 |
Correct |
4 ms |
3228 KB |
Output is correct |
15 |
Correct |
5 ms |
3368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3368 KB |
Output is correct |
2 |
Correct |
4 ms |
3368 KB |
Output is correct |
3 |
Correct |
4 ms |
3368 KB |
Output is correct |
4 |
Correct |
5 ms |
3368 KB |
Output is correct |
5 |
Correct |
4 ms |
3368 KB |
Output is correct |
6 |
Correct |
4 ms |
3368 KB |
Output is correct |
7 |
Correct |
4 ms |
3368 KB |
Output is correct |
8 |
Correct |
4 ms |
3368 KB |
Output is correct |
9 |
Correct |
4 ms |
3368 KB |
Output is correct |
10 |
Correct |
5 ms |
3452 KB |
Output is correct |
11 |
Correct |
5 ms |
3472 KB |
Output is correct |
12 |
Correct |
4 ms |
3472 KB |
Output is correct |
13 |
Correct |
4 ms |
3472 KB |
Output is correct |
14 |
Correct |
5 ms |
3508 KB |
Output is correct |
15 |
Correct |
5 ms |
3544 KB |
Output is correct |
16 |
Correct |
4 ms |
3548 KB |
Output is correct |
17 |
Correct |
6 ms |
3776 KB |
Output is correct |
18 |
Correct |
4 ms |
3776 KB |
Output is correct |
19 |
Correct |
4 ms |
3776 KB |
Output is correct |
20 |
Correct |
5 ms |
3776 KB |
Output is correct |
21 |
Correct |
4 ms |
3776 KB |
Output is correct |
22 |
Correct |
4 ms |
3776 KB |
Output is correct |
23 |
Correct |
5 ms |
3776 KB |
Output is correct |
24 |
Correct |
5 ms |
3776 KB |
Output is correct |
25 |
Correct |
5 ms |
3788 KB |
Output is correct |
26 |
Correct |
4 ms |
3788 KB |
Output is correct |
27 |
Correct |
4 ms |
3824 KB |
Output is correct |
28 |
Correct |
5 ms |
3972 KB |
Output is correct |
29 |
Correct |
4 ms |
3972 KB |
Output is correct |
30 |
Correct |
4 ms |
3972 KB |
Output is correct |
31 |
Correct |
4 ms |
3972 KB |
Output is correct |
32 |
Correct |
4 ms |
3972 KB |
Output is correct |
33 |
Correct |
5 ms |
3972 KB |
Output is correct |
34 |
Correct |
5 ms |
3972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3972 KB |
Output is correct |
2 |
Correct |
4 ms |
3972 KB |
Output is correct |
3 |
Correct |
5 ms |
3972 KB |
Output is correct |
4 |
Correct |
4 ms |
3972 KB |
Output is correct |
5 |
Correct |
4 ms |
3972 KB |
Output is correct |
6 |
Correct |
4 ms |
3972 KB |
Output is correct |
7 |
Correct |
4 ms |
3972 KB |
Output is correct |
8 |
Correct |
4 ms |
3972 KB |
Output is correct |
9 |
Correct |
4 ms |
3972 KB |
Output is correct |
10 |
Correct |
4 ms |
3972 KB |
Output is correct |
11 |
Correct |
5 ms |
3972 KB |
Output is correct |
12 |
Correct |
5 ms |
3972 KB |
Output is correct |
13 |
Correct |
5 ms |
3972 KB |
Output is correct |
14 |
Correct |
4 ms |
3972 KB |
Output is correct |
15 |
Correct |
5 ms |
3972 KB |
Output is correct |
16 |
Correct |
4 ms |
4008 KB |
Output is correct |
17 |
Correct |
5 ms |
4176 KB |
Output is correct |
18 |
Correct |
4 ms |
4176 KB |
Output is correct |
19 |
Correct |
4 ms |
4176 KB |
Output is correct |
20 |
Correct |
5 ms |
4176 KB |
Output is correct |
21 |
Correct |
4 ms |
4176 KB |
Output is correct |
22 |
Correct |
4 ms |
4176 KB |
Output is correct |
23 |
Correct |
4 ms |
4176 KB |
Output is correct |
24 |
Correct |
5 ms |
4176 KB |
Output is correct |
25 |
Correct |
5 ms |
4280 KB |
Output is correct |
26 |
Correct |
4 ms |
4280 KB |
Output is correct |
27 |
Correct |
4 ms |
4280 KB |
Output is correct |
28 |
Correct |
5 ms |
4336 KB |
Output is correct |
29 |
Correct |
4 ms |
4336 KB |
Output is correct |
30 |
Correct |
4 ms |
4336 KB |
Output is correct |
31 |
Correct |
4 ms |
4336 KB |
Output is correct |
32 |
Correct |
4 ms |
4336 KB |
Output is correct |
33 |
Correct |
5 ms |
4336 KB |
Output is correct |
34 |
Correct |
5 ms |
4336 KB |
Output is correct |
35 |
Correct |
36 ms |
6576 KB |
Output is correct |
36 |
Correct |
7 ms |
6576 KB |
Output is correct |
37 |
Correct |
28 ms |
7604 KB |
Output is correct |
38 |
Correct |
34 ms |
8020 KB |
Output is correct |
39 |
Correct |
33 ms |
8284 KB |
Output is correct |
40 |
Correct |
33 ms |
8548 KB |
Output is correct |
41 |
Correct |
33 ms |
8812 KB |
Output is correct |
42 |
Correct |
9 ms |
8812 KB |
Output is correct |
43 |
Correct |
9 ms |
8812 KB |
Output is correct |
44 |
Correct |
9 ms |
8812 KB |
Output is correct |
45 |
Correct |
23 ms |
8812 KB |
Output is correct |
46 |
Correct |
23 ms |
8812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8812 KB |
Output is correct |
2 |
Correct |
4 ms |
8812 KB |
Output is correct |
3 |
Correct |
4 ms |
8812 KB |
Output is correct |
4 |
Correct |
4 ms |
8812 KB |
Output is correct |
5 |
Correct |
4 ms |
8812 KB |
Output is correct |
6 |
Correct |
4 ms |
8812 KB |
Output is correct |
7 |
Correct |
4 ms |
8812 KB |
Output is correct |
8 |
Correct |
4 ms |
8812 KB |
Output is correct |
9 |
Correct |
4 ms |
8812 KB |
Output is correct |
10 |
Correct |
4 ms |
8812 KB |
Output is correct |
11 |
Correct |
5 ms |
8812 KB |
Output is correct |
12 |
Correct |
4 ms |
8812 KB |
Output is correct |
13 |
Correct |
4 ms |
8812 KB |
Output is correct |
14 |
Correct |
5 ms |
8812 KB |
Output is correct |
15 |
Correct |
5 ms |
8812 KB |
Output is correct |
16 |
Correct |
4 ms |
8812 KB |
Output is correct |
17 |
Correct |
6 ms |
8812 KB |
Output is correct |
18 |
Correct |
4 ms |
8812 KB |
Output is correct |
19 |
Correct |
4 ms |
8812 KB |
Output is correct |
20 |
Correct |
5 ms |
8812 KB |
Output is correct |
21 |
Correct |
4 ms |
8812 KB |
Output is correct |
22 |
Correct |
4 ms |
8812 KB |
Output is correct |
23 |
Correct |
6 ms |
8812 KB |
Output is correct |
24 |
Correct |
6 ms |
8812 KB |
Output is correct |
25 |
Correct |
5 ms |
8812 KB |
Output is correct |
26 |
Correct |
5 ms |
8812 KB |
Output is correct |
27 |
Correct |
4 ms |
8812 KB |
Output is correct |
28 |
Correct |
9 ms |
8812 KB |
Output is correct |
29 |
Correct |
4 ms |
8812 KB |
Output is correct |
30 |
Correct |
4 ms |
8812 KB |
Output is correct |
31 |
Correct |
5 ms |
8812 KB |
Output is correct |
32 |
Correct |
6 ms |
8812 KB |
Output is correct |
33 |
Correct |
7 ms |
8812 KB |
Output is correct |
34 |
Correct |
5 ms |
8812 KB |
Output is correct |
35 |
Correct |
27 ms |
9460 KB |
Output is correct |
36 |
Correct |
7 ms |
9460 KB |
Output is correct |
37 |
Correct |
34 ms |
10604 KB |
Output is correct |
38 |
Correct |
34 ms |
11004 KB |
Output is correct |
39 |
Correct |
35 ms |
11292 KB |
Output is correct |
40 |
Correct |
39 ms |
11448 KB |
Output is correct |
41 |
Correct |
39 ms |
11712 KB |
Output is correct |
42 |
Correct |
9 ms |
11712 KB |
Output is correct |
43 |
Correct |
11 ms |
11712 KB |
Output is correct |
44 |
Correct |
10 ms |
11712 KB |
Output is correct |
45 |
Correct |
23 ms |
11712 KB |
Output is correct |
46 |
Correct |
24 ms |
11712 KB |
Output is correct |
47 |
Correct |
65 ms |
18840 KB |
Output is correct |
48 |
Correct |
22 ms |
18840 KB |
Output is correct |
49 |
Correct |
17 ms |
18840 KB |
Output is correct |
50 |
Correct |
14 ms |
18840 KB |
Output is correct |
51 |
Correct |
69 ms |
18840 KB |
Output is correct |
52 |
Correct |
50 ms |
18840 KB |
Output is correct |
53 |
Correct |
23 ms |
18840 KB |
Output is correct |
54 |
Correct |
4 ms |
18840 KB |
Output is correct |
55 |
Correct |
4 ms |
18840 KB |
Output is correct |
56 |
Correct |
25 ms |
18840 KB |
Output is correct |
57 |
Correct |
13 ms |
18840 KB |
Output is correct |
58 |
Correct |
9 ms |
18840 KB |
Output is correct |
59 |
Correct |
10 ms |
18840 KB |
Output is correct |
60 |
Correct |
10 ms |
18840 KB |
Output is correct |
61 |
Correct |
11 ms |
18840 KB |
Output is correct |
62 |
Correct |
33 ms |
19608 KB |
Output is correct |
63 |
Correct |
30 ms |
19608 KB |
Output is correct |
64 |
Correct |
28 ms |
19608 KB |
Output is correct |
65 |
Correct |
38 ms |
19608 KB |
Output is correct |
66 |
Correct |
48 ms |
19608 KB |
Output is correct |
67 |
Correct |
43 ms |
19608 KB |
Output is correct |