hdu 3605 Escape【状态压缩+最大流Dinic+建图】

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8853    Accepted Submission(s): 2066

Problem Description

2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.

 

 

Input

More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000

 

 

Output

Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.

 

 

Sample Input

1 1

1

1

 

2 2

1 0

1 0

1 1

 

 

Sample Output

YES

NO

 

题目大意:给你n个人,m个星球,最后一行输入表示每个星球最多能够容纳的人数,然后给你n行,每一行中的元素只有两个值,要么是1要么是0.1表示这个人适合第ith星球居住。问能否让所有人都能上其他星球去居住。

 

思路:


1、首先我们将每个人看成一个点,然后建立最大流模型。建立炒鸡源点,其连入每个人,其权值为1,然后每个人连入其能够去的星球,每条边权值也是1,最后将每个星球连入炒鸡汇点,其权值为每个星球的容量。然后发现N^2M是一个草鸡大的一个数量级,2000ms是不够用的。


2、那么我们就需要对其进行优化。注意到M最大才10,其实我们不妨将N个点,变成1<<m个点,每个点表示一种人。

比如这四个点:

00表示哪个星球都不适合这种人。

01表示后边那个星球适合这种人。

10表示前边哪个星球适合这种人。

11表示前边后边两个星球都适合这种人。

然后我们设定num【i】表示状态为i的这种人有多少个。


3、那么我们再重新建立最大流模型:

①从炒鸡源点连入各种人的点,其权值为这种人的个数。

②从各个星球连入汇点,其权值为每个星球所能够容纳的人的个数。

③从各种人的节点,连入其能够去的星球,其权值为这种人的个数。


4、跑一遍最大流,然后判断maxflow==n与否即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1000000];
int head[100005];
int cur[100005];
int num[100050];
int a[100050][12];
int divv[100050];
int ss,tt;
int n,m,contz;
void add(int from,int to,int w)
{
    e[contz].to=to;
    e[contz].w=w;
    e[contz].next=head[from];
    head[from]=contz++;
}
int makedivv()
{
    queue<int >s;
    s.push(ss);
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(divv[v]==divv[u]+1&&w)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
void Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    if(ans==n)printf("YES\n");
    else printf("NO\n");
}
int main()
{
    ss=10000;
    tt=10001;
    while(~scanf("%d%d",&n,&m))
    {
        contz=0;
        memset(head,-1,sizeof(head));
        memset(a,0,sizeof(a));
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
        {
            int tmpp=0;
            for(int j=0;j<m;j++)
            {
                scanf("%d",&a[i][j]);
                if(a[i][j]==1)
                {
                    tmpp+=(1<<j);
                }
            }
            num[tmpp]++;
        }
        for(int i=0;i<(1<<m);i++)
        {
            if(num[i]==0)continue;
            add(ss,i,num[i]);
            add(i,ss,0);
        }
        for(int i=0;i<m;i++)
        {
            int tmpp;
            scanf("%d",&tmpp);
            add((1<<m)+i,tt,tmpp);
            add(tt,(1<<m)+i,0);
        }
        for(int i=0;i<(1<<m);i++)
        {
            for(int j=0;j<m;j++)
            {
                if((i&(1<<j))!=0)
                {
                    add(i,(1<<m)+j,num[i]);
                    add((1<<m)+j,i,0);
                }
            }
        }
        Dinic();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值