Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 8372 Accepted Submission(s): 1893
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
题意:现在有n个人,m个星球,每个人有自己喜欢的几个星球,每个星球也有自己的容量,问能否使每个人走住到喜欢的星球上,但是这里人有点多,最多十万个,如果直接建图肯定会超时,因为这里星球数量最多只有十个,所以可以进行状态压缩,每个人对应已中状态,同时记录每一种状态出现的次数,因为最多十个星球,所以状态的数量最多1024,通过状态来与星球之间建边
1:超级源点连接每一种出现过的状态,容量为这种状态出现过的次数
2:超级汇点连接每一个星球,容量为星球的容量
3:状态向星球之间建边,容量为INF,表示人数可以无限转移,但是最终能容纳多少还是要看星球与超级汇点的容量
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 100000
#define MAXM 9000000
#define INF 0x3f3f3f3f
int head[MAXN],dis[MAXN],vis[MAXN],cur[MAXN];
int n,m,cnt;
struct node
{
int u,v,cap,flow,next;
}edge[MAXM];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int a,int b,int w)
{
node E={a,b,w,0,head[a]};
edge[cnt]=E;
head[a]=cnt++;
node E1={b,a,0,0,head[b]};
edge[cnt]=E1;
head[b]=cnt++;
}
bool BFS(int s,int t)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(E.cap>E.flow&&!vis[E.v])
{
dis[E.v]=dis[E.u]+1;
vis[E.v]=1;
if(E.v==t) return true;
q.push(E.v);
}
}
}
return false;
}
int DFS(int x,int a,int e)
{
if(a==0||e==x) return a;
int flow=0,f;
for(int &i=cur[x];i!=-1;i=edge[i].next)
{
node &E=edge[i];
if(dis[E.v]==dis[E.u]+1&&(f=DFS(E.v,min(E.cap-E.flow,a),e))>0)
{
flow+=f;
a-=f;
edge[i].flow+=f;
edge[i^1].flow-=f;
if(a==0) break;
}
}
return flow;
}
int MAXflow(int s,int t)
{
int flow=0;
while(BFS(s,t))
{
memcpy(cur,head,sizeof(head));
flow+=DFS(s,INF,t);
}
return flow;
}
void getmap()
{
int time[2048];
memset(time,0,sizeof(time));
for(int i=1;i<=n;i++)
{
int mark=0;
for(int j=0;j<m;j++)
{
int c;
scanf("%d",&c);
if(c) mark+=(1<<j);
}
// printf("%d\n",mark);
time[mark]++;
}
for(int i=0;i<(1<<m);i++)
{
if(time[i])
{
add(1035,i,time[i]);
for(int j=0;j<m;j++)
if(i&(1<<j))
add(i,j+1025,INF);
}
}
for(int i=0;i<m;i++)
{
int b;
scanf("%d",&b);
add(i+1025,1036,b);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
if(MAXflow(1035,1036)==n)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}