(最大流+合并)HDU-3605 Escape

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

Source

2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

 

按照题目意思,这道题就是裸的最大流问题,常规构图如下:

但是数据范围太大,人的数量为1e5,星球的数量为m,直接这样构图的话会超时,需要进行合并操作。

每个人能够选择一个或者多少星球,那么对于m个星球(m的范围是10),只能有0 0 0...0 到 1 1 1... 1共1024种情况,如果说把区间进行合并的话,就能从1e5优化到1024,构图如下:

将1e5范围内的人压缩进1024个点中,在刚才的老图中,最右边的边权为1,那么对于第二个图中,最左端的边权就应该是n个人中有多少个人的情况符合。

与源点相连的情况与与汇点相连的情况好构图,关键就是人到星球之间的连线不好弄。

1024个星球,每个都有可能跟m中的任意一个或者多个相连,判断方法为且(&):

1024中的数据使用二进制来表示,代表的就是跟m中的点的连接方式!

2^i与x(x∈[0,1023])异或,如果说为1,就代表能够与m中的第i位相连接。

#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int maxn=1e5+100;
const int maxm=2e6;
const int inf=0x3f3f3f3f;
struct Edge
{
    int to,next;
    int cap,flow;
} edge[maxm];

int n,m;
int tmp;
int num[maxn];

int tot;
int head[maxn];
int gap[maxn],dep[maxn],cur[maxn];

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(num,0,sizeof(num));
}

void addedge(int u,int v,int w,int rw=0)
{
    edge[tot].to=v;
    edge[tot].cap=w;
    edge[tot].flow=0;
    edge[tot].next=head[u];
    head[u]=tot++;
    edge[tot].to=u;
    edge[tot].cap=rw;
    edge[tot].flow=0;
    edge[tot].next=head[v];
    head[v]=tot++;
}
int Q[maxn];
void BFS(int s,int e)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]=1;
    int front=0,rear=0;
    dep[e]=0;
    Q[rear++]=e;
    while(front!=rear){
        int u=Q[front++];
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(dep[v]!=-1) continue;
            Q[rear++]=v;
            dep[v]=dep[u]+1;
            gap[dep[v]]++;
        }
    }
}


int S[maxn];
int sap(int s,int e,int n)
{
    BFS(s,e);
    memcpy(cur,head,sizeof(head));
    int top=0;
    int u=s;
    int ans=0;
    while(dep[s]<n)
    {
        if(u==e)
        {
            int Min=inf;
            int inser;
            for(int i=0; i<top; i++)
                if(Min>edge[S[i]].cap-edge[S[i]].flow)
                {
                    Min=edge[S[i]].cap-edge[S[i]].flow;
                    inser=i;
                }
            for(int i=0; i<top; i++)
            {
                edge[S[i]].flow+=Min;
                edge[S[i]^1].flow-=Min;
            }
            ans+=Min;
            top=inser;
            u=edge[S[top]^1].to;
            continue;
        }
        bool flag=false;
        int v;
        for(int i=cur[u]; i!=-1; i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=i;
                break;
            }
        }
        if(flag)
        {
            S[top++]=cur[u];
            u=v;
            continue;
        }
        int Min=n;
        for(int i=head[u]; i!=-1; i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])
            return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=s)
            u=edge[S[--top]^1].to;
    }
    return ans;
}

int in[11];

int main()
{
    in[0]=1;
    for(int i=1;i<=10;++i){
        in[i]=in[i-1]*2;
    }
    while(~scanf("%d%d",&n,&m)){
        init();
        int x;
        for(int i=1;i<=n;++i){
            //n是人  m是星球
            int tmp=0;
            for(int j=0;j<m;++j){
                scanf("%d",&x);
                tmp+=x*in[j];
            }
            //结束,开始构图
            num[tmp]++;
        }

        for(int i=0;i<1024;++i){
            addedge(0,i,num[i]);
            for(int j=0;j<m;++j){
                if(i&in[j]){
                    addedge(i,1024+j+1,inf);
                }
            }
        }

        for(int i=1;i<=m;++i){
            scanf("%d",&x);
            addedge(1024+i,1024+m+1,x);
        }
        int max_puts=sap(0,1024+m+1,1024+m+2);
        if(max_puts==n) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值