zoukankan      html  css  js  c++  java
  • 混合图欧拉回路/路径

    混合图 : 同时包含有有向边以及无向边的图

    求解混合图的欧拉路径/回路问题需要先了解 

    有向or无向图的欧拉回路/路径问题 ==> Click here

    图论最大流算法 ==> Click here (误)

    好了!接下来就是混合图欧拉回路/路径的算法讲解了

    好吧,其实我并不会讲,但是找到了一个很详细且容易理解的讲解

    混合图欧拉回路/路径讲解 ==> Click here 没错、就是 kuangbin 大神!

    以下给出模板 POJ 1637

    #include<stdio.h>
    #include<string.h>
    #include<vector>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int maxn = 2e4 + 10;
    const int INF = 0x3f3f3f3f;
    struct Edge
    {
        int from,to,cap,flow;
        Edge(){}
        Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
    };
    
    struct Dinic
    {
        int n,m,s,t;            //结点数,边数(包括反向弧),源点与汇点编号
        vector<Edge> edges;     //边表 edges[e]和edges[e^1]互为反向弧
        vector<int> G[maxn];    //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
        bool vis[maxn];         //BFS使用,标记一个节点是否被遍历过
        int d[maxn];            //d[i]表从起点s到i点的距离(层次)
        int cur[maxn];          //cur[i]表当前正访问i节点的第cur[i]条弧
    
        void init(int n,int s,int t)
        {
            this->n=n,this->s=s,this->t=t;
            for(int i=0;i<=n;i++) G[i].clear();
            edges.clear();
        }
    
        void AddEdge(int from,int to,int cap)
        {
            edges.push_back( Edge(from,to,cap,0) );
            edges.push_back( Edge(to,from,0,0) );
            m = edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
    
        bool BFS()
        {
            memset(vis,0,sizeof(vis));
            queue<int> Q;//用来保存节点编号的
            Q.push(s);
            d[s]=0;
            vis[s]=true;
            while(!Q.empty())
            {
                int x=Q.front(); Q.pop();
                for(int i=0; i<G[x].size(); i++)
                {
                    Edge& e=edges[G[x][i]];
                    if(!vis[e.to] && e.cap>e.flow)
                    {
                        vis[e.to]=true;
                        d[e.to] = d[x]+1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
    
        //a表示从s到x目前为止所有弧的最小残量
        //flow表示从x到t的最小残量
        int DFS(int x,int a)
        {
            if(x==t || a==0)return a;
            int flow=0,f;//flow用来记录从x到t的最小残量
            for(int& i=cur[x]; i<G[x].size(); i++)
            {
                Edge& e=edges[G[x][i]];
                if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 )
                {
                    e.flow +=f;
                    edges[G[x][i]^1].flow -=f;
                    flow += f;
                    a -= f;
                    if(a==0) break;
                }
            }
            return flow;
        }
    
        int Maxflow()
        {
            int flow=0;
            while(BFS())
            {
                memset(cur,0,sizeof(cur));
                flow += DFS(s,INF);
            }
            return flow;
        }
    }DC;
    
    int N, M;
    int IN[maxn], OUT[maxn];
    int main(void)
    {
        int nCase;
        scanf("%d", &nCase);
        while(nCase--){
            scanf("%d %d", &N, &M);
    
            DC.init(N+2, 0, N+1);
            memset(IN, 0, sizeof(IN));
            memset(OUT, 0, sizeof(OUT));
    
            int u, v, w;
            while(M--){
                scanf("%d %d %d", &u, &v, &w);
                OUT[u]++, IN[v]++;
                if(!w) DC.AddEdge(u, v, 1);
            }
    
            int MX_FLOW = 0;
            bool ok = true;
            for(int i=1; i<=N; i++){
                if((OUT[i]-IN[i]) & 1){ ok = false; break; }
                if(OUT[i] - IN[i] > 0) DC.AddEdge(0, i, (OUT[i]-IN[i])>>1), MX_FLOW += (OUT[i]-IN[i])>>1;
                else if(IN[i] - OUT[i] > 0) DC.AddEdge(i, N+1, (IN[i]-OUT[i])>>1);
            }
    
            if(!ok){
                puts("impossible");
                continue;
            }
    
            if(DC.Maxflow() != MX_FLOW) ok = false;
    
            if(ok) puts("possible");
            else puts("impossible");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Vue实现上下整屏滚动效果
    Vue实现选项卡切换效果
    Vue中在main.js中引入字体图标问题
    Vue中的生命周期beforeDestory不触发问题
    Vue中解决移动端点击300毫秒延迟的问题
    Vue中全局和按需引入Echarts
    Vue组件间的通信
    Echarts图表属性设置
    JZOJ 6799. 【2014广州市选day2】game
    JZOJ 6798. 【2014广州市选day2】regions
  • 原文地址:https://www.cnblogs.com/qwertiLH/p/8783585.html
Copyright © 2011-2022 走看看