BZOJ3438: 小M的作物

【传送门:BZOJ3438


简要题意:

  有n个种子,有两种土地A,B,第i个种子种在A土地的价值为a[i],B土地的价值为b[i]

  共有m种组合关系,如果第i种组合关系的所有种子都种在A土地则获得c1[i]的价值,都种在B土地则获得c2[i]的价值

  求出能得到的最大价值


题解:

  最小割

  st连向种子,流量为a[i],种子连向ed,流量为b[i]

  对于每组组合关系,将每组分成p,q两个点,然后st连向p流量为c1[i],q连向ed流量为c2[i]

  p连向组合关系里的每一个种子,流量为无穷大,组合关系里的每一个种子连向q,流量也是无穷大

  然后总价值-最小割=总收益


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[2100000];int len,last[210000];
void ins(int x,int y,int c)
{
    int k1=++len,k2=++len;
    a[k1].x=x;a[k1].y=y;a[k1].c=c;
    a[k1].next=last[x];last[x]=k1;
    a[k2].x=y;a[k2].y=x;a[k2].c=0;
    a[k2].next=last[y];last[y]=k2;
    a[k1].other=k2;
    a[k2].other=k1;
}
int st,ed;
int h[210000];
int 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;
            }
        }
        head++;
    }
    if(h[ed]>0) return true;
    else return false;
}
int findflow(int x,int flow)
{
    if(x==ed) return flow;
    int s,t=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&&flow>t)
        {
            s=findflow(y,min(a[k].c,flow-t));
            t+=s;
            a[k].c-=s;a[a[k].other].c+=s;
        }
    }
    if(t==0) h[x]=0;
    return t;
}
int A[1100],B[1100];
int main()
{
    int n;
    scanf("%d",&n);
    len=0;memset(last,0,sizeof(last));
    int sum=0;
    for(int i=1;i<=n;i++) scanf("%d",&A[i]),sum+=A[i];
    for(int i=1;i<=n;i++) scanf("%d",&B[i]),sum+=B[i];
    int m;
    scanf("%d",&m);
    st=0;ed=n+2*m+1;
    for(int i=1;i<=n;i++) ins(st,i,A[i]),ins(i,ed,B[i]);
    for(int i=1;i<=m;i++)
    {
        int k,c1,c2;
        scanf("%d%d%d",&k,&c1,&c2);sum+=c1+c2;
        int p=2*i-1+n,q=2*i+n;
        ins(st,p,c1);ins(q,ed,c2);
        for(int j=1;j<=k;j++)
        {
            int x;
            scanf("%d",&x);
            ins(p,x,999999999);ins(x,q,999999999);
        }
    }
    while(bt_h()==true) sum-=findflow(st,999999999);
    printf("%d
",sum);
    return 0;
}

 

原文地址:https://www.cnblogs.com/Never-mind/p/8670345.html