#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];
vector<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];
if(binary_search( pos[ casa[i] ].begin(), pos[ casa[i] ].end(), len[i]) == false) pos[ casa[i] ].push_back(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(binary_search(pos[j].begin(), pos[j].end(), x)) break;
}
for(int j = i - x; j >= 0; j -= x)
{
grafo[i].push_back({j, (i - j)/x});
if(binary_search(pos[j].begin(), pos[j].end(), 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 |
3 ms |
1656 KB |
Output is correct |
2 |
Correct |
3 ms |
1760 KB |
Output is correct |
3 |
Correct |
3 ms |
1796 KB |
Output is correct |
4 |
Correct |
3 ms |
1868 KB |
Output is correct |
5 |
Correct |
2 ms |
1920 KB |
Output is correct |
6 |
Correct |
3 ms |
1940 KB |
Output is correct |
7 |
Correct |
2 ms |
1940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1940 KB |
Output is correct |
2 |
Correct |
3 ms |
1940 KB |
Output is correct |
3 |
Correct |
2 ms |
1940 KB |
Output is correct |
4 |
Correct |
3 ms |
1944 KB |
Output is correct |
5 |
Correct |
2 ms |
1944 KB |
Output is correct |
6 |
Correct |
2 ms |
1944 KB |
Output is correct |
7 |
Correct |
2 ms |
1944 KB |
Output is correct |
8 |
Correct |
2 ms |
2012 KB |
Output is correct |
9 |
Correct |
2 ms |
2028 KB |
Output is correct |
10 |
Correct |
3 ms |
2028 KB |
Output is correct |
11 |
Correct |
4 ms |
2104 KB |
Output is correct |
12 |
Correct |
3 ms |
2104 KB |
Output is correct |
13 |
Correct |
3 ms |
2104 KB |
Output is correct |
14 |
Correct |
3 ms |
2104 KB |
Output is correct |
15 |
Correct |
3 ms |
2104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2104 KB |
Output is correct |
2 |
Correct |
3 ms |
2104 KB |
Output is correct |
3 |
Correct |
3 ms |
2104 KB |
Output is correct |
4 |
Correct |
3 ms |
2104 KB |
Output is correct |
5 |
Correct |
3 ms |
2104 KB |
Output is correct |
6 |
Correct |
2 ms |
2104 KB |
Output is correct |
7 |
Correct |
2 ms |
2104 KB |
Output is correct |
8 |
Correct |
3 ms |
2104 KB |
Output is correct |
9 |
Correct |
2 ms |
2104 KB |
Output is correct |
10 |
Correct |
3 ms |
2104 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
3 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2156 KB |
Output is correct |
14 |
Correct |
3 ms |
2156 KB |
Output is correct |
15 |
Correct |
3 ms |
2156 KB |
Output is correct |
16 |
Correct |
3 ms |
2156 KB |
Output is correct |
17 |
Correct |
23 ms |
2284 KB |
Output is correct |
18 |
Correct |
3 ms |
2284 KB |
Output is correct |
19 |
Correct |
3 ms |
2284 KB |
Output is correct |
20 |
Correct |
4 ms |
2284 KB |
Output is correct |
21 |
Correct |
3 ms |
2284 KB |
Output is correct |
22 |
Correct |
3 ms |
2284 KB |
Output is correct |
23 |
Correct |
10 ms |
2284 KB |
Output is correct |
24 |
Correct |
37 ms |
2284 KB |
Output is correct |
25 |
Correct |
7 ms |
2284 KB |
Output is correct |
26 |
Correct |
86 ms |
35056 KB |
Output is correct |
27 |
Correct |
3 ms |
35056 KB |
Output is correct |
28 |
Correct |
4 ms |
35056 KB |
Output is correct |
29 |
Correct |
5 ms |
35056 KB |
Output is correct |
30 |
Correct |
4 ms |
35056 KB |
Output is correct |
31 |
Correct |
6 ms |
35056 KB |
Output is correct |
32 |
Correct |
5 ms |
35056 KB |
Output is correct |
33 |
Correct |
7 ms |
35056 KB |
Output is correct |
34 |
Correct |
6 ms |
35056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
35056 KB |
Output is correct |
2 |
Correct |
3 ms |
35056 KB |
Output is correct |
3 |
Correct |
2 ms |
35056 KB |
Output is correct |
4 |
Correct |
2 ms |
35056 KB |
Output is correct |
5 |
Correct |
2 ms |
35056 KB |
Output is correct |
6 |
Correct |
3 ms |
35056 KB |
Output is correct |
7 |
Correct |
3 ms |
35056 KB |
Output is correct |
8 |
Correct |
2 ms |
35056 KB |
Output is correct |
9 |
Correct |
2 ms |
35056 KB |
Output is correct |
10 |
Correct |
3 ms |
35056 KB |
Output is correct |
11 |
Correct |
4 ms |
35056 KB |
Output is correct |
12 |
Correct |
4 ms |
35056 KB |
Output is correct |
13 |
Correct |
3 ms |
35056 KB |
Output is correct |
14 |
Correct |
3 ms |
35056 KB |
Output is correct |
15 |
Correct |
3 ms |
35056 KB |
Output is correct |
16 |
Correct |
3 ms |
35056 KB |
Output is correct |
17 |
Correct |
22 ms |
35056 KB |
Output is correct |
18 |
Correct |
3 ms |
35056 KB |
Output is correct |
19 |
Correct |
3 ms |
35056 KB |
Output is correct |
20 |
Correct |
4 ms |
35056 KB |
Output is correct |
21 |
Correct |
3 ms |
35056 KB |
Output is correct |
22 |
Correct |
3 ms |
35056 KB |
Output is correct |
23 |
Correct |
8 ms |
35056 KB |
Output is correct |
24 |
Correct |
35 ms |
35056 KB |
Output is correct |
25 |
Correct |
7 ms |
35056 KB |
Output is correct |
26 |
Correct |
93 ms |
35056 KB |
Output is correct |
27 |
Correct |
4 ms |
35056 KB |
Output is correct |
28 |
Correct |
4 ms |
35056 KB |
Output is correct |
29 |
Correct |
5 ms |
35056 KB |
Output is correct |
30 |
Correct |
4 ms |
35056 KB |
Output is correct |
31 |
Correct |
5 ms |
35056 KB |
Output is correct |
32 |
Correct |
5 ms |
35056 KB |
Output is correct |
33 |
Correct |
7 ms |
35056 KB |
Output is correct |
34 |
Correct |
6 ms |
35056 KB |
Output is correct |
35 |
Correct |
185 ms |
35056 KB |
Output is correct |
36 |
Correct |
36 ms |
35056 KB |
Output is correct |
37 |
Correct |
507 ms |
35056 KB |
Output is correct |
38 |
Correct |
432 ms |
35056 KB |
Output is correct |
39 |
Correct |
472 ms |
35056 KB |
Output is correct |
40 |
Correct |
543 ms |
35056 KB |
Output is correct |
41 |
Correct |
584 ms |
35056 KB |
Output is correct |
42 |
Runtime error |
627 ms |
262144 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
262144 KB |
Output is correct |
2 |
Correct |
2 ms |
262144 KB |
Output is correct |
3 |
Correct |
2 ms |
262144 KB |
Output is correct |
4 |
Correct |
2 ms |
262144 KB |
Output is correct |
5 |
Correct |
2 ms |
262144 KB |
Output is correct |
6 |
Correct |
2 ms |
262144 KB |
Output is correct |
7 |
Correct |
2 ms |
262144 KB |
Output is correct |
8 |
Correct |
3 ms |
262144 KB |
Output is correct |
9 |
Correct |
3 ms |
262144 KB |
Output is correct |
10 |
Correct |
3 ms |
262144 KB |
Output is correct |
11 |
Correct |
3 ms |
262144 KB |
Output is correct |
12 |
Correct |
4 ms |
262144 KB |
Output is correct |
13 |
Correct |
3 ms |
262144 KB |
Output is correct |
14 |
Correct |
3 ms |
262144 KB |
Output is correct |
15 |
Correct |
4 ms |
262144 KB |
Output is correct |
16 |
Correct |
3 ms |
262144 KB |
Output is correct |
17 |
Correct |
26 ms |
262144 KB |
Output is correct |
18 |
Correct |
3 ms |
262144 KB |
Output is correct |
19 |
Correct |
3 ms |
262144 KB |
Output is correct |
20 |
Correct |
5 ms |
262144 KB |
Output is correct |
21 |
Correct |
3 ms |
262144 KB |
Output is correct |
22 |
Correct |
3 ms |
262144 KB |
Output is correct |
23 |
Correct |
8 ms |
262144 KB |
Output is correct |
24 |
Correct |
35 ms |
262144 KB |
Output is correct |
25 |
Correct |
7 ms |
262144 KB |
Output is correct |
26 |
Correct |
89 ms |
262144 KB |
Output is correct |
27 |
Correct |
3 ms |
262144 KB |
Output is correct |
28 |
Correct |
4 ms |
262144 KB |
Output is correct |
29 |
Correct |
6 ms |
262144 KB |
Output is correct |
30 |
Correct |
4 ms |
262144 KB |
Output is correct |
31 |
Correct |
5 ms |
262144 KB |
Output is correct |
32 |
Correct |
5 ms |
262144 KB |
Output is correct |
33 |
Correct |
6 ms |
262144 KB |
Output is correct |
34 |
Correct |
7 ms |
262144 KB |
Output is correct |
35 |
Correct |
191 ms |
262144 KB |
Output is correct |
36 |
Correct |
33 ms |
262144 KB |
Output is correct |
37 |
Correct |
491 ms |
262144 KB |
Output is correct |
38 |
Correct |
437 ms |
262144 KB |
Output is correct |
39 |
Correct |
443 ms |
262144 KB |
Output is correct |
40 |
Correct |
544 ms |
262144 KB |
Output is correct |
41 |
Correct |
558 ms |
262144 KB |
Output is correct |
42 |
Runtime error |
616 ms |
262144 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
43 |
Halted |
0 ms |
0 KB |
- |