code
#include<cstdio>
#include<queue>
using namespace std;
struct cmp{
int z,xh;
}x,y;
bool operator <(cmp x,cmp y)
{ return x.z>y.z;
}
priority_queue<cmp> q;
struct data
{ int next,to;
}e[2000001],d[2000001];
int len1=0,len2=0;
int head[100001],head2[100001];
int connect(int x,int y)
{ e[++len1].next=head[x];
e[len1].to=y;
head[x]=len1;
d[++len2].next=head2[y];
d[len2].to=x;
head2[y]=len2;
}
int lb[100001];
int p[100001],pd[100001];
void dfs(int x)
{ p[x]=1;
for (int i=head2[x];i;i=d[i].next)
{ int v=d[i].to;
if (p[v]==0)
dfs(v);
}
}
int dis[100001];
int main()
{ int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;++i)
{ int x,y;scanf("%d%d",&x,&y);
connect(x,y);
}
int start,end;scanf("%d%d",&start,&end);
dfs(end);
int pp=0;
for (int i=1;i<=n;++i)
if (p[i]==0)
{ pd[i]=1;
for (int j=head2[i];j;j=d[j].next)
if (d[j].to!=end)
pd[d[j].to]=1;
}
for (int i=1;i<=n;++i)
dis[i]=1e9;
dis[start]=0;
if (pd[start]==1)
{ printf("%d",-1);
return 0;
}
x.xh=start;x.z=0;
q.push(x);
while (!q.empty())
{ x=q.top();
q.pop();
if (lb[x.xh]==1)
continue;
lb[x.xh]=1;
for (int i=head[x.xh];i;i=e[i].next)
{ if (pd[e[i].to])
continue;
if (dis[e[i].to]>x.z+1)
{ dis[e[i].to]=x.z+1;
y.xh=e[i].to;y.z=x.z+1;
q.push(y);
}
}
}
if (dis[end]!=1e9)
printf("%d",dis[end]);
else
printf("%d",-1);
}
挺简单的一道题,出现了两个很值得注意的错误
写Floyd对拍的时候注意Floyd一定是把k放在最外层
使用结构体优先队列时 return x.z>y.z;