POJ 3259 wormholes (awarded negative ring)

Source: Internet
Author: User

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 46123   Accepted: 17033

Description

While exploring he many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it's a one-way path that delivers the IT destination at a time that's 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 a avid time-traveling fan, he wants to does the following:start at some field, travel through some paths and worm Holes, and return to the starting field a time before his initial departure. Perhaps he'll be able to meet himself:).

To help FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤ F ≤5) of his farms. No paths'll take longer than seconds to travel and no wormhole can bring FJ back in time by more than-seco Nds.

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. The might is 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-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" (does 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 on time by the cycle 1->2->3->1, arriving back at he starting location 1 second Before he leaves. He could start from anywhere in the cycle to accomplish this.

Source

Usaco 2006 December Gold

Main topic:
FJ has n farm, numbered 1 to N, this n farm by M Road and W Wormhole Connection, no road is bidirectional, the weight is T1, indicating that through each road needs to spend T1 time units, each wormhole is unidirectional, the weight of T2, Passing through each wormhole will allow you to return to the T2 time unit (which is plainly the time to turn back); now ask you, FJ want to start from farm 1th, after a number of farms, a certain time before their departure to return to farm No. 1th. Now ask if you can make it.
Analysis:
We see the weight on the wormhole as negative, so the question becomes whether there is a negative ring in the farm.

 

Simple SPFA run the shortest way, sentenced to a point in the queue more than n times, there is a negative ring, slower.

Write a quick, DFS version of SPFA here. See the code.

AC Code:

#include <cstdio>#include<cstring>#include<algorithm>#defineR Registerusing namespaceStd;inlineintRead () {Rintx=0;BOOLf=1; RCharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=0; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x= (x<<3) + (x<<1) +ch-'0'; ch=GetChar ();} returnf?x:-x;}Const intn=1e5+Ten;intt,n,m1,m2;structnode{intV,w,next;} E[n<<1];intTot,head[n],dis[n];BOOLCan,flag[n];voidAddintXintYintz) {e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;}voidSPFA (intx) {Flag[x]=1;  for(intI=head[x];i;i=E[i].next) {        intv=e[i].v,w=E[I].W; if(dis[v]>dis[x]+W) {            if(flag[v]| | Can) {can=1; Break;} DIS[V]=dis[x]+W;        SPFA (v); }} Flag[x]=0;}voidCl () {can=0; tot=0; memset (DIS,0,sizeofDIS);//Note not INFMemset (Head,0,sizeofhead); memset (Flag,0,sizeofflag);}intMain () {T=read ();  while(t--) {Cl (); N=read (); M1=read (); m2=read ();  for(intI=1, x,y,z;i<=m1;i++) {x=read (); Y=read (); z=read ();            Add (x, y, z);        Add (y,x,z); }         for(intI=1, x,y,z;i<=m2;i++) {x=read (); Y=read (); z=read (); Add (x, Y,-z); }         for(intI=1; i<=n;i++) {SPFA (i); if(CAN) Break; } puts (can?"YES":"NO"); }    return 0;}

 

 

POJ 3259 wormholes (awarded negative ring)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.