1. 程式人生 > >Escape dinic 最大流

Escape dinic 最大流

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. 

題意:末日到了,有10個行星是移民的物件,總共100000人每個人都有想去於不想去的行星,並且每個行星都有最大容納人數

判斷是否所有人都可以離開地球去往行星生活。

首先看完題,準備建個超大的圖,超時。

看完別人的部落格,需要將所有選擇相同的合在一起,名為縮點,之後dinic模板就行了


#include <iostream>
#include <string.h>
#include <stdio.h>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=6e4+5;
struct node
{
    int v,cap,to;
}s[maxn];
int cnt,head[maxn],dis[maxn];
int T,sum[maxn];
void add(int u,int v,int cap)
{
    s[cnt].v=v;
    s[cnt].cap=cap;
    s[cnt].to=head[u];
    head[u]=cnt++;
    s[cnt].v=u;
    s[cnt].cap=0;
    s[cnt].to=head[v];
    head[v]=cnt++;
}
bool bfs()
{
    queue<int>q;
    memset(dis,0,sizeof(dis));
    dis[0]=1;
    q.push(0);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int e=head[u];~e;e=s[e].to)
        {
            int v=s[e].v,cap=s[e].cap;
            if(!dis[v]&&cap>0)
            {
                dis[v]=dis[u]+1;
                q.push(v);
            }
        }
    }
    return dis[T]!=0;
}
int dfs(int u,int flow)
{
    int mm;
    if(u==T)
        return flow;
    for(int e=head[u];~e;e=s[e].to)
    {
        int v=s[e].v,cap=s[e].cap;
        if(cap>0&&dis[v]==dis[u]+1&&(mm=dfs(v,min(cap,flow))))
        {
            s[e].cap-=mm;
            s[e^1].cap+=mm;
            return mm;
        }
    }
    dis[u]=-1;
    return 0;
}
int dinic()
{
    int ans=0,tf;
    while(bfs())
    {
        while(tf=dfs(0,inf))
        {
            //printf("%d*\n",tf);
            ans+=tf;
        }
    }
    return ans;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        memset(sum,0,sizeof(sum));
        cnt=0;T=1500;
        for(int i=1;i<=n;i++)
        {
            int id=0;
            for(int j=0;j<m;j++)
            {
                int x;
                scanf("%d",&x);
                if(x==1)
                    id+=(1<<j);
            }
            sum[id]++;
        }
        for(int i=1;i<(1<<m);i++)
        {
            if(sum[i])
            {
                add(10+i,T,sum[i]);
                int temp=i;
                for(int j=0;j<m;j++)
                {
                    if(temp>>j&1)
                        add(j+1,10+i,sum[i]);
                }
            }
        }
        for(int i=1;i<=m;i++)
        {
            int x;
            scanf("%d",&x);
            add(0,i,x);
        }
        int an=dinic();
        if(an==n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}