zoukankan      html  css  js  c++  java
  • poj 1149 最大流

    题目链接:http://poj.org/problem?id=1149

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <vector>
    
    #define maxn 1050
    #define maxe 200000
    using namespace std;
    
    const int INF = 0x3f3f3f;
    
    struct Edge{
        int u,v,flow,cap;
        int next;
        Edge(int u=0,int v=0,int flow=0,int cap=0,int next=0):
             u(u),v(v),flow(flow),cap(cap),next(next) { }
    };
    
    
    struct Dinic{
        int s,t;
        int d[maxn];
        int cur[maxn];
        bool vis[maxn];
        Edge edges[maxe];
        int head[maxn],cnt;
    
        void init(){
            memset(head,-1,sizeof(head));
            cnt = 0;
        }
    
        void addedge(int u,int v,int cap){
            edges[cnt] = Edge(u,v,0,cap,head[u]);
            head[u] = cnt++;
            edges[cnt] = Edge(v,u,0,0,head[v]);
            head[v] = 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.v] && e.cap > e.flow){
                        vis[e.v] = true;
                        d[e.v] = d[e.u] + 1;
                        Q.push(e.v);
                    }
                }
            }
            return vis[t];
        }
    
        int dfs(int u,int res){
            if(u == t || res == 0)  return  res;  //res 残流大小;
            int flow = 0,f;
            for(int& i=cur[u];i!=-1;i=edges[i].next){
                Edge& e = edges[i];
                if(d[e.v] == d[e.u] + 1 && (f = dfs(e.v,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()
    {
        //freopen("E:\acm\input.txt","r",stdin);
        solver.init();
        int N,M;
        cin>>M>>N;
        int s = 0, t = N+M+1;
        for(int i=1;i<=M;i++){
            int a;
            scanf("%d",&a);
            solver.addedge(s,i,a);
        }
        vector<int> G[105];
        for(int i=1;i<=N;i++){
            int a;
            scanf("%d",&a);
            for(int j=1;j<=a;j++){
                int temp;
                scanf("%d",&temp);
                solver.addedge(temp,M+i,INF);
                for(int k=1;k<i;k++){
                    bool flag = false;
                    for(int m=0;m<G[k].size();m++)
                       if(G[k][m] == temp){
                           flag = true;
                           break;
                       }
                    if(flag)  solver.addedge(M+k,M+i,INF);
                }
                G[i].push_back(temp);
            }
            int b;
            scanf("%d",&b);
            solver.addedge(M+i,t,b);
        }
        printf("%d
    ",solver.MaxFlow(s,t));
    }
    View Code
  • 相关阅读:
    .net core 2.0以上版本加载appsettings.json
    BZOJ 2564: 集合的面积
    P3829 [SHOI2012]信用卡凸包
    P2215 [HAOI2007]上升序列
    P2511 [HAOI2008]木棍分割
    P2510 [HAOI2008]下落的圆盘
    P4053 [JSOI2007]建筑抢修
    P4050 [JSOI2007]麻将
    P4049 [JSOI2007]合金
    P4161 [SCOI2009]游戏
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3267501.html
Copyright © 2011-2022 走看看