This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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
int const N=3e4+3;
int const sN=174;
int const M=3e4+3;
int b[M];
int p[M];
int d[N][sN];
int const INF=1e9;
vector<pair<pii,int>>adj[N][sN];
bitset<sN> vis[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];
if(p[i]>=sN)
{
int nxt=b[i]+p[i];
int cnt=1;
while(nxt<n)
{
adj[b[i]][0].pb({{nxt,0},cnt});
++cnt;
nxt+=p[i];
}
nxt=b[i]-p[i];
cnt=1;
while(nxt>=0)
{
adj[b[i]][0].pb({{nxt,0},cnt});
++cnt;
nxt-=p[i];
}
}
else
{
adj[b[i]][0].pb({{b[i],p[i]},0});
}
}
F0R(i,n)
{
F0R(j,sN)
{
if(i+j>=n)break;
adj[i][j].pb({{i+j,j},1});
adj[i][j].pb({{i+j,0},1});
}
F0R(j,sN)
{
if(i-j<0)break;
adj[i][j].pb({{i-j,j},1});
adj[i][j].pb({{i-j,0},1});
}
}
F0R(i,n)
{
fill(d[i],d[i]+sN,INF);
}
priority_queue<pair<int,pii>> q;
d[b[0]][0]=0;
q.push({0,{b[0],0}});
while(!q.empty())
{
int pos = q.top().se.fi;
int pw = q.top().se.se;
q.pop();
if(vis[pos][pw])continue;
vis[pos][pw]=1;
trav(e,adj[pos][pw])
{
int cand = d[pos][pw]+e.se;
if(cand<d[e.fi.fi][e.fi.se])
{
d[e.fi.fi][e.fi.se]=cand;
q.push({-cand,e.fi});
}
}
adj[pos][pw].clear();
}
if(d[b[1]][0]==INF)cout<<-1<<'\n';
else cout<<d[b[1]][0]<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |