zoukankan      html  css  js  c++  java
  • POJ3259

    题目大意:

    John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。简化下,就是看图中有没有负权环。有的话就是可以,没有的话就是不可以了。

    注意,路是双向的,虫洞是单向的

    代码如下:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN=500000;
    struct edge
    {
        int from,to,cost;
    };
    edge es[MAXN];
    int d[MAXN];
    int F,N,M,W;
    bool find_answer()
    {
        memset(d,0,sizeof(d));
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=2*M+W; j++)
            {
                edge e=es[j];
                if(d[e.to]>d[e.from]+e.cost)
                {
                    d[e.to]=d[e.from]+e.cost;
                    if(i==N)  return true;//如果第N次仍更新了,则存在负圈
                }
            }
        }
        return false;
    }
    int main()
    {
        scanf("%d",&F);
        while(F--)
        {
            int x,y,z;
            scanf("%d%d%d",&N,&M,&W);
            for(int i=1; i<=M; i++)
            {
                scanf("%d%d%d",&x,&y,&z);
              es[i].from=es[i+M].to=x;
              es[i].to=es[i+M].from=y;
              es[i].cost=es[i+M].cost=z;
            }
            for(int i=1; i<=W; i++)
            {
                scanf("%d%d%d",&x,&y,&z);
                es[2*M+i].from=x;
                es[2*M+i].to=y;
                es[2*M+i].cost=-z;
            }
            if(find_answer()) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    hdu 1312 Red and Black
    hdu 1573 X问题
    广工校赛决赛之简单的数论题
    最大的LeftMax与rightMax之差绝对值
    POJ 2385 Apple Catching
    hdu 1171 Big Event in HDU
    ACM比赛经验
    BestCoder Valentine's Day Round
    使用ffmpeg进行视频封面截取
    使用ODP.NET连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/Zeroinger/p/5493937.html
Copyright © 2011-2022 走看看