【NOIP2014】寻找道路

题目链接

算法:

   先反向建图,从终点开始BFS,找出所有与其连通的点

            其次,考虑到图不一定联通,我们特殊处理那些压根就不会与终点在一个联通块里的点

            最后,愉快单源最短路即可

Code:

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T> void read(T &num){
	char c=getchar();T f=1;num=0;
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
	num*=f;
}
template<typename T> void qwq(T x){
	if(x>9)qwq(x/10);
	putchar(x%10+'0');
}
template<typename T> void write(T x){
	if(x<0){x=-x;putchar('-');}
	qwq(x);putchar('\n');
}
int n,m;

struct wzy{
	int nxt,vertice;
}edge[200010];
int head[10010];int len1=0;
inline void add_edge(int x,int y){
	edge[++len1].nxt=head[x];edge[len1].vertice=y;head[x]=len1;
	return;
}

int s,t;bool wjp[10010];bool hashs[10010];
queue<int>v;
inline void Pretreatment(){
	v.push(t);wjp[t]=1;
	while(!v.empty()){
		int nop=v.front();v.pop();
		for(int i=head[nop];i;i=edge[i].nxt){
			int temp=edge[i].vertice;
			if(!wjp[temp]){wjp[temp]=1;v.push(temp);}
		}
	}
	return;
}
int dis[10010];bool in[10010];
inline void SPFA(){
	v.push(t);dis[t]=0;in[t]=1;
	while(!v.empty()){
		int nop=v.front();in[nop]=0;v.pop();
		for(int i=head[nop];i;i=edge[i].nxt){
			int temp=edge[i].vertice;
			if(!hashs[temp])continue;
			if(dis[temp]>dis[nop]+1){
				chkmin(dis[temp],dis[nop]+1);
				if(!in[temp]){in[temp]=1;v.push(temp);}
			}
		}
	}
	return;
}

int main(){
	read(n);read(m);
	rep(i,1,m){
		int x,y;read(x);read(y);
		if(x!=y)add_edge(y,x);
	}
	read(s);read(t);
	
	memset(dis,0x3f,sizeof(dis));
	Pretreatment();
	memcpy(hashs,wjp,sizeof(wjp));
	rep(i,1,n){
		if(wjp[i])continue;
		for(int j=head[i];j;j=edge[j].nxt){
			int nop=edge[j].vertice;
			if(hashs[nop])hashs[nop]=0;
		}
	}
	SPFA();
	
	write((dis[s]>n)?-1:dis[s]);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值