#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;
#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 trav(a,x) for (auto& a: x)
#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second
struct triple
{
int a,b,c;
};
bool operator<(const triple &x, const triple &y)
{
return x.a<y.a;
}
int const N=3e4+3;
int const sN=174;
int const M=3e4+3;
int b[M];
int p[M];
int d[N][sN];
bitset<sN> vis[N];
int const INF=1e9;
vi doges[N];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m;
cin>>n>>m;
F0R(i,m)
{
cin>>b[i]>>p[i];
doges[b[i]].pb(i);
}
F0R(i,n)
{
fill(d[i],d[i]+sN,INF);
}
priority_queue<triple> q;
q.push({0,b[0],0});
d[b[0]][0]=0;
while(!q.empty()&&(!vis[b[1]][0]))
{
int pos = q.top().b;
int pw = q.top().c;
q.pop();
if(vis[pos][pw])continue;
vis[pos][pw]=1;
if(pw==0)
{
trav(doge,doges[pos])
{
if(p[doge]>=sN)
{
int nxt=pos+p[doge];
int cnt=1;
while(nxt<n)
{
int cand = d[pos][0]+cnt;
if(cand<d[nxt][0])
{
d[nxt][0]=cand;
q.push({-cand,nxt,0});
}
++cnt;
nxt+=p[doge];
}
nxt=pos-p[doge];
cnt=1;
while(nxt>=0)
{
int cand = d[pos][0]+cnt;
if(cand<d[nxt][0])
{
d[nxt][0]=cand;
q.push({-cand,nxt,0});
}
++cnt;
nxt-=p[doge];
}
}
else
{
if(vis[pos][p[doge]])continue;
d[pos][p[doge]]=d[pos][0];
q.push({1,pos,p[doge]});
}
}
}
else
{
if(!vis[pos][0])
{
d[pos][0]=d[pos][pw];
q.push({1,pos,0});
}
int cand = d[pos][pw]+1;
int nxt = pos+pw;
if(nxt<n && cand<d[nxt][pw])
{
d[nxt][pw]=cand;
q.push({-cand,nxt,pw});
}
nxt = pos-pw;
if(nxt>=0 && cand<d[nxt][pw])
{
d[nxt][pw]=cand;
q.push({-cand,nxt,pw});
}
}
}
if(d[b[1]][0]==INF)
{
cout<<-1<<'\n';
}
else
{
cout<<d[b[1]][0]<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
2 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
944 KB |
Output is correct |
4 |
Correct |
2 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
2 ms |
972 KB |
Output is correct |
4 |
Correct |
2 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
3 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2380 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
2 ms |
2380 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
7 ms |
2380 KB |
Output is correct |
30 |
Correct |
3 ms |
2380 KB |
Output is correct |
31 |
Correct |
5 ms |
2380 KB |
Output is correct |
32 |
Correct |
4 ms |
2380 KB |
Output is correct |
33 |
Correct |
13 ms |
2508 KB |
Output is correct |
34 |
Correct |
9 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
2 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
2 ms |
972 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2380 KB |
Output is correct |
25 |
Correct |
3 ms |
2508 KB |
Output is correct |
26 |
Correct |
2 ms |
2380 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
7 ms |
2380 KB |
Output is correct |
30 |
Correct |
4 ms |
2428 KB |
Output is correct |
31 |
Correct |
5 ms |
2380 KB |
Output is correct |
32 |
Correct |
4 ms |
2380 KB |
Output is correct |
33 |
Correct |
13 ms |
2508 KB |
Output is correct |
34 |
Correct |
9 ms |
2508 KB |
Output is correct |
35 |
Correct |
9 ms |
2508 KB |
Output is correct |
36 |
Correct |
3 ms |
1868 KB |
Output is correct |
37 |
Correct |
7 ms |
2956 KB |
Output is correct |
38 |
Correct |
10 ms |
2892 KB |
Output is correct |
39 |
Correct |
9 ms |
2764 KB |
Output is correct |
40 |
Correct |
11 ms |
2892 KB |
Output is correct |
41 |
Correct |
10 ms |
3020 KB |
Output is correct |
42 |
Correct |
7 ms |
2892 KB |
Output is correct |
43 |
Correct |
8 ms |
3408 KB |
Output is correct |
44 |
Correct |
7 ms |
2892 KB |
Output is correct |
45 |
Correct |
45 ms |
3328 KB |
Output is correct |
46 |
Correct |
24 ms |
3340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1152 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
1228 KB |
Output is correct |
22 |
Correct |
3 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2380 KB |
Output is correct |
25 |
Correct |
3 ms |
2380 KB |
Output is correct |
26 |
Correct |
3 ms |
2380 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
7 ms |
2380 KB |
Output is correct |
30 |
Correct |
3 ms |
2380 KB |
Output is correct |
31 |
Correct |
5 ms |
2380 KB |
Output is correct |
32 |
Correct |
5 ms |
2380 KB |
Output is correct |
33 |
Correct |
13 ms |
2532 KB |
Output is correct |
34 |
Correct |
9 ms |
2508 KB |
Output is correct |
35 |
Correct |
9 ms |
2588 KB |
Output is correct |
36 |
Correct |
3 ms |
1868 KB |
Output is correct |
37 |
Correct |
8 ms |
2892 KB |
Output is correct |
38 |
Correct |
9 ms |
2892 KB |
Output is correct |
39 |
Correct |
9 ms |
2764 KB |
Output is correct |
40 |
Correct |
9 ms |
2836 KB |
Output is correct |
41 |
Correct |
10 ms |
3020 KB |
Output is correct |
42 |
Correct |
7 ms |
2804 KB |
Output is correct |
43 |
Correct |
8 ms |
3404 KB |
Output is correct |
44 |
Correct |
7 ms |
2836 KB |
Output is correct |
45 |
Correct |
44 ms |
3288 KB |
Output is correct |
46 |
Correct |
25 ms |
3308 KB |
Output is correct |
47 |
Correct |
16 ms |
10764 KB |
Output is correct |
48 |
Correct |
17 ms |
17484 KB |
Output is correct |
49 |
Correct |
18 ms |
18832 KB |
Output is correct |
50 |
Correct |
21 ms |
20428 KB |
Output is correct |
51 |
Correct |
31 ms |
23404 KB |
Output is correct |
52 |
Correct |
32 ms |
23484 KB |
Output is correct |
53 |
Correct |
23 ms |
22468 KB |
Output is correct |
54 |
Correct |
15 ms |
22092 KB |
Output is correct |
55 |
Correct |
15 ms |
22144 KB |
Output is correct |
56 |
Correct |
25 ms |
23244 KB |
Output is correct |
57 |
Correct |
14 ms |
21480 KB |
Output is correct |
58 |
Correct |
26 ms |
23084 KB |
Output is correct |
59 |
Correct |
23 ms |
22564 KB |
Output is correct |
60 |
Correct |
23 ms |
22532 KB |
Output is correct |
61 |
Correct |
23 ms |
23056 KB |
Output is correct |
62 |
Correct |
50 ms |
23284 KB |
Output is correct |
63 |
Correct |
63 ms |
23616 KB |
Output is correct |
64 |
Correct |
70 ms |
23624 KB |
Output is correct |
65 |
Correct |
275 ms |
23844 KB |
Output is correct |
66 |
Correct |
820 ms |
24024 KB |
Output is correct |
67 |
Correct |
357 ms |
24232 KB |
Output is correct |