POJ 3259 wormholes (Bellman_ford algorithm awarded negative ring)

Source: Internet
Author: User
Tags stdin

Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32393 Accepted: 11771

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



Test instructions: John has a farm of N, there are M roads between all the farms, and each road has the time it takes to pass. One day, he was wandering around the farm, unexpectedly found a W worm hole (estimated that the child is good at primary school physics ~ ~ ~), each wormhole has a backward time, he would like to use these wormhole back to the past. Ask him if he can achieve this magical journey.


Analysis: Is the simple judgment whether there is a negative ring, because if there is a negative ring, it represents the return to the origin of the time before the departure, this will be before. But pay special attention to the road is two-way, but the wormhole is one-way!!! Level of the array must be open enough, or re, because the road is two-way, so the need to save the road 2*max_e, plus max_w a worm hole, a total need to open to 2*max_e + Max_w to be able.


Ps:bellman_ford algorithm to judge negative ring there are two methods, but the essence of both are the same, do not tangle, if directly just judge whether there is no negative ring, or version two good to write a little.


AC Code:

Negative ring Version One:

#include <cstdio> #include <iostream> #include <algorithm>using namespace std; #define INF 1234567# Define Max_v 505#define max_e 5205struct edge{int S, E, W;};    Edge Es[max_e];int d[max_v];int E, V;bool bellman_ford (int s) {for (int i=1; i<=v; i++) d[i] = (i==s)? 0:inf;        for (int i=0; i<v-1; i++) {int flag = 0;            for (int j=0; j<e; J + +) {edge now = es[j];                if (D[NOW.E] > D[NOW.S] + now.w) {D[NOW.E] = D[now.s] + now.w;            flag = 1;    }} if (flag = = 0) break;        } for (int i=0; i<e; i++) {//award negative rim edge now = es[i];    if (D[NOW.E] > D[NOW.S] + now.w) return true; } return false;}    int main () {//Freopen ("In.txt", "R", stdin);    int T, N, M, W, S, e, time;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &m, &w);                             V = n;                           Top point E = 0;  The number of sides for (int i=0; i<m; i++) {//Road is bidirectional scanf ("%d%d%d", &s, &e, &time);            ES[E].S = s;            ES[E].E = E;            ES[E++].W = time;            ES[E].S = E;            ES[E].E = s;        ES[E++].W = time;            } for (int i=0; i<w; i++) {//wormholes are unidirectional!!!            scanf ("%d%d%d", &s, &e, &time);            ES[E].S = s;            ES[E].E = E;        ES[E++].W =-time; } printf ("%s\n", flag?)    "YES": "NO"); } return 0;}


Negative Ring version two:

#include <cstdio> #include <iostream> #include <algorithm>using namespace std; #define INF 1234567# Define Max_v 505#define max_e 5205struct edge{int S, E, W;};    Edge Es[max_e];int d[max_v];int E, V;bool bellman_ford (int s) {for (int i=1; i<=v; i++) d[i] = (i==s)? 0:inf;            for (int i=0, i<v; i++) {for (int j=0; j<e; J + +) {edge now = es[j];                if (D[NOW.E] > D[NOW.S] + now.w) {D[NOW.E] = D[now.s] + now.w;            if (i = = V-1) return true; }}} return false;}    int main () {//Freopen ("In.txt", "R", stdin);    int T, N, M, W, S, e, time;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n, &m, &w);        V = n;        E = 0;            for (int i=0; i<m; i++) {//Road is bidirectional scanf ("%d%d%d", &s, &e, &time);            ES[E].S = s;            ES[E].E = E;            ES[E++].W = time;            ES[E].S = E;           ES[E].E = s; ES[E++].W = time;            } for (int i=0; i<w; i++) {//wormholes are unidirectional!!!            scanf ("%d%d%d", &s, &e, &time);            ES[E].S = s;            ES[E].E = E;        ES[E++].W =-time; } printf ("%s\n", Bellman_ford (1)?    "YES": "NO"); } return 0;}


Small Package Benefits: If you want to find out all the negative ring, just want to be in version two of the D array is initialized to 0 can ~ ~ ~




POJ 3259 wormholes (Bellman_ford algorithm 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.