zoukankan      html  css  js  c++  java
  • POJ3259(ford判环)

    Wormholes
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 39078   Accepted: 14369

    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

    题意:每个农场有N各区域,连接所有区域的是M个双向路径和W个单向时空隧道,从S->E若为路径则花费T秒,若为时空隧道则倒退T秒。问是否可以从某点出发,转一圈回来,回到出发时刻之前。
    思路:因为时空隧道实现倒退,所以将其权值设为负值,利用ford判断是否存在负环。
    #include"cstdio"
    #include"cstring"
    using namespace std;
    const int MAXN=10005;
    const int INF=0x3fffffff;
    struct Edge{
        int from,to,cost;
    }es[MAXN];
    int N,M,W;
    int E;
    int d[MAXN];
    bool ford(int s)
    {
        for(int i=1;i<=N;i++)    d[i]=INF;
        d[s]=0;
        
        int n=N;
        while(n--)
        {
            bool update=false;
            for(int i=0;i<E;i++)
            {
                Edge e=es[i];
                if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost)
                {
                    d[e.to]=d[e.from]+e.cost;
                    update=true;
                }
            }
            if(!update)    break;
            
        }
        
        if(n==-1)    return true;
        else return false;
    }
    int main()
    {
        int F;
        scanf("%d",&F);
        while(F--)
        {
            E=0;
            scanf("%d%d%d",&N,&M,&W);
            for(int i=0;i<M;i++)
            {
                int u,v,c;
                scanf("%d%d%d",&u,&v,&c);
                es[E].from=u,es[E].to=v,es[E++].cost=c;
                es[E].from=v,es[E].to=u,es[E++].cost=c;
            }
            for(int i=0;i<W;i++)
            {
                int u,v,c;
                scanf("%d%d%d",&u,&v,&c);
                es[E].from=u,es[E].to=v,es[E++].cost=-c;//倒退c秒 
            }
            
            if(ford(1))    printf("YES
    ");
            else printf("NO
    ");
        }    
        
        return 0;
    }

     spfa+前向星可解决重边问题

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int MAXN=505;
    const int INF=0x3f3f3f3f;
    struct Edge{
        int v,w,next;
    }es[6006];
    int head[MAXN],tot;
    void addedge(int u,int v,int w)
    {
        es[tot].v=v;
        es[tot].w=w;
        es[tot].next=head[u];
        head[u]=tot++;
    }
    int d[MAXN],vis[MAXN],cnt[MAXN];
    int n,m,k;
    bool spfa(int s)
    {
        for(int i=1;i<=n;i++)
        {
            d[i]=INF;
            vis[i]=0;
            cnt[i]=0;
        }
        d[s]=0;
        queue<int> que;
        que.push(s);
        vis[s]=1;
        cnt[s]++;
        while(!que.empty())
        {
            int u=que.front();que.pop();
            vis[u]=0;
            for(int i=head[u];i!=-1;i=es[i].next)
            {
                Edge e=es[i];
                if(d[e.v]>d[u]+e.w)
                {
                    d[e.v]=d[u]+e.w;
                    if(!vis[e.v])
                    {
                        vis[e.v]=1;
                        que.push(e.v);
                        cnt[e.v]++;
                        if(cnt[e.v]>=n)    return true;
                    }
                }            
            }
        }
        return false;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            memset(head,-1,sizeof(head));
            tot=0;
            scanf("%d%d%d",&n,&m,&k);
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            for(int i=0;i<k;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,-w);
            }
            if(spfa(1))    printf("YES
    ");
            else    printf("NO
    ");    
        }
        return 0;
    }
  • 相关阅读:
    Win7 VSCode 在线安装Rust语言及环境配置
    Win7 VSCode 离线安装Rust语言及环境配置
    Win7崩溃程序目录
    fatal error C1047: The object or library file xxx was created with an older compiler than other objects
    Notepad++正则表达式合并多行代码为1行
    Win7 VS2019安装后创建C++工程失败解决
    关于Visual Studio中书签Bookmark的一些问题
    Fira Code,可以让不等号!=直接显示出来的字体
    免费商用字体
    Win7 64位注册32位DLL
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5149893.html
Copyright © 2011-2022 走看看