bzoj 3894: 文理分科

题意:

求一种选文理方式,使得总满意值最大。
满意值按如下的方式
得到:
1.如果第i行第秒J的同学选择了文科,则他将获得art[i][j]的满意值,如
果选择理科,将得到science[i][j]的满意值。
2.如果第i行第J列的同学选择了文科,并且他相邻(两个格子相邻当且
仅当它们拥有一条相同的边)的同学全部选择了文科,则他会更开
心,所以会增加same_art[i][j]的满意值。
3.如果第i行第j列的同学选择了理科,并且他相邻的同学全部选择了理
科,则增加same_science[i]j[]的满意值。

题解:

经典的最小割。
最难想的是一个点要拆三个点,其中一个是选文还是选理,另外两个分别是文和理的额外满意度。
一边连向st/ed,另一边连向周围同学与自己就可以了。
具体代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int inf=(1<<29);
struct node{
    int x,y,next,c,other;
}a[6400000];int last[30010],len=0;
int h[30010],s[30010],st,ed;
void ins(int x,int y,int c)
{
    int k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=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 bt_h()
{
    memset(h,0,sizeof(h));
    int l=1,r=2;s[l]=st;h[st]=1;
    while(l!=r)
    {
        int x=s[l];
        for(int i=last[x];i;i=a[i].next)
        {
            int y=a[i].y;
            if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
        }
        l++;
    }
    return h[ed]!=0;
}
int findflow(int x,int f)
{
    if(x==ed) return f;
    int t,ans=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
        {
            ans+=(t=findflow(y,min(a[i].c,f-ans)));
            a[i].c-=t;a[a[i].other].c+=t;
        }
    }
    if(ans==0) h[x]=0;
    return ans;
}
int n,m,art[110][110],sc[110][110],s_a[110][110],s_s[110][110];
int yh[7]={-1,0,1,0,0};
int yl[7]={0,1,0,-1,0};
int id(int x,int y){return (x-1)*m+y;}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf("%d",&art[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf("%d",&sc[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf("%d",&s_a[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) scanf("%d",&s_s[i][j]);
    st=0;ed=n*m*3+1;int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            ans+=art[i][j];ans+=sc[i][j];
            ans+=s_a[i][j];ans+=s_s[i][j];
            ins(st,id(i,j),art[i][j]);
            ins(id(i,j),ed,sc[i][j]);
            ins(st,n*m+id(i,j),s_a[i][j]);
            ins(2*n*m+id(i,j),ed,s_s[i][j]);
            for(int k=0;k<5;k++)
            {
                int tx=i+yh[k],ty=j+yl[k];
                if(tx<=0||tx>n||ty<=0||ty>m) continue;
                ins(n*m+id(i,j),id(tx,ty),inf);
                ins(id(tx,ty),2*n*m+id(i,j),inf);
            }
        }
    while(bt_h()) ans-=findflow(st,inf);
    printf("%d",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值