Wormholes-poj 3259 (Bellman-ford algorithm)

Source: Internet
Author: User

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 34934 Accepted: 12752

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 the 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 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, F. FFarm 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 Sand EThat requires TSeconds to traverse. The might is 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-path from STo EThat also moves the traveler back TSeconds.

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 on the cycle to accomplish this. The problem uses the Bellman-ford algorithm
1#include <iostream>2 using namespacestd;3 structFarm {4     intS;5     intE;6     intT;7} f[5500];8 intMain () {9     intnum;Ten     intN, M, W; OneCIN >>num; A     intf[ -]; -      for(inti =0; i < num; i++) { -CIN >> N >> M >>W; the          for(intj =0; J < N; J + +) { -F[J] =20000; -         } -f[0] =0; +          for(intj =0; J < M; J + +) { -             intA, B, C; +Cin >> a >> b >>C; Af[2*J]. S =A; atf[2*J]. Cb; -f[2*J]. T =C; -f[2*j+1]. S =b; -f[2*j+1]. E =A; -f[2*j+1]. T =C; -  in         } -          for(intj =2M J <2*m + W; J + +) { to             intA, B, C; +Cin >> a >> b >>C; -F[J]. S =A; theF[J]. E =b; *F[J]. T =0-C; $         }Panax Notoginseng          for(intj =0; J < N-1; J + +) { -              for(intK =0; K <2*m + W; k++) { the                 if(F[f[k]. E] > F[f[k]. S] +F[k]. T) { +F[F[K]. E] = F[f[k]. S] +F[k]. T; A                 } the             } +         } -         intFlag =0; $          for(intK =0; K <2*m + W; k++) { $             if(F[f[k]. E] >f[f[k]. S] +F[k]. T) { -F[F[K]. E] = F[f[k]. S] +F[k]. T; -flag=1; the                  Break; -             }Wuyi         } the         if(flag) { -cout<<"YES"<<Endl; Wu}Else{ -cout<<"NO"<<Endl; About         } $     } -     return 0; -}

Wormholes-poj 3259 (Bellman-ford algorithm)

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.