• (简单) POJ 3259 Wormholes,SPFA判断负环。


      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..N, M (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.

      就是求是否存在负环,这样的话一定能够时间倒退。。。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    
    using namespace std;
    
    const int INF=10e8;
    const int MaxN=510;
    
    struct Edge
    {
        int v,cost;
    
        Edge(int _v,int _cost):v(_v),cost(_cost) {}
    };
    
    vector <Edge> E[MaxN];
    bool vis[MaxN];
    int couNode[MaxN];
    
    bool SPFA(int lowcost[],int n,int start)
    {
        queue <int> que;
        int u,v,c;
        int len;
    
        for(int i=1;i<=n;++i)
        {
            lowcost[i]=INF;
            vis[i]=0;
            couNode[i]=0;
        }
        vis[start]=1;
        couNode[start]=1;
        lowcost[start]=0;
    
        que.push(start);
    
        while(!que.empty())
        {
            u=que.front();
            que.pop();
    
            vis[u]=0;
            len=E[u].size();
    
            for(int i=0;i<len;++i)
            {
                v=E[u][i].v;
                c=E[u][i].cost;
    
                if(lowcost[u]+c<lowcost[v])
                {
                    lowcost[v]=lowcost[u]+c;
    
                    if(!vis[v])
                    {
                        vis[v]=1;
                        ++couNode[v];
                        que.push(v);
    
                        if(couNode[v]>=n)
                            return 0;
                    }
                }
            }
        }
    
        return 1;
    }
    
    inline void addEdge(int u,int v,int c)
    {
        E[u].push_back(Edge(v,c));
    }
    
    int ans[MaxN];
    
    int main()
    {
        int T;
        int N,M,W;
        int a,b,c;
    
        scanf("%d",&T);
    
        while(T--)
        {
            scanf("%d %d %d",&N,&M,&W);
    
            for(int i=1;i<=N;++i)
                E[i].clear();
    
            for(int i=1;i<=M;++i)
            {
                scanf("%d %d %d",&a,&b,&c);
    
                addEdge(a,b,c);
                addEdge(b,a,c);
            }
    
            for(int i=1;i<=W;++i)
            {
                scanf("%d %d %d",&a,&b,&c);
    
                addEdge(a,b,-c);
            }
    
            if(SPFA(ans,N,1))
                printf("NO
    ");
            else
                printf("YES
    ");
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    一个用php抓取网页中电子邮箱的实例
    jQuery中ajax加载文本
    phantomjs介绍(js网页截屏、javascript网页解析渲染工具)
    delphi 如何获取大于2G的物理内存大小[delphi]
    DELPHI——过程与函数[delphi]
    Delphi初始化和析构 initialization和finalization
    Delphi执行DOS命令显示到文本框
    MYSQL基础集合函数(count,sun,avg,max,min)
    MYSQL基础链接查询(内连接查询,外连接查询)
    MYSQL基础合并查询结果(UNION)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338558.html
Copyright © 2020-2023  润新知