[BZOJ1497] 最大权闭合子图+最小割+最大流

BZOJ1497
好吧,看到ARC085的E题好像是个最大权闭合子图,于是就去学习了一下,顺便复习一下dinic,最大流最小割定理。
每个客户群向需要选的两个类型连边,然后由原点向客户连边权为获利的边,基站向汇点连边权为所需消耗价格的边,所有正权值之和减去最小割就是答案了,画图显然。
最小割可以转化为最大流,于是乎跑一个dinic模板就行了

    #include<iostream>
    #include<iomanip>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #define maxn 501000
    using namespace std;
    struct node{
        int f,c,to;
        node*next,*rev;
    }*con[maxn];
    void addedge(int x,int y,int num)
    {
        node*p=new node;
        p->to=y;
        p->c=num;
        p->f=0;
        p->next=con[x];
        con[x]=p;
        p=new node;
        p->to=x;
        p->c=0;
        p->f=0;
        p->next=con[y];
        con[y]=p;
        con[x]->rev=con[y];
        con[y]->rev=con[x];
    }
    bool tag[maxn],vis[maxn];
    int de[maxn],dl[maxn],n,m,t,head,tail,s;
    bool bfs()
    {
        head=1,tail=0;
        bool tmp=false;
        memset(vis,0,sizeof(vis));
        dl[++tail]=s;vis[s]=true;de[s]=1;
        int v; 
        while(head<=tail)
        {
        v=dl[head];
        if(v==t) tmp=true;
        for(node*p=con[v];p;p=p->next)
        if(!vis[p->to]&&p->f<p->c)
            de[p->to]=de[v]+1,dl[++tail]=p->to,vis[p->to]=true;
        ++head;
        }
        return tmp;
    }
    int dinic(int v,int e)
    {
        int temp=0,o=0;
        if(tag[v]) return 0;
        if(v==t) return e;
        for(node*p=con[v];p;p=p->next)
        {
        if(de[p->to]==de[v]+1&&p->f<p->c)
        {
            o=dinic(p->to,min(e-temp,p->c-p->f));
            temp+=o;
            p->f+=o;
            p->rev->f-=o;
        }
        if(temp==e) break;
        }
        if(temp==0) tag[v]=true;
        return temp;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        t=n+m+2;s=n+m+1;
        int x,y,w,ans=0;
        for(int i=1;i<=n;++i)
        scanf("%d",&x),addedge(m+i,t,x);
        for(int i=1;i<=m;++i)
        {
        scanf("%d%d%d",&x,&y,&w);
        addedge(i,m+x,0x3f3f3f3f);
        addedge(i,m+y,0x3f3f3f3f);
        addedge(s,i,w);
        ans+=w;
        }
        while(bfs())
        {
            memset(tag,0,sizeof(tag));
            ans-=dinic(s,0x3f3f3f3f);
        }
        printf("%d",ans);
        return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值