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 M=3e4+3;
int b[M],p[M];
int const N=3e4+3;
vpi adj[N];
bitset<N> vis;
int d[N];
int const INF=1e9;
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];
}
F0R(i,m)
{
int u = b[i];
int v=b[i]+p[i];
int cnt=1;
while(v<n)
{
adj[u].pb({v,cnt});
++cnt;
v+=p[i];
}
v=b[i]-p[i];
cnt=1;
while(v>=0)
{
adj[u].pb({v,cnt});
++cnt;
v-=p[i];
}
}
priority_queue<pii> q;
q.push({0,b[0]});
fill(d,d+n,INF);
d[b[0]]=0;
while(!q.empty() && (!vis[b[1]]))
{
int u=q.top().se;
q.pop();
if(vis[u])continue;
vis[u]=1;
trav(e,adj[u])
{
int cand = d[u]+e.se;
if(cand<d[e.fi])
{
d[e.fi]=cand;
q.push({-cand,e.fi});
}
}
}
if(d[b[1]]==INF)cout<<-1<<'\n';
else cout<<d[b[1]]<<'\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... |