zoukankan      html  css  js  c++  java
  • spfa判断负环

    spfa判断负环

    spfa+bfs

    #include<iostream>
    #include<cstdio>
    #include<queue> 
    #include<cstring>
    using namespace std;
    const int maxn=200010;
    const int inf=0x7fffffff;
    int t,n,m,tot,d[maxn],dis[maxn],head[maxn];
    bool flag[maxn];
    queue<int> q;
    struct node
    {
        int to;
        int w;
        int next;
    }e[maxn*2];
    void prepare()
    {
        memset(head,0,sizeof(head));
        memset(flag,0,sizeof(flag));
        memset(dis,127/3,sizeof(dis));
        memset(d,0,sizeof(d));
        tot=0;dis[1]=0;
    }
    void add_edge(int u,int v,int w)
    {
        e[++tot].to=v;
        e[tot].w=w;
        e[tot].next=head[u];
        head[u]=tot;
    }
    bool spfa()
    {
        q.push(1);flag[1]=1;d[1]=1;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();flag[u]=0;
            for(int i=head[u];i;i=e[i].next)
            {
                int v=e[i].to;
                if(dis[v]>dis[u]+e[i].w)
                {
                    dis[v]=dis[u]+e[i].w;
                    d[v]++;
                    if(d[v]>=n) return 1;
                    if(!flag[v])
                    {
                        flag[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        return 0;
    }
    int main()
    {
        int x,y,z;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            prepare();
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&x,&y,&z);
                add_edge(x,y,z);
                if(z>=0) add_edge(y,x,z);
            }
            if(spfa()) printf("YE5
    ");
            else printf("N0
    ");
        }
        return 0;
    }

    时间较长,容易超时。
    spfa+dfs

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=400010;
    int t,n,m,tot,head[maxn],dis[maxn];
    bool can,flag[maxn];
    struct node
    {
        int to;
        int w;
        int next;
    }e[maxn];
    void prepare()
    {
        memset(flag,0,sizeof(flag));
        memset(dis,0,sizeof(dis));
        memset(head,0,sizeof(head));
        tot=0;can=0;
    }
    void add_edge(int u,int v,int w)
    {
        e[++tot].to=v;
        e[tot].w=w;
        e[tot].next=head[u];
        head[u]=tot;
    }
    void spfa(int u)
    {
        flag[u]=1;
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].to;
            if(dis[v]>dis[u]+e[i].w)
            {
                if(flag[v]||can)
                {can=1;break;}
                dis[v]=dis[u]+e[i].w;
                spfa(v);
            }
        }
        flag[u]=0;
    }
    int main()
    {
        int x,y,z;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            prepare();
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&x,&y,&z);
                add_edge(x,y,z);
                if(z>=0) add_edge(y,x,z);
            }
            for(int i=1;i<=n;i++)
            {spfa(i);if(can)break;}
            if(can) printf("YE5
    ");
            else printf("N0
    ");
        }
        return 0;
    }

    速度快。
    注意事项:
    两种写法大体相同,dis数组的初始值不同,需要注意。

  • 相关阅读:
    HDU 2544 最短路
    HDU 3367 Pseudoforest
    USACO 2001 OPEN
    HDU 3371 Connect the Cities
    HDU 1301 Jungle Roads
    HDU 1879 继续畅通工程
    HDU 1233 还是畅通工程
    HDU 1162 Eddy's picture
    HDU 5745 La Vie en rose
    HDU 5744 Keep On Movin
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070873.html
Copyright © 2011-2022 走看看