//Sorry but iam targeting IOI :))
//NEVER LOSE HOPE
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#define endl "\n"
#define ceil(a,b) (a+(b-1))/b
#define all(v) v.begin(),v.end()
#define Hidden ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
const int N=3e4+10,mod = 1e9+7;
int n,m,f[N],dis[N];
pair<int,int>a[N];
vector<pair<int,int>>adj[N];
set<pair<int,int>>vis;
int main(){
cin>>n>>m;
for(int i=0;i<m;++i){
cin>>a[i].first>>a[i].second;
vis.insert({a[i].first,a[i].second});
}
for(auto i:vis){
for(int j=i.first+i.second;j<n;j+=i.second){
adj[i.first].push_back({j,((j-i.first)/i.second)});
if(vis.find({j,i.second})!=vis.end()) break;
}
}
for(auto i:vis){
for(int j=i.first-i.second;j>=0;j-=i.second){
adj[i.first].push_back({j,((i.first-j)/i.second)});
if(vis.find({j,i.second})!=vis.end()) break;
}
}
priority_queue<pair<int,int>>pq;
pq.push({0,a[0].first});
for(int i=0;i<N;++i) dis[i]=INT_MAX;
dis[a[0].first]=0;
while(!pq.empty()){
pair<int,int>p=pq.top();
pq.pop();
int u=p.second;
for(auto i:adj[u]){
int cost=dis[u]+i.second;
if(cost<dis[i.first]){
dis[i.first]=cost;
pq.push({-cost,i.first});
}
}
}
if(dis[a[1].first]==INT_MAX) cout<<"-1\n";
else cout<<dis[a[1].first]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1128 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
3 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1228 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
4 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
3 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
4 ms |
1804 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
2508 KB |
Output is correct |
34 |
Correct |
8 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1064 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1356 KB |
Output is correct |
18 |
Correct |
2 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1228 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
4 ms |
1392 KB |
Output is correct |
25 |
Correct |
5 ms |
1344 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
4 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1444 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
2508 KB |
Output is correct |
34 |
Correct |
7 ms |
2468 KB |
Output is correct |
35 |
Correct |
41 ms |
3516 KB |
Output is correct |
36 |
Correct |
7 ms |
1484 KB |
Output is correct |
37 |
Correct |
50 ms |
4184 KB |
Output is correct |
38 |
Correct |
68 ms |
4444 KB |
Output is correct |
39 |
Correct |
63 ms |
4492 KB |
Output is correct |
40 |
Correct |
60 ms |
4464 KB |
Output is correct |
41 |
Correct |
61 ms |
4492 KB |
Output is correct |
42 |
Correct |
16 ms |
1424 KB |
Output is correct |
43 |
Correct |
19 ms |
1440 KB |
Output is correct |
44 |
Correct |
16 ms |
1508 KB |
Output is correct |
45 |
Correct |
51 ms |
6980 KB |
Output is correct |
46 |
Correct |
50 ms |
6980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
3 ms |
1228 KB |
Output is correct |
15 |
Correct |
3 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
5 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1228 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1228 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
2 ms |
1100 KB |
Output is correct |
23 |
Correct |
3 ms |
1228 KB |
Output is correct |
24 |
Correct |
4 ms |
1356 KB |
Output is correct |
25 |
Correct |
4 ms |
1356 KB |
Output is correct |
26 |
Correct |
3 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
4 ms |
1868 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
3 ms |
1484 KB |
Output is correct |
32 |
Correct |
3 ms |
1356 KB |
Output is correct |
33 |
Correct |
7 ms |
2480 KB |
Output is correct |
34 |
Correct |
8 ms |
2560 KB |
Output is correct |
35 |
Correct |
44 ms |
3464 KB |
Output is correct |
36 |
Correct |
7 ms |
1496 KB |
Output is correct |
37 |
Correct |
51 ms |
4164 KB |
Output is correct |
38 |
Correct |
67 ms |
4548 KB |
Output is correct |
39 |
Correct |
65 ms |
4560 KB |
Output is correct |
40 |
Correct |
62 ms |
4544 KB |
Output is correct |
41 |
Correct |
62 ms |
4416 KB |
Output is correct |
42 |
Correct |
16 ms |
1356 KB |
Output is correct |
43 |
Correct |
16 ms |
1328 KB |
Output is correct |
44 |
Correct |
16 ms |
1420 KB |
Output is correct |
45 |
Correct |
48 ms |
7000 KB |
Output is correct |
46 |
Correct |
49 ms |
7020 KB |
Output is correct |
47 |
Correct |
188 ms |
11592 KB |
Output is correct |
48 |
Correct |
65 ms |
4684 KB |
Output is correct |
49 |
Correct |
49 ms |
3752 KB |
Output is correct |
50 |
Correct |
36 ms |
3308 KB |
Output is correct |
51 |
Correct |
103 ms |
6596 KB |
Output is correct |
52 |
Correct |
107 ms |
7120 KB |
Output is correct |
53 |
Correct |
45 ms |
4292 KB |
Output is correct |
54 |
Correct |
3 ms |
1612 KB |
Output is correct |
55 |
Correct |
4 ms |
1992 KB |
Output is correct |
56 |
Correct |
33 ms |
3660 KB |
Output is correct |
57 |
Correct |
72 ms |
10348 KB |
Output is correct |
58 |
Correct |
18 ms |
2372 KB |
Output is correct |
59 |
Correct |
22 ms |
2708 KB |
Output is correct |
60 |
Correct |
24 ms |
3360 KB |
Output is correct |
61 |
Correct |
25 ms |
2680 KB |
Output is correct |
62 |
Correct |
106 ms |
7752 KB |
Output is correct |
63 |
Correct |
113 ms |
24108 KB |
Output is correct |
64 |
Correct |
119 ms |
27068 KB |
Output is correct |
65 |
Correct |
160 ms |
36920 KB |
Output is correct |
66 |
Correct |
358 ms |
64040 KB |
Output is correct |
67 |
Correct |
318 ms |
64188 KB |
Output is correct |