[bzoj3894][网络流-最小割]文理分科

109 篇文章 4 订阅
35 篇文章 0 订阅

Description

文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠 结过) 小P所在的班级要进行文理分科。他的班级可以用一个n*m的矩阵进行
描述,每个格子代表一个同学的座位。每位同学必须从文科和理科中选择 一科。同学们在选择科目的时候会获得一个满意值。满意值按如下的方式 得到:
1.如果第i行第秒J的同学选择了文科,则他将获得art[i][j]的满意值,如 果选择理科,将得到science[i][j]的满意值。
2.如果第i行第J列的同学选择了文科,并且他相邻(两个格子相邻当且 仅当它们拥有一条相同的边)的同学全部选择了文科,则他会更开
心,所以会增加same_art[i][j]的满意值。 3.如果第i行第j列的同学选择了理科,并且他相邻的同学全部选择了理
科,则增加same_science[i]j[]的满意值。 小P想知道,大家应该如何选择,才能使所有人的满意值之和最大。请
告诉他这个最大值。

Input

第一行为两个正整数:n,m 接下来n术m个整数,表示art[i][j]; 接下来n术m个整数.表示science[i][j];
接下来n术m个整数,表示same_art[i][j];

Output

输出为一个整数,表示最大的满意值之和

Sample Input

3 4

13 2 4 13
7 13 8 12
18 17 0 5

8 13 15 4
11 3 8 11
11 18 6 5

1 2 3 4
4 2 3 2
3 1 0 4

3 2 3 2
0 2 2 1
0 2 4 4

Sample Output

152

HINT

样例说明

1表示选择文科,0表示选择理科,方案如下:

1 0 0 1

0 1 0 0

1 0 0 0

N,M<=100,读入数据均<=500

题解

不就是 和 3438一个套路的题嘛。。
st->每个点连边,流量art
每个点->ed连边,流量science
组合拆点u,v
st->u 流量art
v->ed 流量science
组合里面互相连就可以了。。流量inf,不会的看看我3438的blog就可以了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[3110000];int len,last[1110000];
void ins(int x,int y,int c)
{
    int k1,k2;
    k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=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 list[1110000],h[81000];
int head,tail,st,ed;
bool bt_h()
{
    list[1]=st;head=1;tail=2;
    memset(h,0,sizeof(h));
    h[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(a[k].c>0 && h[y]==0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]==0)return false;
    return true;
}
int findflow(int x,int f)
{
    if(x==ed)return f;
    int s=0,t;
    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)
        {
            s+=(t=findflow(y,min(a[k].c,f-s)));
            a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0)h[x]=0;
    return s;
}
int n,m;
int main()
{
    int sum=0,ans=0;
    scanf("%d%d",&n,&m);
    st=n*m*3+1;ed=n*m*3+2;
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int x;
            scanf("%d",&x);sum+=x;
            ins(st,(i-1)*m+j,x);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int x;
            scanf("%d",&x);sum+=x;
            ins((i-1)*m+j,ed,x);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int x;
            scanf("%d",&x);sum+=x;
            int u=n*m+(i-1)*m+j;
            ins(st,u,x);
            ins(u,(i-1)*m+j,999999999);
            if(j!=1)ins(u,(i-1)*m+j-1,999999999);
            if(i!=1)ins(u,(i-2)*m+j,999999999);
            if(j!=m)ins(u,(i-1)*m+j+1,999999999);
            if(i!=n)ins(u,i*m+j,999999999);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int x;
            scanf("%d",&x);sum+=x;
            int u=n*m*2+(i-1)*m+j;
            ins(u,ed,x);
            ins((i-1)*m+j,u,999999999);
            if(j!=1)ins((i-1)*m+j-1,u,999999999);
            if(i!=1)ins((i-2)*m+j,u,999999999);
            if(j!=m)ins((i-1)*m+j+1,u,999999999);
            if(i!=n)ins(i*m+j,u,999999999);
        }
    while(bt_h())ans+=findflow(st,999999999);
    printf("%d\n",sum-ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值