M - 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;
}

 二分图多重匹配 匈牙利算法

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100000+5
int mapp[N][15],vis[15];//图G和增广路访问标记
int num[15],w[15];//右边点已匹配值和其可容纳值
int match[15][N];//左边元素与右边元素第n次匹配
int n,m;//左边点数,右边点数
int dfs(int u)//找增广路
{
    for(int i=1;i<=m;i++)//注意,这里节点是从1开始编号,题目有时是从0开始编号!!
    {
        if(!vis[i]&&mapp[u][i])//不在增广路
        {
            vis[i]=1;//放进增广路
            if(num[i]<w[i])//如果当前已匹配数量小于可容纳量,则直接匹配
            {
                match[i][++num[i]]=u;
                return true;
            }
            else
            {
                for(int j=1;j<=num[i];j++)
                {
                    if(dfs(match[i][j]))
                    {//如果先前已匹配右边的点能另外找到增广路,则此点仍可匹配
                        match[i][j]=u;
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
int hungary()
{   
    memset(match,0,sizeof(match));
    memset(num,0,sizeof(num));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(!dfs(i))
            return false;
    }
    return true;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        
        int a;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a);
                mapp[i][j]=a;
            }
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&a);
            w[i]=a;
        }
        if(hungary())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值