#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
#define nl endl
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<int, 3>
using namespace std;
const int MX=30005, MOD=1000000007, BLOCK=180, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
int N, M, B[MX], P[MX];
int vis[MX][BLOCK+5], vis2[MX];
vector<int> doge[MX];
int main(){
fastio;
cin >> N >> M;
for(int i=0; i<=N; i++){
for(int j=0; j<=BLOCK; j++)
vis[i][j]=INF;
}
for(int i=0; i<M; i++){
cin >> B[i] >> P[i];
doge[B[i]].pb(P[i]);
}
priority_queue<arr3, vector<arr3>, greater<arr3>> PQ;
PQ.push({0, B[0], 0});
vis[B[0]][0]=0;
while(PQ.size()){
int cost=PQ.top()[0], idx=PQ.top()[1], p=PQ.top()[2];
PQ.pop();
if(vis[idx][p]<cost)
continue;
vis[idx][p]=cost;
if(!p){
for(int i:doge[idx]){
if(i<=BLOCK){
if(vis[idx][i]<=cost)
continue;
vis[idx][i]=cost;
PQ.push({cost, idx, i});
}else{
for(int j=1, jj; idx+i*j<N; j++){
jj=idx+i*j;
if(vis[jj][0]>cost+j){
vis[jj][0]=cost+j;
PQ.push({cost+j, jj, 0});
}
}
for(int j=1, jj; idx-i*j>=0; j++){
jj=idx-i*j;
if(vis[jj][0]>cost+j){
vis[jj][0]=cost+j;
PQ.push({cost+j, jj, 0});
}
}
}
}
}else{
for(int j=1, jj; j<=1; j++){
jj=idx+p*j;
if(jj>=N)
continue;
if(vis[jj][0]>cost+j){
vis[jj][0]=cost+j;
PQ.push({cost+j, jj, 0});
}
if(vis[jj][p]>cost+j){
vis[jj][p]=cost+j;
PQ.push({cost+j, jj, p});
}
}
for(int j=1, jj; j<=1; j++){
jj=idx-p*j;
if(jj<0)
continue;
if(vis[jj][0]>cost+j){
vis[jj][0]=cost+j;
PQ.push({cost+j, jj, 0});
}
if(vis[jj][p]>cost+j){
vis[jj][p]=cost+j;
PQ.push({cost+j, jj, p});
}
}
}
}
int ans=vis[B[1]][0];
if(ans==INF)
ans=-1;
cout << ans << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2388 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
9 ms |
2516 KB |
Output is correct |
30 |
Correct |
3 ms |
2388 KB |
Output is correct |
31 |
Correct |
5 ms |
2536 KB |
Output is correct |
32 |
Correct |
4 ms |
2516 KB |
Output is correct |
33 |
Correct |
15 ms |
2612 KB |
Output is correct |
34 |
Correct |
15 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1620 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
3 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2388 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
4 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
4 ms |
2588 KB |
Output is correct |
29 |
Correct |
9 ms |
2548 KB |
Output is correct |
30 |
Correct |
3 ms |
2388 KB |
Output is correct |
31 |
Correct |
5 ms |
2516 KB |
Output is correct |
32 |
Correct |
4 ms |
2516 KB |
Output is correct |
33 |
Correct |
16 ms |
2608 KB |
Output is correct |
34 |
Correct |
16 ms |
2608 KB |
Output is correct |
35 |
Correct |
14 ms |
2644 KB |
Output is correct |
36 |
Correct |
4 ms |
1876 KB |
Output is correct |
37 |
Correct |
22 ms |
2988 KB |
Output is correct |
38 |
Correct |
24 ms |
3028 KB |
Output is correct |
39 |
Correct |
22 ms |
3124 KB |
Output is correct |
40 |
Correct |
21 ms |
3028 KB |
Output is correct |
41 |
Correct |
23 ms |
3028 KB |
Output is correct |
42 |
Correct |
6 ms |
2900 KB |
Output is correct |
43 |
Correct |
5 ms |
2900 KB |
Output is correct |
44 |
Correct |
5 ms |
2900 KB |
Output is correct |
45 |
Correct |
61 ms |
3348 KB |
Output is correct |
46 |
Correct |
67 ms |
3464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1028 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
3 ms |
2260 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2388 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2496 KB |
Output is correct |
29 |
Correct |
8 ms |
2516 KB |
Output is correct |
30 |
Correct |
3 ms |
2508 KB |
Output is correct |
31 |
Correct |
5 ms |
2516 KB |
Output is correct |
32 |
Correct |
5 ms |
2516 KB |
Output is correct |
33 |
Correct |
17 ms |
2516 KB |
Output is correct |
34 |
Correct |
16 ms |
2612 KB |
Output is correct |
35 |
Correct |
19 ms |
2668 KB |
Output is correct |
36 |
Correct |
4 ms |
1876 KB |
Output is correct |
37 |
Correct |
24 ms |
2992 KB |
Output is correct |
38 |
Correct |
21 ms |
3112 KB |
Output is correct |
39 |
Correct |
20 ms |
3128 KB |
Output is correct |
40 |
Correct |
20 ms |
3084 KB |
Output is correct |
41 |
Correct |
25 ms |
3148 KB |
Output is correct |
42 |
Correct |
6 ms |
2976 KB |
Output is correct |
43 |
Correct |
5 ms |
2900 KB |
Output is correct |
44 |
Correct |
6 ms |
2900 KB |
Output is correct |
45 |
Correct |
62 ms |
3416 KB |
Output is correct |
46 |
Correct |
65 ms |
3348 KB |
Output is correct |
47 |
Correct |
89 ms |
11028 KB |
Output is correct |
48 |
Correct |
13 ms |
18388 KB |
Output is correct |
49 |
Correct |
15 ms |
20012 KB |
Output is correct |
50 |
Correct |
13 ms |
21716 KB |
Output is correct |
51 |
Correct |
61 ms |
24084 KB |
Output is correct |
52 |
Correct |
74 ms |
24084 KB |
Output is correct |
53 |
Correct |
26 ms |
23592 KB |
Output is correct |
54 |
Correct |
15 ms |
22652 KB |
Output is correct |
55 |
Correct |
12 ms |
22736 KB |
Output is correct |
56 |
Correct |
19 ms |
23800 KB |
Output is correct |
57 |
Correct |
81 ms |
22888 KB |
Output is correct |
58 |
Correct |
18 ms |
23324 KB |
Output is correct |
59 |
Correct |
23 ms |
23476 KB |
Output is correct |
60 |
Correct |
31 ms |
23336 KB |
Output is correct |
61 |
Correct |
24 ms |
23516 KB |
Output is correct |
62 |
Correct |
45 ms |
24244 KB |
Output is correct |
63 |
Correct |
54 ms |
25040 KB |
Output is correct |
64 |
Correct |
60 ms |
25004 KB |
Output is correct |
65 |
Correct |
359 ms |
25072 KB |
Output is correct |
66 |
Correct |
959 ms |
26568 KB |
Output is correct |
67 |
Correct |
959 ms |
26616 KB |
Output is correct |