#include <bits/stdc++.h>
#define N 30050
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
int n, m, casa[N], len[N], dist[N];
unordered_set<int> pos[N];
vector<pii> grafo[N];
priority_queue<pii> pq;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>m>>n;
for(int i = 1; i <= n; i++)
{
cin>>casa[i]>>len[i];
pos[ casa[i] ].insert( len[i] );
}
for(int i = 0; i < m; i++)
{
for(auto x: pos[i])
{
for(int j = i + x; j < m; j += x)
{
grafo[i].push_back({j, (j - i)/x});
if(pos[j].count(x)) break;
}
for(int j = i - x; j >= 0; j -= x)
{
grafo[i].push_back({j, (i - j)/x});
if(pos[j].count(x)) break;
}
}
}
for(int i = 0; i < m; i++) dist[i] = 2000000000;
dist[ casa[1] ] = 0;
pq.push({0, casa[1]});
while(!pq.empty())
{
int x = pq.top().s, d = pq.top().f;
pq.pop();
if(d > dist[x]) continue;
for(auto v: grafo[x])
{
if(dist[v.f] > dist[x] + v.s)
{
dist[v.f] = dist[x] + v.s;
pq.push({dist[v.f], v.f});
}
}
}
cout<<(dist[casa[2]] != 2000000000 ? dist[casa[2]] : -1)<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2844 KB |
Output is correct |
4 |
Correct |
4 ms |
2952 KB |
Output is correct |
5 |
Correct |
4 ms |
2952 KB |
Output is correct |
6 |
Correct |
4 ms |
2952 KB |
Output is correct |
7 |
Correct |
4 ms |
2952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2952 KB |
Output is correct |
2 |
Correct |
3 ms |
3076 KB |
Output is correct |
3 |
Correct |
3 ms |
3076 KB |
Output is correct |
4 |
Correct |
4 ms |
3076 KB |
Output is correct |
5 |
Correct |
4 ms |
3076 KB |
Output is correct |
6 |
Correct |
4 ms |
3076 KB |
Output is correct |
7 |
Correct |
3 ms |
3076 KB |
Output is correct |
8 |
Correct |
3 ms |
3132 KB |
Output is correct |
9 |
Correct |
3 ms |
3132 KB |
Output is correct |
10 |
Correct |
4 ms |
3132 KB |
Output is correct |
11 |
Correct |
5 ms |
3216 KB |
Output is correct |
12 |
Correct |
4 ms |
3216 KB |
Output is correct |
13 |
Correct |
4 ms |
3216 KB |
Output is correct |
14 |
Correct |
4 ms |
3252 KB |
Output is correct |
15 |
Correct |
4 ms |
3264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3272 KB |
Output is correct |
2 |
Correct |
3 ms |
3272 KB |
Output is correct |
3 |
Correct |
4 ms |
3272 KB |
Output is correct |
4 |
Correct |
3 ms |
3272 KB |
Output is correct |
5 |
Correct |
3 ms |
3272 KB |
Output is correct |
6 |
Correct |
3 ms |
3272 KB |
Output is correct |
7 |
Correct |
4 ms |
3272 KB |
Output is correct |
8 |
Correct |
4 ms |
3272 KB |
Output is correct |
9 |
Correct |
4 ms |
3272 KB |
Output is correct |
10 |
Correct |
4 ms |
3272 KB |
Output is correct |
11 |
Correct |
6 ms |
3320 KB |
Output is correct |
12 |
Correct |
4 ms |
3320 KB |
Output is correct |
13 |
Correct |
4 ms |
3320 KB |
Output is correct |
14 |
Correct |
5 ms |
3356 KB |
Output is correct |
15 |
Correct |
5 ms |
3368 KB |
Output is correct |
16 |
Correct |
4 ms |
3380 KB |
Output is correct |
17 |
Correct |
23 ms |
3516 KB |
Output is correct |
18 |
Correct |
5 ms |
3516 KB |
Output is correct |
19 |
Correct |
4 ms |
3516 KB |
Output is correct |
20 |
Correct |
5 ms |
3552 KB |
Output is correct |
21 |
Correct |
4 ms |
3552 KB |
Output is correct |
22 |
Correct |
4 ms |
3552 KB |
Output is correct |
23 |
Correct |
9 ms |
3552 KB |
Output is correct |
24 |
Correct |
38 ms |
3600 KB |
Output is correct |
25 |
Correct |
11 ms |
3620 KB |
Output is correct |
26 |
Correct |
5 ms |
3620 KB |
Output is correct |
27 |
Correct |
4 ms |
3620 KB |
Output is correct |
28 |
Correct |
5 ms |
3760 KB |
Output is correct |
29 |
Correct |
7 ms |
4016 KB |
Output is correct |
30 |
Correct |
5 ms |
4016 KB |
Output is correct |
31 |
Correct |
7 ms |
4016 KB |
Output is correct |
32 |
Correct |
6 ms |
4016 KB |
Output is correct |
33 |
Correct |
9 ms |
4656 KB |
Output is correct |
34 |
Correct |
9 ms |
4656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4656 KB |
Output is correct |
2 |
Correct |
3 ms |
4656 KB |
Output is correct |
3 |
Correct |
3 ms |
4656 KB |
Output is correct |
4 |
Correct |
4 ms |
4656 KB |
Output is correct |
5 |
Correct |
4 ms |
4656 KB |
Output is correct |
6 |
Correct |
4 ms |
4656 KB |
Output is correct |
7 |
Correct |
3 ms |
4656 KB |
Output is correct |
8 |
Correct |
3 ms |
4656 KB |
Output is correct |
9 |
Correct |
4 ms |
4656 KB |
Output is correct |
10 |
Correct |
4 ms |
4656 KB |
Output is correct |
11 |
Correct |
4 ms |
4656 KB |
Output is correct |
12 |
Correct |
4 ms |
4656 KB |
Output is correct |
13 |
Correct |
4 ms |
4656 KB |
Output is correct |
14 |
Correct |
4 ms |
4656 KB |
Output is correct |
15 |
Correct |
4 ms |
4656 KB |
Output is correct |
16 |
Correct |
4 ms |
4656 KB |
Output is correct |
17 |
Correct |
24 ms |
4656 KB |
Output is correct |
18 |
Correct |
5 ms |
4656 KB |
Output is correct |
19 |
Correct |
4 ms |
4656 KB |
Output is correct |
20 |
Correct |
5 ms |
4656 KB |
Output is correct |
21 |
Correct |
4 ms |
4656 KB |
Output is correct |
22 |
Correct |
5 ms |
4656 KB |
Output is correct |
23 |
Correct |
9 ms |
4656 KB |
Output is correct |
24 |
Correct |
39 ms |
4656 KB |
Output is correct |
25 |
Correct |
10 ms |
4656 KB |
Output is correct |
26 |
Correct |
5 ms |
4656 KB |
Output is correct |
27 |
Correct |
4 ms |
4656 KB |
Output is correct |
28 |
Correct |
5 ms |
4656 KB |
Output is correct |
29 |
Correct |
6 ms |
4656 KB |
Output is correct |
30 |
Correct |
5 ms |
4656 KB |
Output is correct |
31 |
Correct |
7 ms |
4656 KB |
Output is correct |
32 |
Correct |
7 ms |
4656 KB |
Output is correct |
33 |
Correct |
9 ms |
4680 KB |
Output is correct |
34 |
Correct |
8 ms |
4680 KB |
Output is correct |
35 |
Correct |
185 ms |
5704 KB |
Output is correct |
36 |
Correct |
36 ms |
5704 KB |
Output is correct |
37 |
Correct |
288 ms |
6768 KB |
Output is correct |
38 |
Correct |
328 ms |
7204 KB |
Output is correct |
39 |
Correct |
331 ms |
7500 KB |
Output is correct |
40 |
Correct |
408 ms |
7780 KB |
Output is correct |
41 |
Correct |
385 ms |
8044 KB |
Output is correct |
42 |
Correct |
9 ms |
8044 KB |
Output is correct |
43 |
Correct |
9 ms |
8044 KB |
Output is correct |
44 |
Correct |
9 ms |
8044 KB |
Output is correct |
45 |
Correct |
33 ms |
11492 KB |
Output is correct |
46 |
Correct |
35 ms |
11680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
11680 KB |
Output is correct |
2 |
Correct |
4 ms |
11680 KB |
Output is correct |
3 |
Correct |
4 ms |
11680 KB |
Output is correct |
4 |
Correct |
3 ms |
11680 KB |
Output is correct |
5 |
Correct |
4 ms |
11680 KB |
Output is correct |
6 |
Correct |
3 ms |
11680 KB |
Output is correct |
7 |
Correct |
3 ms |
11680 KB |
Output is correct |
8 |
Correct |
3 ms |
11680 KB |
Output is correct |
9 |
Correct |
3 ms |
11680 KB |
Output is correct |
10 |
Correct |
4 ms |
11680 KB |
Output is correct |
11 |
Correct |
4 ms |
11680 KB |
Output is correct |
12 |
Correct |
4 ms |
11680 KB |
Output is correct |
13 |
Correct |
4 ms |
11680 KB |
Output is correct |
14 |
Correct |
4 ms |
11680 KB |
Output is correct |
15 |
Correct |
4 ms |
11680 KB |
Output is correct |
16 |
Correct |
4 ms |
11680 KB |
Output is correct |
17 |
Correct |
28 ms |
11680 KB |
Output is correct |
18 |
Correct |
4 ms |
11680 KB |
Output is correct |
19 |
Correct |
4 ms |
11680 KB |
Output is correct |
20 |
Correct |
4 ms |
11680 KB |
Output is correct |
21 |
Correct |
5 ms |
11680 KB |
Output is correct |
22 |
Correct |
4 ms |
11680 KB |
Output is correct |
23 |
Correct |
9 ms |
11680 KB |
Output is correct |
24 |
Correct |
41 ms |
11680 KB |
Output is correct |
25 |
Correct |
11 ms |
11680 KB |
Output is correct |
26 |
Correct |
5 ms |
11680 KB |
Output is correct |
27 |
Correct |
4 ms |
11680 KB |
Output is correct |
28 |
Correct |
5 ms |
11680 KB |
Output is correct |
29 |
Correct |
6 ms |
11680 KB |
Output is correct |
30 |
Correct |
5 ms |
11680 KB |
Output is correct |
31 |
Correct |
6 ms |
11680 KB |
Output is correct |
32 |
Correct |
6 ms |
11680 KB |
Output is correct |
33 |
Correct |
12 ms |
11680 KB |
Output is correct |
34 |
Correct |
9 ms |
11680 KB |
Output is correct |
35 |
Correct |
204 ms |
11680 KB |
Output is correct |
36 |
Correct |
33 ms |
11680 KB |
Output is correct |
37 |
Correct |
289 ms |
11680 KB |
Output is correct |
38 |
Correct |
322 ms |
11680 KB |
Output is correct |
39 |
Correct |
312 ms |
11680 KB |
Output is correct |
40 |
Correct |
392 ms |
11680 KB |
Output is correct |
41 |
Correct |
385 ms |
11680 KB |
Output is correct |
42 |
Correct |
9 ms |
11680 KB |
Output is correct |
43 |
Correct |
11 ms |
11680 KB |
Output is correct |
44 |
Correct |
12 ms |
11680 KB |
Output is correct |
45 |
Correct |
32 ms |
13808 KB |
Output is correct |
46 |
Correct |
35 ms |
14032 KB |
Output is correct |
47 |
Execution timed out |
1074 ms |
18596 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |