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<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
int p[33333];
int q[33333];
std::vector<int> a[33333];
std::priority_queue<std::pair<int,int> > pq;
int t[33333][333];
int d[33333];
int v[33333];
int main()
{
std::pair<int,int> tt;
int i,j,k,l,n,m;
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d%d",&p[i],&q[i]);
if(q[i]>=300||!t[p[i]][q[i]])a[p[i]].push_back(q[i]);
if(q[i]<300)t[p[i]][q[i]]=1;
}
for(i=0;i<n;i++)d[i]=1e9;
pq.push(std::make_pair(0,p[0]));
while(!pq.empty()&&!v[p[1]])
{
tt=pq.top();
pq.pop();
if(v[tt.second])continue;
v[tt.second]=1;
d[tt.second]=-tt.first;
k=tt.second;
for(j=0;j<a[k].size();j++)
{
for(l=1;k+l*a[k][j]<n;l++)
{
if(!v[k+l*a[k][j]]&&d[k+l*a[k][j]]>d[k]+l)
{
d[k+l*a[k][j]]=d[k]+l;
pq.push(std::make_pair(-d[k+l*a[k][j]],k+l*a[k][j]));
}
if(a[k][j]<300&&t[k+l*a[k][j]][a[k][j]])break;
}
for(l=1;k-l*a[k][j]>=0;l++)
{
if(!v[k-l*a[k][j]]&&d[k-l*a[k][j]]>d[k]+l)
{
d[k-l*a[k][j]]=d[k]+l;
pq.push(std::make_pair(-d[k-l*a[k][j]],k-l*a[k][j]));
}
if(a[k][j]<300&&t[k-l*a[k][j]][a[k][j]])break;
}
}
}
if(!v[p[1]])puts("-1");
else printf("%d\n",d[p[1]]);
}
# | 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... |