POJ-3259 Wormholes( 最短路 )

Posted 「空白」物語

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-3259 Wormholes( 最短路 )相关的知识,希望对你有一定的参考价值。

题目链接:http://poj.org/problem?id=3259

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

题目大意:有若干个虫洞,给出了若干普通路径和其所用时间以及虫洞的路径和其倒回的时间,现问你能否回到出发之前的时间,注意普通路径是双向的,虫洞是单向的
解题思路:由题目所给信息已经可以构建一张完整的图了,然后进一步理解题目的意思其实是这张图是否存在负环,因此使用Bellman_Ford即可

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<map>

using namespace std;

typedef struct Edge{
    int beg, end, time;
}Edge;

int n, m, w, sum, dis[505];
Edge edges[6000];

bool Bellman_Ford(){
    for( int t = 1; t < n; t++ ){
        for( int i = 1; i <= sum; i++ ){
            dis[edges[i].end] = min( dis[edges[i].end], dis[edges[i].beg] + edges[i].time );
        }
    }

    for( int i = 1; i <= sum; i++ ){
        if( dis[edges[i].end] > dis[edges[i].beg] + edges[i].time )
            return false;
    }

    return true;
}

int main(){
    ios::sync_with_stdio( false );

    int t;
    cin >> t;
    while( t-- ){
        cin >> n >> m >> w;
        sum = 0;
        memset( dis, 0x3f3f3f3f, sizeof( dis ) );
        for( int i = 0; i < m; i++ ){
            sum++;
            cin >> edges[sum].beg >> edges[sum].end >> edges[sum].time;
            if( edges[sum].beg == 1 ){
                dis[edges[sum].end] = min( dis[edges[sum].end], edges[sum].time );
            }
            sum++;
            edges[sum].beg = edges[sum - 1].end;
            edges[sum].end = edges[sum - 1].beg;
            edges[sum].time = edges[sum - 1].time;
            if( edges[sum].beg == 1 ){
                dis[edges[sum].end] = min( dis[edges[sum].end], edges[sum].time );
            }
        }
        for( int i = 0; i < w; i++ ){
            sum++;
            cin >> edges[sum].beg >> edges[sum].end >> edges[sum].time;
            edges[sum].time *= -1;
            if( edges[sum].beg == 1 ){
                dis[edges[sum].end] = min( dis[edges[sum].end], edges[sum].time );
            }
        }

        if( Bellman_Ford() ){
            cout << "NO\n";
        }
        else{
            cout << "YES\n";
        }
    }

    return 0;
}

 

以上是关于POJ-3259 Wormholes( 最短路 )的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3259 Wormholes最短路/SPFA判断负环模板

poj3259 Wormholes (判负环)spfa(模板)

POJ 3259 -- Wormholes

Wormholes POJ 3259SPFA

[2016-04-03][POJ][3259][Wormholes]

POJ3259:Wormholes(spfa判负环)