[网络流] bzoj3894: 文理分科

bzoj3894: 文理分科:http://www.lydsy.com/JudgeOnline/problem.php?id=3894

网络流最小割
其实和 bzoj3438: 小M的作物 是很像很像很像的 https://blog.csdn.net/qq_36038511/article/details/79662306

对于单个人
源点连每一个人 容量为art
人连上汇点 容量为science
对于一个人和他周围的同学 相当于一个组合
组合拆成两个点 q和p
st连q 容量为same_art
q连上这个人及周围的四个人 容量为inf 防止被割掉
这五个人又连回p 容量为inf
p连汇点 容量为same_science
最大流流出来的即为最小损失
这个正确性显然易见
最大收益-最小损失即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[410000];
int last[31000],len;
int dep[31000],list[11000000];
int art[110][110],sc[110][110],sart[110][110],ssc[110][110];
int st,ed;
void build(int x,int y,int c)
{
    len++;int k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
    len++;int 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;
}
bool bfs()
{
    memset(dep,0,sizeof(dep));
    int head=1,tail=1;
    list[1]=st;dep[st]=1;
    while (head<=tail)
    {
        int x=list[head];
        for (int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if (dep[y]==0&&a[k].c>0)
            {
                dep[y]=dep[x]+1;
                list[++tail]=y;
            }
        }
        head++;
    }
    if (dep[ed]==0) return 0;
    else return 1;
} 
int dfs(int x,int flow)
{
    if (x==ed) return flow;
    int tot=0;
    for (int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if (dep[y]==dep[x]+1&&a[k].c>0&&flow>tot)
        {
            int sum=dfs(y,min(flow-tot,a[k].c));
            tot+=sum;a[k].c-=sum;a[a[k].other].c+=sum;
        }
    }
    if (tot==0) dep[x]=0;
    return tot;
}
int main()
{
    int n,m,sum=0;
    scanf("%d%d",&n,&m);
    st=3*n*m+1;ed=st+1;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            scanf("%d",&art[i][j]); sum+=art[i][j];
            build(st,(i-1)*m+j,art[i][j]);
        }
    }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            scanf("%d",&sc[i][j]); sum+=sc[i][j];
            build((i-1)*m+j,ed,sc[i][j]);
        }
    }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            scanf("%d",&sart[i][j]); sum+=sart[i][j];
            int p=(i-1)*m+j+n*m;
            build(st,p,sart[i][j]);
                        build(p,(i-1)*m+j,999999999);
            if (i-1>=1) build(p,(i-2)*m+j,999999999);
            if (i+1<=n) build(p,i*m+j,999999999); 
            if (j-1>=1) build(p,(i-1)*m+j-1,999999999);
            if (j+1<=m) build(p,(i-1)*m+j+1,999999999); 
        }
    }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            scanf("%d",&ssc[i][j]); sum+=ssc[i][j];
            int q=2*n*m+(i-1)*m+j;
            build(q,ed,ssc[i][j]);
                        build((i-1)*m+j,q,999999999);
            if (i-1>=1) build((i-2)*m+j,q,999999999);
            if (i+1<=n) build(i*m+j,q,999999999);
            if (j-1>=1) build((i-1)*m+j-1,q,999999999);
            if (j+1<=m) build((i-1)*m+j+1,q,999999999); 
        }
    }
    int ans=0;
    while (bfs()) 
        ans+=dfs(st,999999999);
    printf("%d\n",sum-ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值