Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
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.
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
解题思路:这道题目是匹配的问题,考虑用网络流来做。。。最开始就是建模了,由于这道题是一个很典型的二分图,所以直接加入一个超级源点和超级汇点。人与星球之间建立一条容量为1的有向边,源点与人之间建立一条容量为人总数的边,星球与汇点建立一条容量为该星球容纳人数的边。。建图很简单,但是人的数量达到10W,肯定是超时的。。
看了下别人的思路,这道题目确实很巧妙,由于最多只有10个星球,可以考虑用状态压缩来处理,dp[i]表示状态为i时满足条件的人数,只要状态i中第j位是1,就表明了它肯定要与星球j连一条边,这样源点与每个状态连一条容量为dp[i]的边,星球与汇点连一条容量为该星球容纳人数的边。。。
AC:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=2100;
const int MAXE=13000;
const int INF=1<<30;
int net[MAXN],size;
int pre[MAXN],pe[MAXN],gap[MAXN],dist[MAXN];
struct EDGE
{
int v,next;
int cap;
}edge[MAXE];
void init()
{
memset(net,-1,sizeof(net));
size=0;
}
void add(int u,int v,int cap)
{
edge[size].v=v;
edge[size].cap=cap;
edge[size].next=net[u];
net[u]=size++;
edge[size].v=u;
edge[size].cap=0;
edge[size].next=net[v];
net[v]=size++;
}
int ISAP(int s,int t,int node)
{
int i,temp,neck,cur_flow,u;
int max_flow;
memset(pre,-1,sizeof(pre));
memset(gap,0,sizeof(gap));
memset(dist,0,sizeof(dist));
for(i=0;i<=node;i++)
pe[i]=net[i];
gap[node]=node;
max_flow=0;
u=s;
while(dist[s]<node)
{
if(u==t)
{
cur_flow=INF;
for(i=s;i!=t;i=edge[pe[i]].v)
if(cur_flow>edge[pe[i]].cap)
{
neck=i;
cur_flow=edge[pe[i]].cap;
}
for(i=s;i!=t;i=edge[pe[i]].v)
{
temp=pe[i];
edge[temp].cap-=cur_flow;
edge[temp^1].cap+=cur_flow;
}
max_flow+=cur_flow;
u=neck;
}
for(i=pe[u];i!=-1;i=edge[i].next)
if(edge[i].cap>0&&dist[u]==dist[edge[i].v]+1)
break;
if(i!=-1)
{
pe[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else
{
if(0==--gap[dist[u]])
break;
pe[u]=net[u];
for(temp=INF,i=net[u];i!=-1;i=edge[i].next)
if(edge[i].cap>0)
temp=min(temp,dist[edge[i].v]);
dist[u]=temp+1;
gap[dist[u]]++;
if(u!=s)
u=pre[u];
}
}
return max_flow;
}
int main()
{
int n,m,i,j,temp,dp[MAXN],val[MAXN];
while(scanf("%d%d",&n,&m)==2)
{
int start=0;
init();
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
int s=0;
for(j=0;j<m;j++)
{
int x;
scanf("%d",&x);
s|=x<<j;
}
dp[s]++;
}
int end=(1<<m)+m+1;
int node=end+1;
for(i=0;i<m;i++)
{
scanf("%d",&val[i]);
add((1<<m)+1+i,end,val[i]);
}
for(i=0;i<(1<<m);i++)
{
if(!dp[i]) continue;
add(start,i+1,dp[i]);
for(j=0;j<m;j++)
{
int k=1<<j;
if(k&i)
add(i+1,j+(1<<m)+1,INF);
}
}
int ans=ISAP(start,end,node);
if(ans==n)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}