Wormholes 虫洞(floyed求最短路)

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..NM (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, FF farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: NM, and W
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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).

在探索他的许多农场时,法默约翰发现了许多惊人的虫洞。虫洞非常奇特,因为它是一条单向路径,可以在您进入虫洞之前的时间将您送到目的地!FJ 的每个农场都包含N (1 ≤ N ≤ 500) 个田地,方便地编号为 1.. NM (1 ≤ M ≤ 2500) 条路径和W (1 ≤ W ≤ 200) 个虫洞。

由于 FJ 是一个狂热的时间旅行迷,他想做以下事情:从某个场地开始,穿过一些路径和虫洞,并在他最初出发前的某个时间返回出发场地。也许他将能够见到自己:)。

为了帮助 FJ 确定这是否可行,他将向您提供他的农场F (1 ≤ F ≤ 5) 的完整地图。没有一条路径需要超过 10,000 秒的时间,也没有虫洞可以让 FJ 回到过去超过 10,000 秒。

输入

第1行:一个整数, ˚FF农场描述如下。
每个农场的第 1 行:三个空格分隔的整数分别为: NMW 每个农场的
第 2..  M +1行:三个空格分隔的数字( SET)分别描述:一条双向路径在 SE之间需要 T秒才能遍历。两个字段可能由多条路径连接。
线 中号2 .. 中号w ^每个场的1:三个空间分隔的数字( S ,  E ,  T ) 分别描述: 从 SE 的单向路径,也将旅行者移回 T秒。

输出

第 1..  F 行:对于每个农场,如果 FJ 可以实现他的目标,则输出“YES”,否则输出“NO”(不包括引号)。

样本输入

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

样本输出

NO

YES

题意描述:给多组数据,然后是农场的N块田地 M条路径和W个虫洞,然后输入M行双向路径和经过时间,然后是w行表示虫洞,及它的单项路径和倒退时间(即让你返回到前多少秒)如果fj在出发前可以回到原位置,即输出yes,否则no

拿输入数据举例:

3 2 1
1 2 3
2 3 4
3 1 8

3个田地,2条路,1个虫洞可以时间倒退(3+4=7  7+(-8)=-1;也就是说fj可以在出发前回到原地)

解题思路:可以利用floyed算法处理负权边问题,当有一个路的值小于0,就代表可以返回,满足要求,跳出循环即可。

当然你也可以用bellman算法写

注意事项:双向路a-b可能有多条,所以要记录最短的那个

AC代码

#include<stdio.h>
int e[510][510];
int inf=9999999;
int main(void)
{
	int f,n,m,w,a,b,t,d;
	scanf("%d",&d);
	while(d--)
	{
		f=0;
		scanf("%d %d %d",&n,&m,&w);
		//初始化
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				if(i==j) e[i][j]=0;
				else e[i][j]=inf; 	
		for(int i=1;i<=m;i++)//双向路  正边权 
		{
			scanf("%d %d %d",&a,&b,&t);//a~b/b~a t时间 
			if(e[a][b]>t)
			{
				e[a][b]=e[b][a]=t;
			}
				
		}
		for(int i=1;i<=w;i++) //虫洞路  负边权 
		{
			scanf("%d %d %d",&a,&b,&t); //a~b -t时间即倒退
			if(e[a][b]>-t) //
				e[a][b]=-t;
		}
		for(int k=1;k<=n;k++)
		{
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=n;j++)
				{
					if(e[i][j]>e[i][k]+e[k][j])
						e[i][j]=e[i][k]+e[k][j];				
				}
				if(e[i][i]<0)
				{
					f=1;
					break;//注意书写格式 
				}	
			}
		//	if(f==1)
			//	break;	
		}
		if(f==1)
			printf("YES\n");
		else
			printf("NO\n");	
		
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值