zoukankan      html  css  js  c++  java
  • POJ3259——Bellman_foed——Wormholes

    Description

    While exploring his many 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 comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (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 maps to F (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, FF farm descriptions follow. 
    Line 1 of each farm: Three space-separated integers respectively: NM, and W 
    Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and E that requires T seconds 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 (SET) that describe, respectively: A one way path from S to E that 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" (do 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 in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

    Source

    大意:有F个test,共N个编号,M个正向的u,v,t,即从u到v要花t秒,可双向,W个虫洞反向的,只单向,要花-t秒,问最后能不能到达出发之前。
    就有if(d[edge.v]>d[edge.u]+edge.t){
           flag = 0;
          d[edge.v] = d[edge.u]+edge.t;
    }
    意为如果虫洞返回后的时间前于出发的时间,那么满足,经过N此循环都是满足的话,那么就是满足的。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAX = 22222222;
    int F,N,M,W,cnt;
    int d[6000];
    struct edge
    {
        int u;
        int v;
        int t;
    }edge[6000];
    
    bool bellman_ford()
    {
        for(int i = 1; i <= N;i++)
            d[i] = MAX;
        d[1] = 0;
        bool flag;
        for(int i = 1; i <= N;i++){
                flag = 1;
           for(int j = 0; j < cnt; j++){
                if(d[edge[j].v]>d[edge[j].u]+edge[j].t){
                        flag = 0;
                  d[edge[j].v] = d[edge[j].u]+edge[j].t;
             }
          }
        if(flag)
            return true;
       }
       return false;
    }
    
    
    
    int main()
    {
        int u,v,t;
        scanf("%d",&F);
        while(F--){
                cnt = 0;
                scanf("%d%d%d",&N,&M,&W);
                for(int i = 1; i <= M;i++){
                    scanf("%d%d%d",&u,&v,&t);
                    edge[cnt].u = u; edge[cnt].v = v; edge[cnt].t = t;
                    cnt++;
                    edge[cnt].u = v; edge[cnt].v = u; edge[cnt].t = t;
                    cnt++;
                }
                for(int i = 1; i <= W;i++){
                        scanf("%d%d%d",&u,&v,&t);
                        edge[cnt].u = u;edge[cnt].v = v;edge[cnt].t = -t;
                        cnt++;
                }
            if(bellman_ford())
                printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    获取浏览器类型和版本
    js 防抖、节流
    判断数据类型
    一个函数判断数据类型
    面试题3道
    如何处理循环的异步操作
    this的原理以及几种使用场景
    v-model原理解析
    小程序setData数据量过大时候会对渲染有影响吗?
    js中in关键字的使用方法
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4316677.html
Copyright © 2011-2022 走看看