bzoj1497: [NOI2006]最大获利

又双叒叕get到新姿势——最大权闭合图(好像第一次gdkoi就出过,当时ylmb)胡伯涛的论文:https://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html

那就是sum-最小割咯。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[510000];int len,last[210000];
void ins(int x,int y,int c)
{
    int k1,k2;
    
    len++;k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    
    len++;k2=len;
    a[len].x=y;a[len].y=x;a[len].c=0;
    a[len].next=last[y];last[y]=len;
    
    a[k1].other=k2;
    a[k2].other=k1;
}
int st,ed,h[210000],list[210000];
bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    int head=1,tail=2;list[1]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(h[y]==0&&a[k].c>0)
            {
                h[y]=h[x]+1;
                list[tail]=y;
                tail++;
            }
        }
        head++;
    }
    if(h[ed]==0)return false;
    return true;
}
int findflow(int x,int f)
{
    if(x==ed)return f;
    int s=0;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(h[y]==h[x]+1&&a[k].c>0&&s<f)
        {
            int t=findflow(y,min(a[k].c,f-s));
            s+=t;a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    return s;
}
int main()
{
    int n,m,x,y,z,sum=0;
    scanf("%d%d",&n,&m);
    //1~m表示收益,m+1~m+n表示花费
    st=n+m+1;ed=n+m+2;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        ins(m+i,ed,x);
    }
    for(int j=1;j<=m;j++)
    {
        scanf("%d%d%d",&x,&y,&z);
        ins(st,j,z);sum+=z;
        ins(j,m+x,2147483647);
        ins(j,m+y,2147483647);
    }
    int ans=0;
    while(bt_h()==true)
    {
        ans+=findflow(st,2147483647);
    }
    printf("%d
",sum-ans);
    return 0;
}
原文地址:https://www.cnblogs.com/AKCqhzdy/p/7643860.html