天天看點

poj3259Wormholes(bellman_ford判斷負環)

Wormholes

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 52526 Accepted: 19557

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

題意:田地裡有很多田埂可以連通不同的區域,但是現在出現蟲洞,蟲洞可以讓你回到過去的某一個地點,現在問是否存在一個路使得你回到過去的某一個點見到自己(判斷是否存在負環,負環可以一直減少時間,這樣就能回到過去啦!)

可以選用spfa或者bellman判斷負環

bellman原理:Bellman_Ford是通過N-1次循環求出每個點到原點的最短路的,每次循環周遊所有的邊,如果能更新就更新。前面說過為什麼N-1次就行(若一個點到起點的最短路需要經過N個點,包括他自己,那麼第N次循環就能确定他的最短路)。

最短路一定是不含環的,如果存在負環就根本不存在最短路了。是以這個方法可以用來判斷是否有負環,如果循環了N-1次後還能進行松弛操作,說明有負環。Bellman_Ford的複雜度是O(M*N)。

首先田埂是雙向路徑,是以建圖時要采取雙向建圖,蟲洞路徑指派要賦負值,因為是減少時間。這樣就是枚舉每個點并判斷是否能産生負環

條件為:d[v]>d[u]+w;如果存在則能産生負環(d[i]為到i點為止,所産生的總值,如果到下一點為止産生的總值大于前面的所有點值加上下一點本身的權值,重新重新整理)

代碼:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define INF 10000
struct node
{
    int u,v,w;
}edge[*++];

int d[];
int n,m1;
int bellman_ford()
{
    for(int i=;i<=n;i++)
    {
        d[i]=INF;
    }
    d[]=;

    for(int i=;i<n;i++)
    {
        int flag=;
        for(int j=;j<m1;j++)
        {
            int u=edge[j].u;
            int v=edge[j].v;
            int w=edge[j].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                flag=;
            }
        }
        if(flag)
        return ;
    }
    for(int i=;i<m1;i++)
    {
        if(d[edge[i].v]>d[edge[i].u]+edge[i].w)
        return ;
    }
    return ;
}

int main()
{
    int t;
    int M,W;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&M,&W);
        m1=;
        int u,v,w;
        for(int i=;i<=M;i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            edge[m1].u=u;
            edge[m1].v=v;
            edge[m1++].w=w;

            edge[m1].v=u;
            edge[m1].u=v;
            edge[m1++].w=w;
        }

        for(int i=;i<=W;i++)
        {

            scanf("%d %d %d",&u,&v,&w);
            edge[m1].u=u;
            edge[m1].v=v;
            edge[m1++].w=-w;
        }

        if(bellman_ford())
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
    return ;
}