• hdu 4289 最小割,分拆点为边


    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2609

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <vector>
    
    #define maxn 450
    #define maxe 100000 
    using namespace std;
    
    const int INF = 0x3f3f3f;
    
    struct Edge{
        int from,to,cap,flow;
        int next;
    };
    
    struct Dinic{
        int s,t;
        int head[maxn];
        int cur[maxn];
        Edge edges[maxe];
        int d[maxn];
        bool vis[maxn];
        int cnt;
        
           void init(){
             memset(head,-1,sizeof(head));
             cnt = 0;      
        }
        void addedge(int from,int to,int cap){
            edges[cnt].from = from; edges[cnt].to = to;   edges[cnt].cap = cap; 
            edges[cnt].flow = 0   ; edges[cnt].next = head[from];  head[from] = cnt++;
            edges[cnt].from = to  ; edges[cnt].to = from; edges[cnt].cap = 0; 
            edges[cnt].flow = 0   ; edges[cnt].next = head[to];  head[to] = cnt++;
        }
        bool bfs(){
            memset(vis,0,sizeof(vis)); 
            queue<int> Q;
            Q.push(s);  
            vis[s] = true;  
            d[s] = 0;
            while(!Q.empty()){
                int u = Q.front();  Q.pop(); 
                for(int i=head[u];i!=-1;i=edges[i].next){
                    Edge& e = edges[i];    
                    if(!vis[e.to] && e.cap>e.flow){
                        vis[e.to] = true;
                        d[e.to] = d[e.from] + 1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
        int dfs(int u,int res){
            if( u == t ||  res == 0)   return res; 
            int flow = 0,f;
            for(int& i=cur[u];i!=-1;i=edges[i].next){   //还不是很理解到cur[]的作用; 
                Edge& e = edges[i];
                if(d[e.to] == d[e.from] + 1 && (f = dfs(e.to,min(res,e.cap-e.flow)))>0){
                    e.flow += f;
                    edges[i^1].flow -= f;  
                    flow += f;
                    res -= f;
                    if(res == 0) break; 
                } 
            }
            return flow;
        }
        int Maxflow(int S,int T){
            s = S; t = T;
            int flow = 0;
            while(bfs()){
                for(int i=s;i<=t;i++)  cur[i] = head[i];
                flow += dfs(s,INF);   
            }
            return flow;
        }
    }solver;
    
    int main()
    {
        //if(freopen("input.txt","r",stdin)== NULL)  {printf("Error
    "); exit(0);}
        
        int N,M,S,T;
        while(scanf("%d%d",&N,&M) == 2){
            scanf("%d%d",&S,&T);
            solver.init();
            int s,t;
            s = 0;  t = 2*N + 1;
            solver.addedge(s,S,INF);
            solver.addedge(T+N,t,INF);  //
            for(int i=1;i<=N;i++){
                int cost;
                scanf("%d",&cost);
                solver.addedge(i,i+N,cost);
            }
            for(int i=1;i<=M;i++){  
                int a,b;
                scanf("%d %d",&a,&b);
                solver.addedge(b+N,a,INF);
                solver.addedge(a+N,b,INF);
            }
            printf("%d
    ",solver.Maxflow(s,t));
        }
    }
    View Code
  • 相关阅读:
    2246 练习sql语句(上)
    202242 mysql卸载干净
    22412 mysql报错编号1045(下)
    22410 理解接口的含义?
    22410 express.js接口
    2243 mysql管理工具sqlyog错误号码篇(上)
    22331 提交项目代码git的一些补充(拒绝合并)
    22412 sql语句报错编号记录(上)+sql语法
    2022331 express app.listen引发的思考(1)
    202242 mysql初识
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3227634.html
Copyright © 2020-2023  润新知