/*input
5 3
0 2
1 1
4 1
*/
#include<bits/stdc++.h>
using namespace std;
const int N=3e5 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define FOR(i, n) for(int i=1;i<=n;i++)
#define TRACE(x) cerr << #x << " = " << x << endl
//Trace prints the name of the variable and the value.
set<int> doges[N];
int src, tar, dist[N];
priority_queue< pii, vector< pii >, greater<pii> > pq;
set<int> visited[N];
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n, m;cin>>n>>m;
for(int i=0;i<n;i++) dist[i]=inf;
for(int i=0;i<m;i++)
{
int b, p;cin>>b>>p;
if(i==0) src=b;
if(i==1) tar=b;
doges[b].insert(p);
}
pq.push(mp(0, src));dist[src]=0;
while(!pq.empty())
{
pii cur=pq.top();pq.pop();
if(cur.f>dist[cur.s]) continue;
for(auto i:doges[cur.s])
{
int moves=0;
for(int j=cur.s+i;j<n;j+=i)
{
moves++;
if(dist[j]>cur.f + moves)
{
dist[j]=cur.f + moves;pq.push(mp(dist[j], j));
}
}
moves=0;
for(int j=cur.s-i;j>=0;j-=i)
{
moves++;
if(dist[j]>cur.f + moves)
{
dist[j]=cur.f + moves;pq.push(mp(dist[j], j));
}
}
}
}
if(dist[tar]==inf) cout<<-1;
else cout<<dist[tar];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28416 KB |
Output is correct |
2 |
Correct |
19 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
20 ms |
28544 KB |
Output is correct |
5 |
Correct |
19 ms |
28544 KB |
Output is correct |
6 |
Correct |
19 ms |
28544 KB |
Output is correct |
7 |
Correct |
19 ms |
28544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
28544 KB |
Output is correct |
2 |
Correct |
19 ms |
28672 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
19 ms |
28544 KB |
Output is correct |
5 |
Correct |
19 ms |
28544 KB |
Output is correct |
6 |
Correct |
20 ms |
28544 KB |
Output is correct |
7 |
Correct |
21 ms |
28672 KB |
Output is correct |
8 |
Correct |
19 ms |
28544 KB |
Output is correct |
9 |
Correct |
21 ms |
28544 KB |
Output is correct |
10 |
Correct |
20 ms |
28544 KB |
Output is correct |
11 |
Correct |
22 ms |
28672 KB |
Output is correct |
12 |
Correct |
19 ms |
28544 KB |
Output is correct |
13 |
Correct |
21 ms |
28544 KB |
Output is correct |
14 |
Correct |
21 ms |
28672 KB |
Output is correct |
15 |
Correct |
20 ms |
28672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
28544 KB |
Output is correct |
2 |
Correct |
19 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
21 ms |
28544 KB |
Output is correct |
5 |
Correct |
20 ms |
28544 KB |
Output is correct |
6 |
Correct |
19 ms |
28544 KB |
Output is correct |
7 |
Correct |
25 ms |
28672 KB |
Output is correct |
8 |
Correct |
20 ms |
28544 KB |
Output is correct |
9 |
Correct |
20 ms |
28544 KB |
Output is correct |
10 |
Correct |
20 ms |
28544 KB |
Output is correct |
11 |
Correct |
21 ms |
28800 KB |
Output is correct |
12 |
Correct |
20 ms |
28544 KB |
Output is correct |
13 |
Correct |
22 ms |
28544 KB |
Output is correct |
14 |
Correct |
20 ms |
28672 KB |
Output is correct |
15 |
Correct |
21 ms |
28672 KB |
Output is correct |
16 |
Correct |
21 ms |
28584 KB |
Output is correct |
17 |
Correct |
20 ms |
28672 KB |
Output is correct |
18 |
Correct |
20 ms |
28544 KB |
Output is correct |
19 |
Correct |
19 ms |
28544 KB |
Output is correct |
20 |
Correct |
23 ms |
28672 KB |
Output is correct |
21 |
Correct |
26 ms |
28544 KB |
Output is correct |
22 |
Correct |
19 ms |
28544 KB |
Output is correct |
23 |
Correct |
21 ms |
28672 KB |
Output is correct |
24 |
Correct |
21 ms |
28672 KB |
Output is correct |
25 |
Correct |
21 ms |
28800 KB |
Output is correct |
26 |
Correct |
20 ms |
28544 KB |
Output is correct |
27 |
Correct |
20 ms |
28672 KB |
Output is correct |
28 |
Correct |
20 ms |
28672 KB |
Output is correct |
29 |
Correct |
22 ms |
29004 KB |
Output is correct |
30 |
Correct |
20 ms |
28800 KB |
Output is correct |
31 |
Correct |
21 ms |
28800 KB |
Output is correct |
32 |
Correct |
20 ms |
28800 KB |
Output is correct |
33 |
Correct |
27 ms |
29304 KB |
Output is correct |
34 |
Correct |
27 ms |
29304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28544 KB |
Output is correct |
2 |
Correct |
21 ms |
28544 KB |
Output is correct |
3 |
Correct |
22 ms |
28544 KB |
Output is correct |
4 |
Correct |
22 ms |
28544 KB |
Output is correct |
5 |
Correct |
20 ms |
28544 KB |
Output is correct |
6 |
Correct |
21 ms |
28544 KB |
Output is correct |
7 |
Correct |
21 ms |
28544 KB |
Output is correct |
8 |
Correct |
20 ms |
28544 KB |
Output is correct |
9 |
Correct |
21 ms |
28544 KB |
Output is correct |
10 |
Correct |
21 ms |
28544 KB |
Output is correct |
11 |
Correct |
20 ms |
28672 KB |
Output is correct |
12 |
Correct |
23 ms |
28496 KB |
Output is correct |
13 |
Correct |
20 ms |
28544 KB |
Output is correct |
14 |
Correct |
21 ms |
28672 KB |
Output is correct |
15 |
Correct |
21 ms |
28672 KB |
Output is correct |
16 |
Correct |
20 ms |
28544 KB |
Output is correct |
17 |
Correct |
21 ms |
28672 KB |
Output is correct |
18 |
Correct |
20 ms |
28672 KB |
Output is correct |
19 |
Correct |
20 ms |
28544 KB |
Output is correct |
20 |
Correct |
26 ms |
28672 KB |
Output is correct |
21 |
Correct |
20 ms |
28544 KB |
Output is correct |
22 |
Correct |
22 ms |
28544 KB |
Output is correct |
23 |
Correct |
20 ms |
28672 KB |
Output is correct |
24 |
Correct |
21 ms |
28672 KB |
Output is correct |
25 |
Correct |
21 ms |
28800 KB |
Output is correct |
26 |
Correct |
23 ms |
28544 KB |
Output is correct |
27 |
Correct |
20 ms |
28672 KB |
Output is correct |
28 |
Correct |
21 ms |
28672 KB |
Output is correct |
29 |
Correct |
22 ms |
28928 KB |
Output is correct |
30 |
Correct |
21 ms |
28800 KB |
Output is correct |
31 |
Correct |
20 ms |
28800 KB |
Output is correct |
32 |
Correct |
25 ms |
28800 KB |
Output is correct |
33 |
Correct |
23 ms |
29304 KB |
Output is correct |
34 |
Correct |
25 ms |
29304 KB |
Output is correct |
35 |
Correct |
31 ms |
30080 KB |
Output is correct |
36 |
Correct |
22 ms |
28800 KB |
Output is correct |
37 |
Correct |
29 ms |
29944 KB |
Output is correct |
38 |
Correct |
33 ms |
30464 KB |
Output is correct |
39 |
Correct |
41 ms |
30456 KB |
Output is correct |
40 |
Correct |
33 ms |
30464 KB |
Output is correct |
41 |
Correct |
33 ms |
30464 KB |
Output is correct |
42 |
Correct |
25 ms |
28800 KB |
Output is correct |
43 |
Correct |
35 ms |
28792 KB |
Output is correct |
44 |
Correct |
31 ms |
28928 KB |
Output is correct |
45 |
Correct |
38 ms |
30836 KB |
Output is correct |
46 |
Correct |
37 ms |
30844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28544 KB |
Output is correct |
2 |
Correct |
19 ms |
28544 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
20 ms |
28544 KB |
Output is correct |
5 |
Correct |
19 ms |
28544 KB |
Output is correct |
6 |
Correct |
19 ms |
28544 KB |
Output is correct |
7 |
Correct |
20 ms |
28544 KB |
Output is correct |
8 |
Correct |
20 ms |
28544 KB |
Output is correct |
9 |
Correct |
20 ms |
28544 KB |
Output is correct |
10 |
Correct |
20 ms |
28544 KB |
Output is correct |
11 |
Correct |
20 ms |
28672 KB |
Output is correct |
12 |
Correct |
19 ms |
28544 KB |
Output is correct |
13 |
Correct |
22 ms |
28800 KB |
Output is correct |
14 |
Correct |
20 ms |
28672 KB |
Output is correct |
15 |
Correct |
21 ms |
28672 KB |
Output is correct |
16 |
Correct |
19 ms |
28544 KB |
Output is correct |
17 |
Correct |
20 ms |
28672 KB |
Output is correct |
18 |
Correct |
20 ms |
28544 KB |
Output is correct |
19 |
Correct |
19 ms |
28544 KB |
Output is correct |
20 |
Correct |
23 ms |
28672 KB |
Output is correct |
21 |
Correct |
20 ms |
28672 KB |
Output is correct |
22 |
Correct |
20 ms |
28544 KB |
Output is correct |
23 |
Correct |
20 ms |
28672 KB |
Output is correct |
24 |
Correct |
21 ms |
28672 KB |
Output is correct |
25 |
Correct |
21 ms |
28800 KB |
Output is correct |
26 |
Correct |
20 ms |
28720 KB |
Output is correct |
27 |
Correct |
21 ms |
28544 KB |
Output is correct |
28 |
Correct |
20 ms |
28672 KB |
Output is correct |
29 |
Correct |
22 ms |
28928 KB |
Output is correct |
30 |
Correct |
20 ms |
28800 KB |
Output is correct |
31 |
Correct |
20 ms |
28800 KB |
Output is correct |
32 |
Correct |
20 ms |
28800 KB |
Output is correct |
33 |
Correct |
23 ms |
29304 KB |
Output is correct |
34 |
Correct |
24 ms |
29304 KB |
Output is correct |
35 |
Correct |
30 ms |
30080 KB |
Output is correct |
36 |
Correct |
22 ms |
28800 KB |
Output is correct |
37 |
Correct |
28 ms |
29824 KB |
Output is correct |
38 |
Correct |
33 ms |
30464 KB |
Output is correct |
39 |
Correct |
34 ms |
30456 KB |
Output is correct |
40 |
Correct |
33 ms |
30464 KB |
Output is correct |
41 |
Correct |
33 ms |
30464 KB |
Output is correct |
42 |
Correct |
25 ms |
28800 KB |
Output is correct |
43 |
Correct |
25 ms |
28792 KB |
Output is correct |
44 |
Correct |
26 ms |
28920 KB |
Output is correct |
45 |
Correct |
36 ms |
30844 KB |
Output is correct |
46 |
Correct |
35 ms |
30844 KB |
Output is correct |
47 |
Correct |
39 ms |
31224 KB |
Output is correct |
48 |
Correct |
28 ms |
29952 KB |
Output is correct |
49 |
Correct |
27 ms |
29952 KB |
Output is correct |
50 |
Correct |
26 ms |
29568 KB |
Output is correct |
51 |
Correct |
51 ms |
32756 KB |
Output is correct |
52 |
Correct |
49 ms |
31728 KB |
Output is correct |
53 |
Correct |
39 ms |
31736 KB |
Output is correct |
54 |
Correct |
21 ms |
29184 KB |
Output is correct |
55 |
Correct |
22 ms |
29184 KB |
Output is correct |
56 |
Correct |
493 ms |
31100 KB |
Output is correct |
57 |
Correct |
73 ms |
33140 KB |
Output is correct |
58 |
Correct |
26 ms |
29312 KB |
Output is correct |
59 |
Correct |
30 ms |
29440 KB |
Output is correct |
60 |
Correct |
28 ms |
29440 KB |
Output is correct |
61 |
Correct |
30 ms |
29568 KB |
Output is correct |
62 |
Correct |
31 ms |
30464 KB |
Output is correct |
63 |
Correct |
64 ms |
34664 KB |
Output is correct |
64 |
Correct |
65 ms |
34672 KB |
Output is correct |
65 |
Correct |
81 ms |
34664 KB |
Output is correct |
66 |
Correct |
131 ms |
38756 KB |
Output is correct |
67 |
Correct |
130 ms |
38764 KB |
Output is correct |