Wormholes (Shortest _ bellman_ford)

Source: Internet
Author: User
Wormholes
Time limit:2000 ms   Memory limit:65536 K
Total submissions:31762   Accepted:11561

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

NO
Yes

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. john's farm has n places, m links are connected to two places, W wormhole holes, and the wormhole holes are a one-way road that will send you to the destination before you leave, that is, TS will be regressed in the past. Our task is to know if we will return again after starting from a certain place and see ourselves before leaving. Idea: Check whether there is a negative ring. If there is a negative ring, it will prove that yes can be returned, and no can be output if there is no negative ring. You can use bellmanford to determine the negative ring.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;#define inf 0x3f3f3f3fstruct node{    int u,v,w;} edge[6010];int dis[510];int cnt;int n,m,W;void add_edge(int u,int v,int w){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].w=w;    cnt++;}int bellman_ford(){    int i,j;    for(i=1; i<=n; i++)        dis[i]=inf;    dis[1]=0;    for(i=1; i<n; i++)    {        int flag=0;        for(j=0; j<cnt; j++)        {            if(dis[edge[j].v]>dis[edge[j].u]+edge[j].w)            {                dis[edge[j].v]=dis[edge[j].u]+edge[j].w;                flag=1;            }        }        if(!flag)            break;    }    for(i=0; i<cnt; i++)        if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w)            return 1;    return 0;}int main(){    int T;    int u,v,w;    scanf("%d",&T);    while(T--)    {        cnt=0;        scanf("%d %d %d",&n,&m,&W);        while(m--)        {            scanf("%d %d %d",&u,&v,&w);            add_edge(u,v,w);            add_edge(v,u,w);        }        while(W--)        {            scanf("%d %d %d",&u,&v,&w);            add_edge(u,v,-w);        }        if(bellman_ford())            printf("YES\n");        else            printf("NO\n");    }    return 0;}

Zookeeper

Wormholes (Shortest _ bellman_ford)

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.