#include<bits/stdc++.h>
using namespace std;
struct dsu{
vector<int>parent,sz;
void build(int n){
parent.resize(n);
sz.resize(n);
for (int i = 0;i<n;++i){
parent[i] = i;
sz[i] = 1;
}
}
int findset(int v){
if (v == parent[v])return v;
parent[v] = findset(parent[v]);
return parent[v];
}
bool unionset(int a,int b){
int u = findset(a);
int v = findset(b);
if (u == v) return false;
if (sz[v]>sz[u])swap(u,v);
parent[v] = u;
sz[u]+=sz[v];
return true;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
vector<pair<int,int>>arr(m);
vector<set<int>>brr(n);
for (int i = 0;i<m;++i){
cin>>arr[i].first>>arr[i].second;
brr[arr[i].first].insert(arr[i].second);
}
queue<int>q;
q.push(arr[0].first);
vector<int>dist(n,(int)1e9);
dist[arr[0].first] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
for (auto x:brr[u]){
for (int k = 1;u - k*x>=0;++k){
if (dist[u - k*x] > dist[u] + k){
dist[u - k*x] = dist[u] + k;
q.push(u - k*x);
}
}
for (int k = 1;u + k*x<n;++k){
if (dist[u + k*x] > dist[u] + k){
dist[u + k*x] = dist[u] + k;
q.push(u + k*x);
}
}
}
}
if (dist[arr[1].first]==1e9){
dist[arr[1].first]=-1;
}
cout<<dist[arr[1].first]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
480 KB |
Output is correct |
18 |
Correct |
1 ms |
456 KB |
Output is correct |
19 |
Correct |
1 ms |
448 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
448 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
452 KB |
Output is correct |
20 |
Correct |
5 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
444 KB |
Output is correct |
23 |
Correct |
1 ms |
452 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
456 KB |
Output is correct |
30 |
Correct |
1 ms |
452 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
2 ms |
584 KB |
Output is correct |
35 |
Correct |
12 ms |
2004 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
11 ms |
1620 KB |
Output is correct |
38 |
Correct |
15 ms |
2252 KB |
Output is correct |
39 |
Correct |
17 ms |
2248 KB |
Output is correct |
40 |
Correct |
17 ms |
2368 KB |
Output is correct |
41 |
Correct |
16 ms |
2260 KB |
Output is correct |
42 |
Correct |
4 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
852 KB |
Output is correct |
44 |
Correct |
8 ms |
936 KB |
Output is correct |
45 |
Correct |
23 ms |
2308 KB |
Output is correct |
46 |
Correct |
19 ms |
2348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
456 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
452 KB |
Output is correct |
30 |
Correct |
1 ms |
444 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
11 ms |
2036 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
11 ms |
1632 KB |
Output is correct |
38 |
Correct |
16 ms |
2260 KB |
Output is correct |
39 |
Correct |
16 ms |
2244 KB |
Output is correct |
40 |
Correct |
25 ms |
2260 KB |
Output is correct |
41 |
Correct |
19 ms |
2256 KB |
Output is correct |
42 |
Correct |
5 ms |
876 KB |
Output is correct |
43 |
Correct |
5 ms |
844 KB |
Output is correct |
44 |
Correct |
8 ms |
904 KB |
Output is correct |
45 |
Correct |
19 ms |
2292 KB |
Output is correct |
46 |
Correct |
16 ms |
2260 KB |
Output is correct |
47 |
Correct |
22 ms |
2756 KB |
Output is correct |
48 |
Correct |
7 ms |
2900 KB |
Output is correct |
49 |
Correct |
6 ms |
2900 KB |
Output is correct |
50 |
Correct |
6 ms |
2632 KB |
Output is correct |
51 |
Correct |
23 ms |
4172 KB |
Output is correct |
52 |
Correct |
27 ms |
4180 KB |
Output is correct |
53 |
Correct |
16 ms |
3916 KB |
Output is correct |
54 |
Correct |
2 ms |
1876 KB |
Output is correct |
55 |
Correct |
1 ms |
1876 KB |
Output is correct |
56 |
Correct |
730 ms |
3844 KB |
Output is correct |
57 |
Correct |
71 ms |
2800 KB |
Output is correct |
58 |
Correct |
6 ms |
2260 KB |
Output is correct |
59 |
Correct |
6 ms |
2376 KB |
Output is correct |
60 |
Correct |
6 ms |
2320 KB |
Output is correct |
61 |
Correct |
7 ms |
2516 KB |
Output is correct |
62 |
Correct |
12 ms |
3776 KB |
Output is correct |
63 |
Correct |
19 ms |
4436 KB |
Output is correct |
64 |
Correct |
18 ms |
4436 KB |
Output is correct |
65 |
Correct |
29 ms |
4484 KB |
Output is correct |
66 |
Correct |
65 ms |
5460 KB |
Output is correct |
67 |
Correct |
54 ms |
5440 KB |
Output is correct |