hdu-3605Escape(最大流,二进制)

Escape
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 11694    Accepted Submission(s): 2839

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

题意:每个人都有喜欢的星球和不喜欢的星球,每个星球有自己能够承受的人数,求最多能有多少住到自己喜欢的星球上的人。

思路:由于人数太多所以会超时,刚好星球只有10个所以所有的情况就只有2的10次方种情况,每种都由0和1组成,在j位上为1的话就是第j个星球可以住。

#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 1100
#define MAXE 21000
#define INF 0x7fffffff
using namespace std;
int num[MAXN];
struct Edge{
    int to,from,cap,next;
} edge[MAXE];
int head[MAXN],cnt,n,m,s,t;
int dep[MAXN],gap[MAXN];
void init(){
    memset(head,-1,sizeof(head));
    cnt=0;}
void addedge(int cu,int cv,int cw){
    edge[cnt].from=cu;edge[cnt].to=cv;edge[cnt].cap=cw;edge[cnt].next=head[cu];head[cu]=cnt++;
    edge[cnt].from=cv;edge[cnt].to=cu;edge[cnt].cap=0;edge[cnt].next=head[cv];head[cv]=cnt++;}
int que[MAXN];
void BFS(){
    memset(dep,-1,sizeof(dep));memset(gap,0,sizeof(gap));
    gap[0]=1;int L=0,R=0;dep[t]=0;que[R++]=t;
    int u,v;while(L!=R){
        u=que[L++];L=L%MAXN;
        for(int i=head[u];i!=-1;i=edge[i].next){
            v=edge[i].to;if(edge[i^1].cap==0||dep[v]!=-1)continue;
            que[R++]=v;R=R%MAXN;++gap[dep[v]=dep[u]+1];}
    }
}
int cur[MAXN],sta[MAXN];
int Sap(){
    int res=0;BFS();int top=0;memcpy(cur,head,sizeof(head));int u=s,i;
    while(dep[s]<n){
        if(u==t){int temp=INF,inser=n;
            for(i=0; i!=top; ++i){
                if(temp>edge[sta[i]].cap){
                    temp=edge[sta[i]].cap;inser=i;}
            }
            for(i=0; i!=top; ++i){
                edge[sta[i]].cap-=temp;edge[sta[i]^1].cap+=temp;}
            res+=temp;top=inser;u=edge[sta[top]].from;}
        for(i=cur[u]; i!=-1; i=edge[i].next)
            if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
                break;
        if(i!=-1){
            cur[u]=i;
            sta[top++]=i;
            u=edge[i].to;}
        else{
            if(--gap[dep[u]]==0)break;
            int minn=n;
            for(i=head[u]; i!=-1; i=edge[i].next){
                if(edge[i].cap==0)
                    continue;
                if(minn>dep[edge[i].to]){
                    minn=dep[edge[i].to];cur[u]=i;}
            }
            ++gap[dep[u]=minn+1];
            if(u!=s)
                u=edge[sta[--top]].from;}
    }
    return res;}
int main(){
    while(~scanf("%d%d",&n,&m)){init();memset(num,0,sizeof(num));
    s=0;t=(1<<m)+m+1;
    for(int i=0;i<n;i++){int y=0;
        for(int j=0;j<m;j++){
                int x;scanf("%d",&x);y=(y<<1)+x;
            }num[y]++;}int p=(1<<m);
    for(int i=0;i<p;i++){if(num[i]){
            addedge(0,i+1,num[i]);int x=m;
        for(int j=i;j>0;j>>=1){if(j&1)
        addedge(i+1,p+x,num[i]);
        x--;}
        }
    }
    for(int i=0;i<m;i++){int x;scanf("%d",&x);addedge(p+i+1,t,x);}int N=n;n=t+1;
    if(N==Sap())cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    }
}


 

原文地址:https://www.cnblogs.com/da-mei/p/9053265.html