POJ3259_Wormholes_spfa

来源:互联网 发布:cf英雄级武器淘宝网 编辑:程序博客网 时间:2024/06/09 17:13

Wormholes
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 50118 Accepted: 18480

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..M+W+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

23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8

Sample Output

NOYES

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 个双向虫洞,虫洞的长度是负的。

求问是否存在一个点,从这个点出发回来之后,穿越了时空(路径的总长度是负的)。

实际上是判断负权环是否存在。用spfa判断即可。

题面有点问题,实际上 点1 一定在负权环上。


#include<cstdio>#include<iostream>#include<queue>#include<cstring>using namespace std;const int maxn = 510;int  Map [maxn][maxn];//敲黑板,spfa 零件bool in [maxn];int  times [maxn];int dis [maxn];//敲黑板,spfa 零件//敲黑板 spfa ,判负权环bool spfa (int x, int N){//初始化queue<int> qu;memset(in, false, sizeof(in) );memset(times, 0, sizeof(times) );memset(dis, 0x3f, sizeof(dis) );//第一个点加入dis[x] = 0;    qu.push(x);    in[x] = true, times[x] ++;    while(qu.size() ){int t = qu.front();qu.pop();in[t] = false;for(int i= 1; i<= N; i++)if(dis[i] > dis[t]+Map[t][i]){dis[i] = dis[t] + Map[t][i];if(!in[i]){qu.push(i);in[i] = true;times[i] ++;if(times[i] >= N) return true;}}    }    return false;}int main (){int F;scanf("%d", &F);while(F--){int N, M, W;scanf("%d %d %d", &N, &M, &W);//初始化memset(Map, 0x3f, sizeof(Map) );for(int i= 1; i<= N; i++)Map[i][i] = 0;for(int i= 0; i< M; i++){int s, e, t;scanf("%d %d %d", &s, &e, &t);if(t < Map[s][e]) Map[s][e] = Map[e][s] = t;}for(int i= 0; i< W; i++){int s, e, t;scanf("%d %d %d", &s, &e, &t);if(-t < Map[s][e] ) Map[s][e] = -t;}int f = spfa(1, N);if(f) puts("YES");else puts("NO");}return 0;}



0 0
原创粉丝点击