码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ1458: 士兵占领

时间:2018-03-01 23:23:51      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:for   size   表示   math   题意   .com   als   line   node   

【传送门:BZOJ1458


简要题意:

  给出一个n*m的矩阵,矩阵上的点可以放置至多1个士兵,矩阵上有些点是不能放士兵的,给出每一行的约束条件L[i]表示第i行至少要有L[i]个士兵,和每一列的约束条件C[i],求出最少放置多少个士兵使得满足所有约束条件


题解:

  最小割

  将L[i]和C[i]全部加起来为sum,表示表面上看需要多少个士兵

  而实际上一个士兵可以做到同时对行和列的约束条件作贡献,所以将所有能够放士兵的点(x,y),将x连向y,流量为无穷

  将st连向行,流量为L[i],列连向ed,流量为C[i]

  至于无论放置多少个士兵都没有办法占领整个棋盘这种情况,就很容易判断了


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[210000];int len,last[11000];
void ins(int x,int y,int c)
{
    int k1=++len,k2=++len;
    a[k1].x=x;a[k1].y=y;a[k1].c=c;
    a[k1].next=last[x];last[x]=k1;
    a[k2].x=y;a[k2].y=x;a[k2].c=0;
    a[k2].next=last[y];last[y]=k2;
    a[k1].other=k2;
    a[k2].other=k1;
}
int h[11000],list[11000],st,ed;
bool bt_h()
{
    memset(h,0,sizeof(h));
    h[st]=1;
    int head=1,tail=2;
    list[1]=st;
    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;
    else 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&&f>s)
        {
            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 L[110],C[110];
bool map[110][110];
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);st=0;ed=n*m+1;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&L[i]);
        ins(st,i,L[i]);sum+=L[i];
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&C[i]);
        ins(i+n,ed,C[i]);sum+=C[i];
    }
    memset(map,true,sizeof(map));
    for(int i=1;i<=k;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        map[x][y]=false;
        L[x]++;C[y]++;
    }
    for(int i=1;i<=n;i++) if(L[i]>m){printf("JIONG!\n");return 0;}
    for(int i=1;i<=m;i++) if(C[i]>n){printf("JIONG!\n");return 0;}
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(map[i][j]==true) ins(i,j+n,999999999);
    int ans=0;
    while(bt_h())
    {
        ans+=findflow(st,999999999);
    }
    printf("%d\n",sum-ans);
    return 0;
}

 

BZOJ1458: 士兵占领

标签:for   size   表示   math   题意   .com   als   line   node   

原文地址:https://www.cnblogs.com/Never-mind/p/8490656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!