#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define sz(v) (int)(v).size()
#define all(v) begin(v),end(v)
#define each(e,v) for(auto &e:(v))
#define pb push_back
#define f first
#define s second
typedef long long ll;
const ll INF = 1e18;
template<class T> bool ckmin(T&a, const T&b) {
return b < a ? a = b, 1 : 0;
}
const int MX = 3e4+5;
int N,M, st,p,en;
set<int> jmp[MX];
vector<pair<ll,int>> g[MX];
ll d[MX];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
void solve() {
cin>>N>>M;
cin>>st>>p;
jmp[st].insert(p);
cin>>en>>p;
FOR(i,2,M) {
int b;
cin>>b>>p;
jmp[b].insert(p);
}
F0R(i,N) {
each(p,jmp[i]) {
for(int j = 1; ; j++) {
int pos = i+j*p;
if(pos<0 || pos>=N) break;
g[i].pb({pos,j});
if(jmp[pos].count(p)) break;
}
}
}
F0R(i,N) {
each(p,jmp[i]) {
for(int j = 1;; j++) {
int pos = i-j*p;
if(pos<0 || pos>=N) break;
g[i].pb({pos,j});
if(jmp[pos].count(p)) break;
}
}
}
memset(d,0x3f,sizeof d);
pq.push({d[st] = 0, st});
while(sz(pq)) {
int u = pq.top().s; ll du = pq.top().f;
pq.pop();
if(du > d[u]) continue;
F0R(i,sz(g[u])) {
int v = g[u][i].f; ll dv = du + g[u][i].s;
if(ckmin(d[v],dv)) {
pq.push({d[v],v});
}
}
}
if(d[en] >= INF) {
cout << "-1\n";
}
else cout << d[en] << "\n";
}
int main() {
int t=1;
//cin>>t;
while(t-->0) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
3 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2764 KB |
Output is correct |
15 |
Correct |
3 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
3020 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2764 KB |
Output is correct |
21 |
Correct |
3 ms |
2636 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
4 ms |
3008 KB |
Output is correct |
25 |
Correct |
3 ms |
2892 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
3 ms |
2764 KB |
Output is correct |
28 |
Correct |
3 ms |
3148 KB |
Output is correct |
29 |
Correct |
5 ms |
3916 KB |
Output is correct |
30 |
Correct |
3 ms |
3020 KB |
Output is correct |
31 |
Correct |
4 ms |
3404 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
10 ms |
5072 KB |
Output is correct |
34 |
Correct |
8 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2536 KB |
Output is correct |
3 |
Correct |
1 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
3 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2764 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
3020 KB |
Output is correct |
18 |
Correct |
4 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
3 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2764 KB |
Output is correct |
23 |
Correct |
3 ms |
2764 KB |
Output is correct |
24 |
Correct |
3 ms |
2892 KB |
Output is correct |
25 |
Correct |
3 ms |
2892 KB |
Output is correct |
26 |
Correct |
4 ms |
2764 KB |
Output is correct |
27 |
Correct |
3 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
3148 KB |
Output is correct |
29 |
Correct |
5 ms |
3916 KB |
Output is correct |
30 |
Correct |
2 ms |
3020 KB |
Output is correct |
31 |
Correct |
3 ms |
3404 KB |
Output is correct |
32 |
Correct |
2 ms |
3148 KB |
Output is correct |
33 |
Correct |
8 ms |
5072 KB |
Output is correct |
34 |
Correct |
7 ms |
5072 KB |
Output is correct |
35 |
Correct |
30 ms |
5804 KB |
Output is correct |
36 |
Correct |
5 ms |
3160 KB |
Output is correct |
37 |
Correct |
28 ms |
7504 KB |
Output is correct |
38 |
Correct |
35 ms |
7512 KB |
Output is correct |
39 |
Correct |
31 ms |
7600 KB |
Output is correct |
40 |
Correct |
29 ms |
7420 KB |
Output is correct |
41 |
Correct |
27 ms |
7504 KB |
Output is correct |
42 |
Correct |
13 ms |
2776 KB |
Output is correct |
43 |
Correct |
17 ms |
2692 KB |
Output is correct |
44 |
Correct |
13 ms |
2732 KB |
Output is correct |
45 |
Correct |
32 ms |
12168 KB |
Output is correct |
46 |
Correct |
33 ms |
12104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
1 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2764 KB |
Output is correct |
15 |
Correct |
2 ms |
2776 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
3 ms |
3020 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
4 ms |
2764 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
2 ms |
2776 KB |
Output is correct |
23 |
Correct |
2 ms |
2776 KB |
Output is correct |
24 |
Correct |
3 ms |
2904 KB |
Output is correct |
25 |
Correct |
3 ms |
2776 KB |
Output is correct |
26 |
Correct |
3 ms |
2776 KB |
Output is correct |
27 |
Correct |
2 ms |
2776 KB |
Output is correct |
28 |
Correct |
3 ms |
3160 KB |
Output is correct |
29 |
Correct |
6 ms |
3928 KB |
Output is correct |
30 |
Correct |
3 ms |
3160 KB |
Output is correct |
31 |
Correct |
3 ms |
3404 KB |
Output is correct |
32 |
Correct |
3 ms |
3056 KB |
Output is correct |
33 |
Correct |
7 ms |
5072 KB |
Output is correct |
34 |
Correct |
9 ms |
5216 KB |
Output is correct |
35 |
Correct |
22 ms |
5964 KB |
Output is correct |
36 |
Correct |
5 ms |
3196 KB |
Output is correct |
37 |
Correct |
23 ms |
7708 KB |
Output is correct |
38 |
Correct |
28 ms |
7532 KB |
Output is correct |
39 |
Correct |
33 ms |
7620 KB |
Output is correct |
40 |
Correct |
28 ms |
7628 KB |
Output is correct |
41 |
Correct |
30 ms |
7608 KB |
Output is correct |
42 |
Correct |
18 ms |
2892 KB |
Output is correct |
43 |
Correct |
13 ms |
2808 KB |
Output is correct |
44 |
Correct |
13 ms |
2892 KB |
Output is correct |
45 |
Correct |
32 ms |
12220 KB |
Output is correct |
46 |
Correct |
32 ms |
12224 KB |
Output is correct |
47 |
Correct |
53 ms |
21112 KB |
Output is correct |
48 |
Correct |
25 ms |
8128 KB |
Output is correct |
49 |
Correct |
22 ms |
6400 KB |
Output is correct |
50 |
Correct |
16 ms |
6188 KB |
Output is correct |
51 |
Correct |
65 ms |
11040 KB |
Output is correct |
52 |
Correct |
51 ms |
11780 KB |
Output is correct |
53 |
Correct |
24 ms |
5320 KB |
Output is correct |
54 |
Correct |
3 ms |
3052 KB |
Output is correct |
55 |
Correct |
4 ms |
3660 KB |
Output is correct |
56 |
Correct |
22 ms |
5588 KB |
Output is correct |
57 |
Correct |
42 ms |
19596 KB |
Output is correct |
58 |
Correct |
14 ms |
4164 KB |
Output is correct |
59 |
Correct |
19 ms |
5188 KB |
Output is correct |
60 |
Correct |
22 ms |
6000 KB |
Output is correct |
61 |
Correct |
21 ms |
4980 KB |
Output is correct |
62 |
Correct |
33 ms |
13392 KB |
Output is correct |
63 |
Correct |
88 ms |
43820 KB |
Output is correct |
64 |
Correct |
96 ms |
51608 KB |
Output is correct |
65 |
Correct |
130 ms |
70472 KB |
Output is correct |
66 |
Correct |
261 ms |
123792 KB |
Output is correct |
67 |
Correct |
222 ms |
123736 KB |
Output is correct |