#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<vii> vvii;
int main(){
int l,n;
cin >> l >> n;
vii v(n);
for (int i=0;i<n;i++) cin >> v[i].first >> v[i].second;
int inicio=v[0].first,final=v[1].first;
vector<set<int>> pos(l);
for (int i=0;i<n;i++) pos[v[i].first].insert(v[i].second);
vvii conexiones(l);
for (int i=0;i<l;i++){
for (int p:pos[i]){
int a=i-p;
while (a>=0){
if (pos[a].size()) conexiones[i].push_back(pii{(i-a)/p,a});
if (pos[a].count(p)) break;
a-=p;
}
a=i+p;
while (a<l){
if (pos[a].size()) conexiones[i].push_back(pii{(a-i)/p,a});
if (pos[a].count(p)) break;
a+=p;
}
}
}
priority_queue<pii> cola;
cola.push(pii{0,inicio});
bool T=true;
vi porvisitar(l,true);
//for (int i=0;i<n;i++){
// for (pii x:conexiones[i]) cerr << x.second<<' '<<x.first<<' '<<' ';
// cerr<<'\n';
//}
while (cola.size()){
pii x=cola.top();
cola.pop();
if (porvisitar[x.second]){
porvisitar[x.second]=false;
if (x.second==final){
T=false;
cout << -x.first << '\n';
break;
}
for (pii y:conexiones[x.second]){
if (porvisitar[y.second]) cola.push(pii{x.first-y.first,y.second});
}
}
}
if (T) cout << -1<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
308 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
556 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
424 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
420 KB |
Output is correct |
33 |
Correct |
3 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
352 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
692 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
3 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
420 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
3 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
556 KB |
Output is correct |
35 |
Correct |
27 ms |
3536 KB |
Output is correct |
36 |
Correct |
5 ms |
844 KB |
Output is correct |
37 |
Correct |
26 ms |
4144 KB |
Output is correct |
38 |
Correct |
37 ms |
5112 KB |
Output is correct |
39 |
Correct |
32 ms |
3992 KB |
Output is correct |
40 |
Correct |
36 ms |
4292 KB |
Output is correct |
41 |
Correct |
44 ms |
5104 KB |
Output is correct |
42 |
Correct |
15 ms |
844 KB |
Output is correct |
43 |
Correct |
14 ms |
844 KB |
Output is correct |
44 |
Correct |
14 ms |
944 KB |
Output is correct |
45 |
Correct |
27 ms |
2568 KB |
Output is correct |
46 |
Correct |
24 ms |
2628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
476 KB |
Output is correct |
12 |
Correct |
2 ms |
304 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
716 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
464 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
3 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
424 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
3 ms |
560 KB |
Output is correct |
35 |
Correct |
27 ms |
3540 KB |
Output is correct |
36 |
Correct |
5 ms |
844 KB |
Output is correct |
37 |
Correct |
26 ms |
4160 KB |
Output is correct |
38 |
Correct |
38 ms |
5072 KB |
Output is correct |
39 |
Correct |
33 ms |
4000 KB |
Output is correct |
40 |
Correct |
40 ms |
4288 KB |
Output is correct |
41 |
Correct |
36 ms |
5060 KB |
Output is correct |
42 |
Correct |
14 ms |
844 KB |
Output is correct |
43 |
Correct |
14 ms |
844 KB |
Output is correct |
44 |
Correct |
16 ms |
1004 KB |
Output is correct |
45 |
Correct |
25 ms |
2636 KB |
Output is correct |
46 |
Correct |
29 ms |
2620 KB |
Output is correct |
47 |
Correct |
56 ms |
11216 KB |
Output is correct |
48 |
Correct |
25 ms |
4940 KB |
Output is correct |
49 |
Correct |
21 ms |
4428 KB |
Output is correct |
50 |
Correct |
20 ms |
3916 KB |
Output is correct |
51 |
Correct |
40 ms |
7648 KB |
Output is correct |
52 |
Correct |
43 ms |
8080 KB |
Output is correct |
53 |
Correct |
26 ms |
5192 KB |
Output is correct |
54 |
Correct |
2 ms |
2508 KB |
Output is correct |
55 |
Correct |
2 ms |
2508 KB |
Output is correct |
56 |
Correct |
23 ms |
5276 KB |
Output is correct |
57 |
Correct |
8 ms |
3280 KB |
Output is correct |
58 |
Correct |
13 ms |
2832 KB |
Output is correct |
59 |
Correct |
17 ms |
2948 KB |
Output is correct |
60 |
Correct |
16 ms |
2920 KB |
Output is correct |
61 |
Correct |
18 ms |
2968 KB |
Output is correct |
62 |
Correct |
37 ms |
9324 KB |
Output is correct |
63 |
Correct |
31 ms |
4376 KB |
Output is correct |
64 |
Correct |
34 ms |
4292 KB |
Output is correct |
65 |
Correct |
38 ms |
4332 KB |
Output is correct |
66 |
Correct |
52 ms |
4420 KB |
Output is correct |
67 |
Correct |
47 ms |
4676 KB |
Output is correct |