POJ 3259 Wormholes Bellman_ford负权回路

Description

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
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

题意:

  John的农场里N块地,M条路连接两块地,W个虫洞;路是双向的,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。

思路:

  Bellman_ford判断一下有没有负权回路就行了。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 0x3f3f3f3f
#define N 10100
int nodenum, edgenum, original;
typedef struct Edge
{
    int u, v;
    int cost;
} Edge;
Edge edge[N];
int flag;
int dis[N];
bool Bellman_Ford()
{
    for(int i = 1; i <= nodenum; ++i)
        dis[i] = MAX;
    int ok;
    dis[1]=0;
    for(int i = 1; i <= nodenum - 1; ++i)
    {
        ok=1;
        for(int j = 1; j <= flag; ++j)
            if(dis[edge[j].v] > dis[edge[j].u] + edge[j].cost)
            {
                dis[edge[j].v] = dis[edge[j].u] + edge[j].cost;
                ok=0;
            }
        if(ok) //优化这里,如果这趟没跟新任何节点就可以直接退出了。
            break;
    }
    bool logo = 1;
    for(int i = 1; i <= flag; ++i)
        if(dis[edge[i].v] > dis[edge[i].u] + edge[i].cost)
        {
            logo = 0;
            break;
        }
    return logo;
}

int main()
{
    int T,num;
    cin>>T;
    while(T--)
    {
        scanf("%d%d%d", &nodenum, &edgenum, &num);
        flag=1;
        int end,begin,power;
        for(int i = 1; i <= edgenum; i++)
        {
            scanf("%d%d%d", &begin,&end,&power);
            edge[flag].u=begin;
            edge[flag].v=end;
            edge[flag].cost=power;
            flag++;
            edge[flag].u=end;
            edge[flag].v=begin;
            edge[flag].cost=power;
            flag++;
        }
        for(int i=0; i<num; i++)
        {
            cin>>begin>>end>>power;
            edge[flag].u=begin;
            edge[flag].v=end;
            edge[flag].cost=-power;//注意这里是负的
            flag++;
        }
        if(Bellman_Ford()==0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/aiguona/p/7240643.html