Poj 3259 wormholes

Source: Internet
Author: User

1.Link:

http://poj.org/problem?id=3259

2.Content:

Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32078 Accepted: 11651

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 in the cycle to accomplish this.

Source

Usaco 2006 December Gold

3.Method:

The template title of the Bellman algorithm is mainly used to find out if the negative ring exists and has nothing to do with the initial value of dis.

4.Code:

1#include <iostream>2#include <cstring>3 4 using namespacestd;5 6 //const int max_d = 0;7 8 structRoad9 {Ten     ints; One     inte; A     intT; - }; -  the intMain () - { -     //freopen ("D://input.txt "," R ", stdin); -  +     inti,j; -  +     intff; ACIN >>ff; at  -     intn,m,w; -      while(ff--) -     { -CIN >> n >> M >>W; -  inRoad *arr_road =NewRoad[m *2+W]; -  to         ints,e,t; +          for(i =0; I < m; ++i) -         { theCIN >> s >> e >>T; *              $Arr_road[i *2].S =s;Panax NotoginsengArr_road[i *2].E =e; -Arr_road[i *2].t =T; the  +Arr_road[i *2+1].S =e; AArr_road[i *2+1].E =s; theArr_road[i *2+1].t =T; +         } -          for(i =0; i < W; ++i) $         { $CIN >> s >> e >>T; -  -Arr_road[m *2+ I].S =s; theArr_road[m *2+ I].E =e; -Arr_road[m *2+ i].t =-T;Wuyi         } the  -         //For (i = 0; i < m * 2 + W; ++i) Wu         //{ -         //cout << i << "<< arr_road[i].s <<" "<< arr_road[i].e <<" "<< arr_ road[i].t << Endl; About         //} $         //cout << Endl; -  -         int*arr_d =New int[n]; -         //For (i = 0; i < n; ++i) arr_d[i] = max_d; Amemset (Arr_d,0,sizeof(int) *n); +  the         //Bellman-ford -         BOOLFlag; $          for(i =0; I < n-1; ++i) the         { theFlag =false; the              for(j =0; J <2* m + W; ++j) the             { -                 if(ARR_D[ARR_ROAD[J].E] > ARR_D[ARR_ROAD[J].S] +arr_road[j].t) in                 { theARR_D[ARR_ROAD[J].E] = Arr_d[arr_road[j].s] +arr_road[j].t; theFlag =true; About                 } the             } the             if(!flag) Break; the         } +  -          for(j =0; J <2* m + W; ++j) the         {Bayi             if(ARR_D[ARR_ROAD[J].E] > Arr_d[arr_road[j].s] + arr_road[j].t) Break; the         } the  -         if(J <2* m + W) cout <<"YES"<<Endl; -         Elsecout <<"NO"<<Endl; the  the Delete [] arr_d; the Delete [] arr_road; the     } -  the     return 0; the}

5.Reference:

Poj 3259 wormholes

Related Keywords:

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.