Wormholes(Bellman-ford或Spfa)

Wormholes(Bellman-ford或Spfa)

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1…N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself 😃 .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2…M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2…M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output
Lines 1…F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).

Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output
NO
YES

Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

题意: m条双向路径,花费ts时间,w条单向虫洞,回到ts之前,问FJ从某个领域开始,穿过一些路径和虫洞,在他最初离开之前的一段时间回到起始领域。其实就是让判断图中是否存在负权环,存在输出YES,否则输出NO

思路: 因为数据太水,用floyd+剪枝(if判断)能卡过,1s大约对应的程序复杂度为108,这道题给的是2s,就是2 * 107,floyd三层for循环耗时500 * 500 * 500=1.25 * 108,不能直接min,应用if剪枝,这道题标准应用Bellman-ford或者Spfa解决

例:

1
4 1 3
1 4 200
1 2 1
2 3 1
3 2 1

这个例子,用Floyd 输出yes,但用Bellman-ford输出no,3-2-3是-2,形成负权回路( 如果存在一个环(从某个点出发又回到自己的路径),而且这个环上所有权值之和是负数,那这就是一个负权环,也叫负权回路。存在负权回路的图是不能求两点间最短路的,因为只要在负权回路上不断兜圈子,所得的最短路长度可以任意小。 ),而Floyd算法解决负权边,是不能存在负权回路的,否则会出错,但是为什么会输出no呢,原文中有这样一句翻译:虫洞很特别,因为它是一条单向的路径,在你进入虫洞之前把你送到它的目的地!,个人理解为,需先通过路径到达目的地后,在通过虫洞返回到起点和出发前的时间,1-4之后没有虫洞可回去,这样就可以理解为啥上面样例输出no了
在这里插入图片描述

Floyd

#include<stdio.h>//Floyd
#include<string.h>
#include<queue>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
int a[510][510],n;
int floyd()
{
	int i,j,k;
	int flag=0;
	for(k=1; k<=n; k++)
	{
		for(i=1; i<=n; i++)
		{
			for(j=1; j<=n; j++)
			{
				if(a[i][j]>a[i][k]+a[k][j])//这里不能用a[i][j]=min(a[i][j],a[i][k]+a[k][j]); 会超时
				a[i][j]=a[i][k]+a[k][j];
			}
			if(a[i][i]<0)
			return 1;
		}
	}
	return 0;
}
int main()
{
	int m,w,s,e,t,i,j,k,f;
	scanf("%d",&f);
	while(f--)
	{
		scanf("%d%d%d",&n,&m,&w);
		for(i=1; i<=n; i++)
			for(j=1; j<=n; j++)
			{
				if(i==j)
					a[i][j]=0;
				else
					a[i][j]=inf;
			}
		while(m--)
		{
			scanf("%d%d%d",&s,&e,&t);
			if(a[s][e]>t)
				a[s][e]=a[e][s]=t;
		}
		while(w--)
		{
			scanf("%d%d%d",&s,&e,&t);
			a[s][e]=-t;
		}

		if(floyd())
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}

Bellman-ford

#include<stdio.h>//Bellman-Ford
#include<string.h>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
int dis[5100],u[5100],v[5100],w[5100];
int main()
{
	int m,ww,s,e,t,i,j,k,n,f;
	scanf("%d",&f);
	while(f--)
	{
		scanf("%d%d%d",&n,&m,&ww);
		for(i=1;i<=m*2;i+=2)
		{
			scanf("%d%d%d",&u[i],&v[i],&w[i]);
			u[i+1]=v[i];//双向
			v[i+1]=u[i];
			w[i+1]=w[i];
		}
		for(i=2*m+1;i<=2*m+ww;i++)
		{
			scanf("%d%d%d",&u[i],&v[i],&w[i]);
			w[i]=-w[i];
		}
		for(i=1;i<=n;i++)//初始化所有的道路不通 
		dis[i]=inf;
		dis[1]=0;//1-1为0
		int flag=0;
		for(i=1;i<n;i++)
		{
			flag=0;//检测本次循环是否松弛 
			for(j=1;j<=m*2+ww;j++)
			{
				if(dis[v[j]]>dis[u[j]]+w[j])
				{
					dis[v[j]]=dis[u[j]]+w[j];
					flag=1;
				}
			}
			if(flag==0)//若没有则松弛结束 
			break;
		 } 
		 int check=0;
		 for(i=1;i<=2*m+ww;i++)
		 if(dis[v[i]]>dis[u[i]]+w[i])
		 check=1;//经过n-1条边松弛完成后,
		 if(check)//若还能松弛,则存在负权环
		 printf("YES\n");
		 else
		 printf("NO\n");
	}
	return 0;
}

Spfa

#include<stdio.h>
#include<string.h>
#include<queue>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
const int N=1e5+10;
int head[N],dis[N],book[N],ans[N];
int f,n,m,w,cnt;
struct pp
{
	int to,next,w;
}p[N];
void add(int x,int y,int z)
{
	p[cnt].w=z;
	p[cnt].to=y;
	p[cnt].next=head[x];
	head[x]=cnt++;
}
int spfa(int x)
{
	queue<int>q;
	q.push(x);
	book[x]=1;
	memset(dis,inf,sizeof(dis));
	dis[x]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		book[u]=0;
		for(int i=head[u];~i;i=p[i].next)
		{
			int y=p[i].to;
			if(dis[y]>dis[u]+p[i].w)
			{
				dis[y]=dis[u]+p[i].w;
				ans[y]=ans[u]+1;
				if(ans[y]>=n)//ans[y]记录从1到y的最短路上包含几个点(不包含本身),即最多有n-1个点,若超过则代表有负环
				return 1;
				if(!book[y])
				{
					book[y]=1;
					q.push(y);
				}
			}
			
		}
	}
	return 0;
}
int main()
{
	
	int i,j,k,a,b,c;
	scanf("%d",&f);
	while(f--)
	{
		cnt=0;
		memset(head,-1,sizeof(head));
		memset(book,0,sizeof(book));
		memset(ans,0,sizeof(ans));
		scanf("%d%d%d",&n,&m,&w);
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);//双向边
			add(b,a,c);
		}
		while(w--)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,-c);//单向边
		}
		if(spfa(1))
		printf("YES\n");
		else
		printf("NO\n");
	}
	return 0;
  }  
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值