#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=200;
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 |
1024 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1160 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 |
1164 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1028 KB |
Output is correct |
2 |
Correct |
1 ms |
1028 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 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 |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1156 KB |
Output is correct |
13 |
Correct |
2 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
3 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
1288 KB |
Output is correct |
22 |
Correct |
2 ms |
2380 KB |
Output is correct |
23 |
Correct |
4 ms |
2380 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
3 ms |
2764 KB |
Output is correct |
28 |
Correct |
4 ms |
2732 KB |
Output is correct |
29 |
Correct |
7 ms |
2636 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
5 ms |
2636 KB |
Output is correct |
32 |
Correct |
4 ms |
2636 KB |
Output is correct |
33 |
Correct |
13 ms |
2756 KB |
Output is correct |
34 |
Correct |
8 ms |
2764 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 |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1028 KB |
Output is correct |
7 |
Correct |
2 ms |
972 KB |
Output is correct |
8 |
Correct |
2 ms |
1024 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 |
1160 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 |
1168 KB |
Output is correct |
15 |
Correct |
2 ms |
1100 KB |
Output is correct |
16 |
Correct |
2 ms |
1100 KB |
Output is correct |
17 |
Correct |
2 ms |
1684 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
4 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
3 ms |
2380 KB |
Output is correct |
23 |
Correct |
3 ms |
2380 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
3 ms |
2704 KB |
Output is correct |
28 |
Correct |
4 ms |
2764 KB |
Output is correct |
29 |
Correct |
7 ms |
2636 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
5 ms |
2636 KB |
Output is correct |
32 |
Correct |
6 ms |
2692 KB |
Output is correct |
33 |
Correct |
13 ms |
2756 KB |
Output is correct |
34 |
Correct |
8 ms |
2748 KB |
Output is correct |
35 |
Correct |
9 ms |
2936 KB |
Output is correct |
36 |
Correct |
3 ms |
2064 KB |
Output is correct |
37 |
Correct |
7 ms |
3148 KB |
Output is correct |
38 |
Correct |
10 ms |
3404 KB |
Output is correct |
39 |
Correct |
10 ms |
3276 KB |
Output is correct |
40 |
Correct |
10 ms |
3404 KB |
Output is correct |
41 |
Correct |
11 ms |
3532 KB |
Output is correct |
42 |
Correct |
8 ms |
3276 KB |
Output is correct |
43 |
Correct |
8 ms |
3804 KB |
Output is correct |
44 |
Correct |
8 ms |
3220 KB |
Output is correct |
45 |
Correct |
51 ms |
3696 KB |
Output is correct |
46 |
Correct |
28 ms |
3744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1020 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 |
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 |
2 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 |
1740 KB |
Output is correct |
18 |
Correct |
2 ms |
2380 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2380 KB |
Output is correct |
23 |
Correct |
3 ms |
2316 KB |
Output is correct |
24 |
Correct |
3 ms |
2636 KB |
Output is correct |
25 |
Correct |
4 ms |
2636 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
3 ms |
2764 KB |
Output is correct |
28 |
Correct |
3 ms |
2764 KB |
Output is correct |
29 |
Correct |
9 ms |
2600 KB |
Output is correct |
30 |
Correct |
4 ms |
2636 KB |
Output is correct |
31 |
Correct |
5 ms |
2636 KB |
Output is correct |
32 |
Correct |
5 ms |
2636 KB |
Output is correct |
33 |
Correct |
13 ms |
2636 KB |
Output is correct |
34 |
Correct |
8 ms |
2700 KB |
Output is correct |
35 |
Correct |
9 ms |
2892 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
7 ms |
3252 KB |
Output is correct |
38 |
Correct |
10 ms |
3376 KB |
Output is correct |
39 |
Correct |
10 ms |
3276 KB |
Output is correct |
40 |
Correct |
10 ms |
3404 KB |
Output is correct |
41 |
Correct |
11 ms |
3464 KB |
Output is correct |
42 |
Correct |
8 ms |
3252 KB |
Output is correct |
43 |
Correct |
10 ms |
3760 KB |
Output is correct |
44 |
Correct |
8 ms |
3276 KB |
Output is correct |
45 |
Correct |
52 ms |
3796 KB |
Output is correct |
46 |
Correct |
27 ms |
3784 KB |
Output is correct |
47 |
Correct |
21 ms |
12320 KB |
Output is correct |
48 |
Correct |
19 ms |
20020 KB |
Output is correct |
49 |
Correct |
19 ms |
21644 KB |
Output is correct |
50 |
Correct |
19 ms |
23500 KB |
Output is correct |
51 |
Correct |
41 ms |
27076 KB |
Output is correct |
52 |
Correct |
35 ms |
27020 KB |
Output is correct |
53 |
Correct |
30 ms |
25988 KB |
Output is correct |
54 |
Correct |
17 ms |
25352 KB |
Output is correct |
55 |
Correct |
21 ms |
25412 KB |
Output is correct |
56 |
Correct |
27 ms |
26768 KB |
Output is correct |
57 |
Correct |
17 ms |
24632 KB |
Output is correct |
58 |
Correct |
33 ms |
26504 KB |
Output is correct |
59 |
Correct |
27 ms |
26040 KB |
Output is correct |
60 |
Correct |
26 ms |
26060 KB |
Output is correct |
61 |
Correct |
26 ms |
26572 KB |
Output is correct |
62 |
Correct |
55 ms |
26828 KB |
Output is correct |
63 |
Correct |
69 ms |
27196 KB |
Output is correct |
64 |
Correct |
75 ms |
27196 KB |
Output is correct |
65 |
Correct |
353 ms |
27320 KB |
Output is correct |
66 |
Correct |
921 ms |
27564 KB |
Output is correct |
67 |
Correct |
477 ms |
27544 KB |
Output is correct |