[noip]2014复赛提高组day2 寻找道路

本蒟蒻整了接近三小时,交了n遍,9个点都RE,好不容易整出来了(原来是掉了个边界)

#include<iostream> 
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,x,y,s,t,num,sum,f2;
int h[10050],fla[10050],fl[10050];
int q[5000000],zd[10050];
struct edge{
	int next1;
	int to1;
}a[5000000];
void bfs1(int x)
{
	int hhh=1,ttt=1;
	 q[hhh]=x; fla[x]=1;
	 while(hhh<=ttt)
	  {
	  	x=q[hhh];
	  	for(int i=h[x];i>0;i=a[i].next1)
	  	 if(fla[a[i].to1]==0)
	  	 {
	  	 	ttt++;
	  	 	q[ttt]=a[i].to1;
	  	    fla[a[i].to1]=1;
	  	 }
	  	hhh++; 
	  }
}
void bfs2(int x,int y)
{
	memset(q,0,sizeof(q));
	int hd=1,tl=1;
	 q[hd]=x;
	 while(hd<=tl)
	  {
	  	int x1=q[hd];
	  	for(int i=h[x1];i>0;i=a[i].next1)
	  	 {
	  	 	if(fla[a[i].to1]==1)//边界问题,哎
	  	 	 {
			     tl++;
			   	 q[tl]=a[i].to1;
			   	 zd[tl]=zd[hd]+1;
			   	 if(q[tl]==y){cout<<zd[tl]; f2=1;} 
			   	 fla[q[tl]]=0;
			 }
	  	 }
         hd++;
	  } 
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	 {
	 	cin>>x>>y;
	 	a[i].next1=h[y];
	 	a[i].to1=x;
	 	h[y]=i;
	 }
	 cin>>s>>t;
	 bfs1(t);
	 for(int i=1;i<=n;i++)
	   if(fla[i]==0)
	  	 for(int j=h[i];j>0;j=a[j].next1)
	  	   fl[a[j].to1]=1;
	 for(int i=1;i<=n;i++)
	   if(fla[i]!=0&&fl[i]==1)
	  	 fla[i]=0; 
	 bfs2(t,s);
	 if(f2==0) cout<<"-1";
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值