POJ 3259 Wormholes(floyd求负环,最短路)

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..  MW+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).
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.

题目大意:

POJ 3259
农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞到达目的地并且时光倒流!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他想做到以下几点:从某个农场开始,经过一些路径和虫洞进行探索,他要回到最开始出发的那个农场,并回到出发前的时间。也许他就能遇到自己了(回到过去看到自己出发前的样子)。为了帮助FJ判断是否可以达成他的心愿,他会为你提供F(1≤F≤5)个农场的完整映射。任何路径所花时间都不大于10000秒,任何的虫洞回溯时间都不大于10000秒。

输入
第1行:一个整数F表示接下来会有F个农场的映射。
每个农场映射的第一行:分别是三个空格隔开的整数:N,M和W
接下来的M行:三个空格分开的数字(S,E,T)描述,分别为:需要T秒走过S和E之间的双向路径。两个农场可能由一个以上的路径来连接。
接下来的W行:三个空格分开的数字(S,E,T)描述虫洞,描述单向路径,S到E可以回溯T秒。

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
输出
F行,每行代表一个农场映射。
每个农场映射占单独的一行,” YES”表示能满足要求,”NO”表示不能满足要求。
NO
YES

图解:


思路:判断是否存在负环,用floyd算法来判断。3层循环,其中不能用min,用min会超时,只能用if来判断大小。具体的看代码中的解释。

代码:

/*
1
3 2 1
1 2 3
2 3 4
3 1 8
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int map[505][505],n,m,k,num=0;
//map不是直线距离!!不是直线距离!!是经过计算后的i到j所用的最短的时间,也不是最短的长度!!是时间最短!!
int floyd()
{
    int i,j,k,f=0;
    //三重循环
    for(k=1;k<=n;k++)
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)//如果走<i,j>这条路径的长度大于不走这条路径的长度
            {
                int t=map[i][k]+map[k][j];
                if(map[i][j]>t)map[i][j]=t;
                //意为求经过k到j近还是不经过近,并更新map[i][j]的值,求出从i到j用的最短时间,而不是最短路径!可以绕道
                //用min会超时,所以用if判断。
                //map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
                //下面是测试的数据,可以看出在测试的时候map是被一直更新的。
                /*
                printf("---------------------------------------k:%d i:%d j:%d\n",k,i,j);
                for(int a=1;a<=n;a++)
                {
                    for(int b=1;b<=n;b++)
                    {printf("%d ",map[a][b]);}
                    printf("\n");
                }
                */
            }
            if(map[i][i]<0)return 1;  //看看所经过的k相同时,从不同的点出发是否存在负环
        }
    return f;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,a,b,c;
        scanf("%d%d%d",&n,&m,&k);
        memset(map,0x3f3f3f3f,sizeof(map));
        for(i=1;i<=n;i++)map[i][i]=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(c<map[a][b])map[a][b]=map[b][a]=c;
        }
        for(i=1;i<=k;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            map[a][b]=-c;
        }
        num++;
        int f=floyd();
        if(!f)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值