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 
#include 
#include 
#include
#include
#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()
{
    queueq;
    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>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
#include
#include
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]

 

你可能感兴趣的:(【二分图】,【网络流】)