题目描述
在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:
1 .路径上的所有点的出边所指向的点都直接或间接与终点连通。
2 .在满足条件1 的情况下使路径最短。
注意:图G 中可能存在重边和自环,题目保证终点没有出边。
请你输出符合条件的路径的长度。
输入输出格式
输入格式:
输入文件名为road .in。
第一行有两个用一个空格隔开的整数n 和m ,表示图有n 个点和m 条边。
接下来的m 行每行2 个整数x 、y ,之间用一个空格隔开,表示有一条边从点x 指向点y 。
最后一行有两个用一个空格隔开的整数s 、t ,表示起点为s ,终点为t 。
输出格式:
输出文件名为road .out 。
输出只有一行,包含一个整数,表示满足题目描述的最短路径的长度。如果这样的路径不存在,输出- 1 。
输入输出样例
3 2 1 2 2 1 1 3
-1
6 6 1 2 1 3 2 6 2 5 4 5 3 4 1 5
3
说明
解释1:
如上图所示,箭头表示有向道路,圆点表示城市。起点1 与终点3 不连通,所以满足题目描述的路径不存在,故输出- 1 。
解释2:
如上图所示,满足条件的路径为1 - >3- >4- >5。注意点2 不能在答案路径中,因为点2连了一条边到点6 ,而点6 不与终点5 连通。
对于30%的数据,0<n≤10,0<m≤20;
对于60%的数据,0<n≤100,0<m≤2000;
对于100%的数据,0<n≤10,000,0<m≤200,000,0<x,y,s,t≤n,x≠t。
思路:因为是一个边权为1的图,所以可以不用跑最短路而用BFS代替,使用前向星存图。
对于60%的数据,n≤100故可以预处理出每个点的与终点的连通性,再从起点BFS,BFS到的每一个点判断出边所连的点是否与终点联通即可。复杂度O(n²+m²)=O(m²).存图也可以使用邻接矩阵。
对于100%的数据,只需要有一点小的观察:我们可以先反向加边,从终点开始BFS,BFS到的点就是连通的点,再正向加边,从起点开始BFS,中途判断题意的连通性。复杂度O(n²).
70代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
const int MAXN=110;
int a[MAXN][MAXN],w[MAXN];
bool go[MAXN][MAXN],visit[MAXN],z;
queue <int> que;
int ans,Ans;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if (u!=v)
{
a[u][v]=1;
}
}
int s,t;
scanf("%d%d",&s,&t);
for (int i=1;i<=n;i++)
{
que.push(i);
memset(visit,0,sizeof(visit));
while (!que.empty())
{
int x=que.front();
que.pop();
for (int j=1;j<=n;j++)
{
if (a[x][j] && !go[i][j] && i!=j && !visit[j])
{
go[i][j]=1;
visit[j]=true;
que.push(j);
}
}
}
}
for (int i=1;i<=n;i++)
{
w[i]=100000000;
}
memset(visit,0,sizeof(visit));
go[t][t]=true;
que.push(s);
visit[s]=true;
w[s]=0;
while (!que.empty())
{
int x=que.front();
que.pop();
for (int i=1;i<=n;i++)
{
if (i==x || !a[x][i])
{
continue;
}
bool p=true;
for (int j=1;j<=n;j++)
{
if (a[i][j])
{
if (!go[j][t])
{
p=false;
break;
}
}
}
if (p && !visit[i])
{
visit[i]=true;
que.push(i);
w[i]=min(w[i],w[x]+1);
}
}
}
if (w[t]==100000000)
{
cout<<-1;
}
else
{
cout<<w[t];
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
const int MAXM=200010;
const int MAXN=10010;
struct edge
{
int next,node,w;
};
edge e[4*MAXM];
int head[MAXN],tot,w[MAXN],a[MAXM],b[MAXM];
bool go[MAXN],visit[MAXN];
queue <int> que;
void addedge(int a,int b,int w)
{
e[++tot].next=head[a];
head[a]=tot;
e[tot].node=b;
e[tot].w=w;
}
void ready()
{
memset(e,0,sizeof(e));
tot=0;
memset(head,0,sizeof(head));
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
addedge(b[i],a[i],1);
}
int s,t;
scanf("%d%d",&s,&t);
que.push(t);
while (!que.empty())
{
int x=que.front();
que.pop();
for (int i=head[x];i;i=e[i].next)
{
if (!visit[e[i].node])
{
visit[e[i].node]=true;
go[e[i].node]=true;
que.push(e[i].node);
}
}
}
go[t]=true;
go[s]=true;
ready();
for (int i=1;i<=m;i++)
{
addedge(a[i],b[i],1);
}
memset(visit,false,sizeof(visit));
for (int i=1;i<=n;i++)
{
w[i]=2147483640;
}
que.push(s);
w[s]=0;
while (!que.empty())
{
int x=que.front();
que.pop();
for (int i=head[x];i;i=e[i].next)
{
bool p=true;
for (int j=head[e[i].node];j;j=e[j].next)
{
if (!go[e[j].node])
{
p=false;
break;
}
}
if (!visit[e[i].node] && p)
{
visit[e[i].node]=true;
que.push(e[i].node);
w[e[i].node]=min(w[x]+1,w[e[i].node]);
}
}
}
if (w[t]==2147483640)
{
cout<<"-1";
}
else
{
cout<<w[t];
}
return 0;
}