POJ - 3259 Wormholes

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. 

【题意】题目不难,讲的是一个人有n个农场,农场里有m条无向的道路,连接两个农场,从两个中的任意一个到达另一个都要一段时间,农场里还有w个虫洞,也是连接两个农场,但是从第一个农场到第二个农场需要的时间为负数,现在问你能不能选择出一个农场使得从该农场出发后回到该农场的时间为负数。其中道路是双向的,但是虫洞是单项的,如果有一个农场满足该条件就输出YES,否则为NO。样例有多组。

【分析】题目简单,就是判断所给的图中是否有负权回路,如果存在那么一定存在所给的农场,就输出yes。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef struct
{
    int to,val;//分别表示一条边的终点和这条边的权值
}edge;
vector < edge > mat[505];//统计和当前点相连的边,mat【i】表示以i为起点的边
int n,m,w;
int change[505];//统计改变次数
int value[505];//当前价值
queue <edge> ans;
bool spfa(void)
{
    memset(change,0,sizeof(change));
    fill(value,value+504,0x5fffffff);//一系列初始化的操作
    while(!ans.empty()) ans.pop();//因为多组输入,所以要清空
    edge x,y;
    x.to=1,x.val=0;
    value[x.to]=0;//初始化第一条边
    change[1]=1;
    ans.push(x);
    while(!ans.empty())
    {
        x=ans.front();
        ans.pop();
        int len=mat[x.to].size();
        for(int i=0;i<len;i++)//每次取出和x.to相连的边
        {
            y=mat[x.to][i];//取出后的y这条边的起点是x.to,终点是y.to,权值是y.val
            if(value[y.to]>value[x.to]+y.val)//如果这条边终点的权值可以减小,那么更新它
            {
                value[y.to]=value[x.to]+y.val;
                change[y.to]++;//用来统计这个点的改变次数
                ans.push(y);//因为这点改变了,所以和这个点相连的边就要重新统计
                if(change[y.to]==n)//如果某个点被更新了n次说明这个图中存在负权回路,所以就输出yes
                    return true;
            }
        }
    }
    return false;
}
int main ()
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<505;i++)
            mat[i].clear();
        scanf("%d%d%d",&n,&m,&w);
        int a,b,c;
        edge x;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            x.to=b,x.val=c;
            mat[a].push_back(x);
            x.to=a;
            mat[b].push_back(x);
        }
        for(int i=0;i<w;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            x.to=b,x.val=-c;
            mat[a].push_back(x);//注意虫洞是有向的,道路是无向的
        }
        if(spfa())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值