/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
void solve()
{
int n,m;
cin>>n>>m;
pair<int,int>a[m];
vector<int>lo[n];
for (auto& i:a)
{
cin>>i.first>>i.second;
lo[i.first].push_back(i.second);
}
int dp[n]={};
for (int i=0;i<n;i++)
dp[i]=1e9+10;
dp[a[0].first]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,a[0].first});
bool vis[n]={};
while (pq.size())
{
int loc=pq.top().second;
pq.pop();
if (vis[loc])
continue;
vis[loc]=1;
for (auto j:lo[loc])
{
int z=0;
for (int k=loc-j;k>=0;k-=j)
{
z++;
if (dp[k]>dp[loc]+z)
{
dp[k]=dp[loc]+z;
pq.push({dp[k],k});
}
}
z=0;
for (int k=loc+j;k<n;k+=j)
{
z++;
if (dp[k]>dp[loc]+z)
{
dp[k]=dp[loc]+z;
pq.push({dp[k],k});
}
}
}
}
cout<<(dp[a[1].first]!=1e9+10?dp[a[1].first]:-1)<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
2 ms |
760 KB |
Output is correct |
32 |
Correct |
2 ms |
600 KB |
Output is correct |
33 |
Correct |
3 ms |
600 KB |
Output is correct |
34 |
Correct |
3 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
752 KB |
Output is correct |
34 |
Correct |
2 ms |
600 KB |
Output is correct |
35 |
Correct |
7 ms |
1012 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
1112 KB |
Output is correct |
39 |
Correct |
7 ms |
860 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
7 ms |
856 KB |
Output is correct |
42 |
Correct |
37 ms |
860 KB |
Output is correct |
43 |
Correct |
40 ms |
856 KB |
Output is correct |
44 |
Correct |
36 ms |
1012 KB |
Output is correct |
45 |
Correct |
6 ms |
932 KB |
Output is correct |
46 |
Correct |
7 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
928 KB |
Output is correct |
35 |
Correct |
5 ms |
860 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
5 ms |
864 KB |
Output is correct |
38 |
Correct |
6 ms |
856 KB |
Output is correct |
39 |
Correct |
9 ms |
868 KB |
Output is correct |
40 |
Correct |
9 ms |
860 KB |
Output is correct |
41 |
Correct |
9 ms |
856 KB |
Output is correct |
42 |
Correct |
40 ms |
972 KB |
Output is correct |
43 |
Correct |
40 ms |
980 KB |
Output is correct |
44 |
Correct |
40 ms |
964 KB |
Output is correct |
45 |
Correct |
5 ms |
1112 KB |
Output is correct |
46 |
Correct |
5 ms |
1004 KB |
Output is correct |
47 |
Correct |
13 ms |
1816 KB |
Output is correct |
48 |
Correct |
7 ms |
1628 KB |
Output is correct |
49 |
Correct |
5 ms |
1628 KB |
Output is correct |
50 |
Correct |
3 ms |
1628 KB |
Output is correct |
51 |
Correct |
20 ms |
3280 KB |
Output is correct |
52 |
Correct |
26 ms |
2776 KB |
Output is correct |
53 |
Correct |
12 ms |
2772 KB |
Output is correct |
54 |
Correct |
2 ms |
1368 KB |
Output is correct |
55 |
Correct |
3 ms |
1520 KB |
Output is correct |
56 |
Correct |
509 ms |
2828 KB |
Output is correct |
57 |
Correct |
34 ms |
3536 KB |
Output is correct |
58 |
Correct |
481 ms |
1896 KB |
Output is correct |
59 |
Correct |
514 ms |
1904 KB |
Output is correct |
60 |
Correct |
508 ms |
1904 KB |
Output is correct |
61 |
Correct |
481 ms |
1908 KB |
Output is correct |
62 |
Correct |
9 ms |
2392 KB |
Output is correct |
63 |
Correct |
26 ms |
2784 KB |
Output is correct |
64 |
Correct |
25 ms |
3792 KB |
Output is correct |
65 |
Correct |
34 ms |
3796 KB |
Output is correct |
66 |
Correct |
42 ms |
3788 KB |
Output is correct |
67 |
Correct |
41 ms |
3788 KB |
Output is correct |