Poj 3259 wormholes

Source: Internet
Author: User


Wormholes
Time limit:2000 ms   Memory limit:65536 K
Total submissions:30766   Accepted:11157

Description

While processing his own farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is before you entered the wormhole! Each of FJ's farms comprisesN(1 ≤N≤ 500) fields conveniently numbered 1 ..N,M(1 ≤M≤ 2500) paths, andW(1 ≤W≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: Start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. perhaps he will be able to meet himself :).

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

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 SAnd EThat requires TSeconds to traverse. Two fields might be 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 way 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" (do 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 cocould travel back in time by the cycle 1-> 2-> 3-> 1, arriving back at his starting location 1 second before he leaves. he cocould start from anywhere on the cycle to accomplish this.

Source

Usaco 2006 December gold


Question: there is a two-way positive side and a single negative side (the time of the worm hole is reversed). Ask if you can go back.

Idea: Use the bellman_ford algorithm to determine whether a negative weight ring exists.


#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <cstring>#include <stdlib.h>using namespace std;const int INF=1000000009;struct Node{    int s,e,t;}mp[5205];int d[505];int n,m,w;bool bellman_ford(){    memset(d,INF,sizeof(d));    d[1]=0;    for(int i=1;i<n;i++){        bool flag=true;        for(int j=1;j<=2*m+w;j++){            int a=mp[j].s,b=mp[j].e,c=mp[j].t;            if(d[b]>d[a]+c){                d[b]=d[a]+c;                flag=false;            }        }        if(flag) false;    }    for(int i=1;i<=2*m+w;i++){        int a=mp[i].s,b=mp[i].e,c=mp[i].t;        if(d[b]>d[a]+c)            return false;    }    return true;}int main(){    int t;    scanf("%d",&t);    while(t--){        memset(mp,0,sizeof(mp));        scanf("%d%d%d",&n,&m,&w);        int k=0;        for(int i=1;i<=m;i++){            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            ++k;            mp[k].s=x,mp[k].e=y,mp[k].t=z;            ++k;            mp[k].e=x,mp[k].s=y,mp[k].t=z;        }        for(int i=m+1;i<=m+w;i++){            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            ++k;            mp[k].s=x,mp[k].e=y,mp[k].t=-z;        }        if(!bellman_ford()) printf("YES\n");        else printf("NO\n");    }    return 0;}

Zookeeper
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.