[網絡流] 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;
}
發佈了105 篇原創文章 · 獲贊 18 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章