zoukankan      html  css  js  c++  java
  • BellmanFord模版

    struct Edge{
        int from,to;
        int dist;
    };
    struct BellmanFord{
        int n,m;
        vector<Edge>edges;
        vector<int>G[MAXN];
        bool inq[MAXN];
        int d[MAXN];
        int p[MAXN];
        int cnt[MAXN];
        void init(int n)
        {
            this->n=n;
            for(int i=0;i<n;i++)G[i].clear();
            edges.clear();
        }
        void AddEdge(int from,int to,int dist)
        {
            edges.push_back((Edge){from,to,dist});
            m=edges.size();
            G[from].push_back(m-1);
        }
        bool negativeCycle()
        {
            queue<int>Q;
            memset(inq,0,sizeof(inq));
            memset(cnt,0,sizeof(cnt));
            for(int i=0;i<n;i++)
            {
            d[i]=0;inq[0]=true;Q.push(i);
            }
            while(!Q.empty())
            {
                int u=Q.front();Q.pop();
                inq[u]=false;
                for(int i=0;i<G[u].size();i++)
                {
                    Edge& e=edges[G[u][i]];
                    if(d[e.to]>d[u]+e.dist)
                    {
                        d[e.to]=d[u]+e.dist;
                        p[e.to]=G[u][i];
                        if(!inq[e.to])
                        {
                            Q.push(e.to);
                            inq[e.to]=true;
                            if(++cnt[e.to]>n)
                                return true;
                        }
                    }
                }
            }
            return false;
        }
    };
  • 相关阅读:
    「CodeForces
    「POJ
    「CodeForces
    「CodeForces
    【CodeForces 717C】Potions Homework
    【CodeForces 730H】Car Repair Shop
    【CodeForces 730H】Delete Them
    【Gym 100947I】What a Mess
    j
    PDE工具箱的简单使用
  • 原文地址:https://www.cnblogs.com/arbitrary/p/2912643.html
Copyright © 2011-2022 走看看