hdu3605 Escape(最大流+状态压缩)

Escape

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


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行m列,表示每个人对于每个星球是否可住,最后有一行m个数,表示每个星球的容量

问这m个星球是否可以住n人,如果可以输出YES,否则输出NO


容量限制,最大流问题,关键是如何建边,n<=1e5,点太多了,肯定会超时,此时就需要状态压缩

发现m的取值很小,而每个人对于每个星球是否能住,只有0或1,那么可以压缩成二进制,将每个人对应的状态存入对应的二进制点的位置,此时最多只有1024个状态,将每个状态与星球建边即可

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=1e5+50;
const int maxm=1e7+50;
//可能有某些L为0
struct node
{
    node() {};
    node(int tv,int tw,int tnext)
    {
        v=tv,w=tw,next=tnext;
    };
    int v,w,next;
} e[maxm];
int first[maxn],vis[maxn],dis[maxn],tot;
void add_edge(int u,int v,int w)
{
    e[tot]=node(v,w,first[u]);
    first[u]=tot++;
    e[tot]=node(u,0,first[v]);
    first[v]=tot++;
}
int bfs(int s,int t)
{
    mem(vis,0);
    mem(dis,0);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=first[u]; ~i; i=e[i].next)
        {
            if(!vis[e[i].v]&&e[i].w>0)
            {
                vis[e[i].v]=1;
                dis[e[i].v]=dis[u]+1;
                q.push(e[i].v);
            }
        }
    }
    return dis[t];
}
int dfs(int u,int t,int flow)
{
    if(u==t)return flow;
    for(int i=first[u]; ~i; i=e[i].next)
    {
        if(dis[e[i].v]==dis[u]+1&&e[i].w>0)
        {
            int dd=dfs(e[i].v,t,min(e[i].w,flow));
            if(dd)
            {
                e[i].w-=dd;
                e[i^1].w+=dd;
                return dd;
            }
        }
    }
    dis[u]=0;
    return 0;
}
int Dinic(int s,int t)
{
    int ans=0,flow;
    while(bfs(s,t))
    {
        while(flow=dfs(s,t,INF))
            ans+=flow;
    }
    return ans;
}
void init()
{
    mem(first,-1);
    tot=0;
}
int book[1500],a[50];
int main()
{
    int n,m,x;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        mem(book,0);
        bool flag=0;
        for(int i=1; i<=n; i++)
        {
            int cnt=0;
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&x);
                cnt^=x;
                if(j!=m)
                cnt<<=1;
            }
            book[cnt]++;
        }
        for(int i=1;i<=m;i++)
            scanf("%d",&a[i]);
       if(book[0]!=0)
       {
           puts("NO");
           continue;
       }
       int num=(1<<m)-1;
       for(int i=1;i<=num;i++)
       {
           if(book[i])
            {
                add_edge(0,i,book[i]);
                int cnt=i;
                for(int j=m;j>=1;j--)
                {
                    int x=cnt%2;
                    cnt>>=1;
                    if(x)
                    add_edge(i,num+j,book[i]);
                }
            }
       }
       for(int i=1;i<=m;i++)
       {
           add_edge(num+i,m+num+1,a[i]);
       }
       int now=Dinic(0,m+num+1);
       if(now==n)
        puts("YES");
       else
        puts("NO");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值