• UVA10330拆点最大流


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int maxn = 1000;
    const int maxm = 100000;
    const int inf = 10000000;
    struct node{
        int v,flow,next;
    }edge[maxm];
    int head[maxn],dis[maxn];
    int id,s,t,n,m;
    void add_edge(int u,int v,int flow){
        edge[id].v = v;edge[id].flow = flow;edge[id].next =head[u];head[u] = id++;
        edge[id].v = u;edge[id].flow = 0   ;edge[id].next =head[v];head[v] = id++;
    }
    void init(){
        int flow;
        s = 0,t = n*2+2;//设置源点和汇点
        memset(head,-1,sizeof(head));id = 0;
        for(int i = 1; i <= n; i++){//拆点
            scanf("%d",&flow);
            add_edge(i,i+n,flow);
        }
        scanf("%d",&m);
        int u,v;
        while( m-- ){
            scanf("%d%d%d",&u,&v,&flow);
            add_edge(u+n,v,flow);
        }
        int b,d;
        scanf("%d%d",&b,&d);
        while( b-- ){
            scanf("%d",&v);
            add_edge(s,v,inf);
        }
        while( d --){
            scanf("%d",&u);
            add_edge(u+n,t,inf);
        }
    }
    bool bfs(){
        memset(dis,-1,sizeof(dis));
        queue<int>que;
        dis[s] = 0;
        que.push(s);
        while(!que.empty()){
            int u = que.front();
            que.pop();
            for(int id = head[u]; id != -1; id = edge[id].next){
                int v = edge[id].v;
                if(edge[id].flow > 0 && dis[v] == -1 ){
                    dis[v] = dis[u] + 1;
                    que.push(v);
                }
            }
        }
        return dis[t] != -1;
    }
    int dinic(int u,int flow){
        if( u == t || flow == 0)return flow;
        int tmp = flow;
        for(int id = head[u]; id != -1; id = edge[id].next){
            int v = edge[id].v;
            if( edge[id].flow > 0 && dis[v] == dis[u] + 1){
                int tt = dinic(v,min(tmp,edge[id].flow));
                tmp -= tt;
                edge[id].flow -= tt;
                edge[id^1].flow += tt;
                if(tmp == 0)break;
            }
        }
        return flow - tmp;
    }
    int main(){
        //freopen("in.txt","r",stdin);
        while(~scanf("%d",&n)){
            init();
            int max_flow = 0;
            while(bfs())
            max_flow += dinic(s,inf);
            printf("%d
    ",max_flow);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Codeforces 858B
    区域赛前立FLAG
    GCJ Practice Contest
    UVa 10004 Bicoloring
    UVa 784 Maze Exploration
    Pacemaker+ISCSI实现Apache高可用-环境准备
    多机免密互信
    HAPROXY + Keepalived
    LVS+KEEPALIVED
    oracle-数据库被注入恶意攻击程序的案例恢复
  • 原文地址:https://www.cnblogs.com/LUO257316/p/3229043.html
Copyright © 2020-2023  润新知