hdu3605(网络流+状态压缩)

本文介绍了一种利用状态压缩技巧结合网络流算法解决特定匹配问题的方法。针对大规模人群与有限选项间的偏好匹配问题,通过二进制压缩减少状态数量,并使用最大流算法判断是否能完美匹配每个人到他们喜爱的星球。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的矩阵,n,(i,j)表示这个人是否喜欢这个星球(1表示喜欢0表示不喜欢),问是否可能让所有的人都住在自己喜欢的星球上。

 

思路:由于n很大,如果直接建边跑网络流的话,肯定会tle所以,我们需要将状态压缩一下,考虑到m最大只有10,那么如果用二进制表示人对星球的喜好的话,那么最多只有1024种状态,我们在建边的时候,流量就是这种状态下的人的个数。

取一个超级源点,超级汇点,源点向1024种状态连边,流量是该状态下的人数,然后状态向星球连边,流量还是人数,最后星球向超级汇点连边,流量为星球容量,再跑最大流。

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=1e9+10;
const int Max=2e6+10;
int n,m,e;
int head[Max],le[Max],que[Max],sum[1030];
struct EDGE
{
    int to,flow,next;
};
EDGE edge[Max];
void add(int u,int v,int f)
{
    edge[e].to=v,edge[e].flow=f;
    edge[e].next=head[u];
    head[u]=e++;
    edge[e].to=u,edge[e].flow=0;
    edge[e].next=head[v];
    head[v]=e++;
}
bool bfs(int s)
{
    memset(le,-1,sizeof le);
    memset(que,0,sizeof que);
    le[s]=0;int l=0,r=0;
    que[r++]=s;
    while(l!=r){
        int u=que[l++];
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to,f=edge[i].flow;
            if(f>0&&le[v]<0){
                le[v]=le[u]+1;
                que[r++]=v;
            }
        }
    }
    return le[1024+m+1]>0;
}
int dfs(int u,int Min)
{
    if(u==1024+m+1) return Min;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to,f=edge[i].flow;
        if(f>0&&le[v]==le[u]+1){
            int a=dfs(v,min(Min,f));
            if(a>0){
                edge[i].flow-=a;
                edge[i^1].flow+=a;
                return a;
            }
        }
    }
    le[u]=-1;
    return 0;
}
int dinic()
{
    int ans=0,tmp;
    while(bfs(0))
        while(tmp=dfs(0,inf))
            ans+=tmp;
    return ans;
}
int main()
{
    int x,tmp,xx,cnt,flag;
    while(~scanf("%d%d",&n,&m)){
        flag=0,e=0;
        memset(head,-1,sizeof head);
        memset(sum,0,sizeof sum);
        for(int i=1;i<=n;i++){
            tmp=0;
            for(int j=1;j<=m;j++){
                scanf("%d",&x);
                tmp=tmp*2+x;
            }
            if(tmp==0) flag=1;
            sum[tmp]++;//统计人数
        }
        for(int i=1;i<=1024;i++){
            add(0,i,sum[i]);//源点向状态连边
            cnt=1,xx=i;
            while(xx){
                if(xx%2) add(i,1024+(m-cnt+1),sum[i]);//状态向星球连边
                xx/=2;
                cnt++;
            }
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&x);
            add(1024+i,1024+m+1,x);//星球向汇点连边
        }
        if(dinic()==n&&!flag) printf("YES\n");//dinic模板
        else printf("NO\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值