zoukankan      html  css  js  c++  java
  • 网络最大流——POJ

    题目链接

    题目含义

    说了一大堆,就是发电站发电,中转站不用电不发电,用户用电,而且每个都有一个电流的限制

    问用户们最多能得到多少电

    题目分析

    用一个超级源头指向所有发电站,最大容量是他们的发电量

    将所有用户指向一个终点,最大容量是他们的用电量

    于是就变成了一个最简单的最大流

    以后遇到类似的题,像这样转化就可以了

    但是输入很烦,要想读括号这几个字符,又会因为读空格导致出错,注意一下就行了

    题目代码

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    typedef long long LL;
    const int maxn=1e4+7;
    const int INF=0x3f3f3f3f;
    int head[107],deep[107];
    LL maxflow,z;
    int tot,n,m,np,nc,u,v;
    struct edge{
        int to,next;
        LL dis;
    }e[maxn*2];
    void add(int u,int v,LL w){
        e[tot].to=v;
        e[tot].dis=w;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    bool Bfs(int s,int t){
        memset(deep,0,sizeof(deep));
        queue<int>q;
        deep[s]=1;
        q.push(s);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=head[u];i!=-1;i=e[i].next){
                int v=e[i].to;
                if(!deep[v]&&e[i].dis){
                    deep[v]=deep[u]+1;
                    q.push(v);
                }
            }
        }return deep[t];
    }
    LL Dfs(int u,int t,LL dis){
        if(u==t||!dis)return dis;
        LL sum=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(deep[v]==deep[u]+1&&e[i].dis){
                LL temp=Dfs(v,t,min(dis,e[i].dis));
                if(temp){
                    sum+=temp;
                    dis-=temp;
                    e[i].dis-=temp;
                    e[i^1].dis+=temp;
                    if(!dis)break;
                }
            }
        }return sum;
    }
    void Dinic(int s,int t){
        while(Bfs(s,t)){
            maxflow+=Dfs(s,t,INF);
        }
    }
    void init(){
        memset(head,-1,sizeof(head));
        tot=maxflow=0;
    }
    int main(){
        while(~scanf("%d%d%d%d",&n,&np,&nc,&m)){
            init();
            for(int i=1;i<=m;i++){
                while(getchar()!='(');
                scanf("%d%*c%d%*c%lld",&u,&v,&z);
                add(u,v,z),add(v,u,0);
            }
            for(int i=1;i<=np;i++){
                while(getchar()!='(');
                scanf("%d%*c%lld",&u,&z);
                add(n,u,z),add(u,n,0);
            }
            for(int i=1;i<=nc;i++){
                while(getchar()!='(');
                scanf("%d%*c%lld",&u,&z);
                add(u,n+1,z),add(n+1,u,0);
            }
            Dinic(n,n+1);
            printf("%lld
    ",maxflow);
        }
        return 0;
    }
  • 相关阅读:
    逻辑卷管理LVM (Logical Volume Manager)
    Windows Server 2008 R2 域控修改域用户密码复杂性
    win7 加域开机自动登录域用户
    红帽Linux 配置VNC桌面远程工具
    Redhat linux 挂载命令mount
    SUSE Linux 防火墙设置
    IPv4 地址分类
    常用RAID简介_001
    Element UI
    ie中datepicker赋值不成功
  • 原文地址:https://www.cnblogs.com/helman/p/11295941.html
Copyright © 2011-2022 走看看