POJ 3259 wormholes

Source: Internet
Author: User
Tags printf time limit
Wormholes
Time limit:2000ms Memory limit:65536k
Total submissions:35456 accepted:12955

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 the FJ find out whether this is possible or not, he'll supply you with complete maps to F (1≤f≤5) of the 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. F Farm 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 Betwe En 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 (S, E, T) that describe, respectively:a one path paths from S To E this 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

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

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.

Read a long time of the problem also did not understand,,,, and finally read the online problem-solving report to read the code to understand, just read test instructions ....

Test instructions is:

First enter the number of cases, and then enter 3 number, V,E,W, indicating that the figure has a V point, e-bar, W time retrograde channel

The next E line enters the specific edge, From,to,time, from the from point to the to point takes time, here is the weight value,

It is important to note that the edges here are non-2*e, that is, there are edges of the bar,

Then you enter the edge of the wormhole, and the worm Hole has a w bar,

Note that the side of this back is unidirectional, because the entrance to the retrograde passage of time will only be at one end of the channel, and at the other end there is no way to reverse the clock.


Let's introduce the negative weights diagram:

(from Baidu know)

For example, there is a 3-point number, and a single edge exists between any two points, so there is a ring in three-way. (the definition of the ring here is not very rigorous, with the direction of the words need to distinguish with the circle)
Each edge has a side right.
We make g (i,j) represent the weights of the edges between i,j.
When G (+g) (2,3) +g (3,1) <0 is a negative weight ring.
When G (+g) (2,3) +g (3,1) >0, it is a positive weight ring.

If there is a negative weight map, it means that you can go back to the origin at one point and make time go back to any previous moment before you can see yourself.




#include <stdio.h> #include <string.h> #define INF 100000 int d[10005];
int n, m;
    struct Edge {int from;//from vertex from point to vertex to the weight of the cost of the other int to;

int cost;//weight}edges[50005];
    int Bellman_ford (int m) {int i,j,k,t;
    int OK;
    for (I=1; i<=n; i++) d[i] = inf;
    D[1] = 0;
        for (I=1; i<=n-1; i++) {ok = 1;
                for (j=1; j<=m; J + +) {if (D[edges[j].from] > d[edges[j].to] + edges[j].cost) {
                D[edges[j].from] = d[edges[j].to] + edges[j].cost;
            OK = 0;
    }} if (OK) break;
    } for (j=1; j<=m; J + +) {if (D[edges[j].from] > d[edges[j].to] + edges[j].cost) return 0;
} return 1;
    } int main () {int I, J, T, X;
    int A, B, C, W;
    Freopen ("A.txt", "R", stdin);
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &n, &m, &w); memset (edges, 0, sizeof (EDges));
            For (i=0, j=1; j<=m;j++) {scanf ("%d%d%d", &a, &b, &c);
            i++;
            Edges[i].from = A;
            Edges[i].to = b;
            Edges[i].cost = C;
            i++;
            Edges[i].from = b;
            Edges[i].to = A;

        Edges[i].cost = C;

            } for (j=1; j<=w; J + +) {scanf ("%d%d%d", &a, &b, &c);
            i++;
            Edges[i].from = A;
            Edges[i].to = b;

        Edges[i].cost =-C;
        }//printf ("%d\n", I);
        if (Bellman_ford (i)) printf ("no\n");
    else printf ("yes\n");
    }//getch ();
return 0;
 }


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.