bzoj3894: 文理分科(最小割)

33 篇文章 0 订阅

题目传送门
跟上一题有点像呢。

解法:
一个点不能同时选理科和文科。
那么这个割很容易建出来。
st连x容量为art
x连ed容量为science
然后四周选相同的怎么表示割呢。
首先前面的最小割把全局割成了两个集合。
一个集合全部选理科。一个集合全部选文科。
那么在文科这个集合里就要连边。
那么对于每一个位置新建一个点。表示四周都选文科。
理科也是一样。新建一个点表示四周都选理科。
然后文科的在文科的集合所以st连x容量为same_art
理科的x连ed容量为same_science。
然后图中的边就互相连,容量为无限大就好了。
最后用总收益-最小割。

代码实现:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {int x,y,c,next,other;}a[1100000];int len,last[510000];
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 list[510000],head,tail,st,ed,h[510000];
bool bt_h() {
    head=1;tail=2;list[1]=st;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(h[y]==0&&a[k].c>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) {t=findflow(y,min(a[k].c,f-s));s+=t;a[k].c-=t;a[a[k].other].c+=t;}
    }if(s==0)h[x]=0;return s;
}
int art[110][110],sc[110][110],sa[110][110],ss[110][110];
int dx[5]={0,1,0,-1,0},dy[5]={1,0,-1,0,0},n,m;
bool pd(int x,int y) {if(x<1||x>n||y<1||y>m)return false;return true;}
int pt(int x,int y) {return (x-1)*m+y;}const int inf=999999999;
int main() {
    scanf("%d%d",&n,&m);st=n*m*3+1;ed=st+1;len=0;memset(last,0,sizeof(last));int sum=0;
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) {int x;scanf("%d",&x);ins(st,pt(i,j),x);sum+=x;}
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) {int x;scanf("%d",&x);ins(pt(i,j),ed,x);sum+=x;}
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) {
        int x;scanf("%d",&x);sum+=x;ins(st,pt(i,j)+n*m,x);
        for(int k=0;k<=4;k++)if(pd(i+dx[k],j+dy[k])==true)ins(pt(i,j)+n*m,pt(i+dx[k],j+dy[k]),inf);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) {
        int x;scanf("%d",&x);sum+=x;ins(pt(i,j)+n*m*2,ed,x);
        for(int k=0;k<=4;k++)if(pd(i+dx[k],j+dy[k])==true)ins(pt(i+dx[k],j+dy[k]),pt(i,j)+n*m*2,inf);
    }
    int ans=0;while(bt_h()==true)ans+=findflow(st,inf);printf("%d\n",sum-ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值