HDU 3605 Escape 最大流

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7831    Accepted Submission(s): 1702


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
//二进制压缩加dinic
//因为最多十个星球,所有人的状态可以分为2的10次方种,即1024种。统计相同状态人的个数,然后建图
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

const int N = 1500;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cap, rev;
};
vector <edge> G[N];
int level[N], iter[N];

void add_edge(int from, int to, int cap)
{
    edge e;
    e.to = to, e.cap = cap, e.rev = G[to].size();
    G[from].push_back(e);
    e.to = from, e.cap = 0, e.rev = G[from].size() - 1;
    G[to].push_back(e);
}

void bfs(int s)
{
    memset(level, -1, sizeof level);
    queue <int> que;
    level[s] = 0;
    que.push(s);

    while(! que.empty())
    {
        int v = que.front(); que.pop();
        for(int i = 0; i < G[v].size(); i++)
        {
            edge &e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0)
            {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f)
{
    if(v == t) return f;
    for(int &i = iter[v]; i < G[v].size(); i++)
    {
        edge &e = G[v][i];
        if(e.cap > 0 && level[v] < level[e.to])
        {
            int d = dfs(e.to, t, min(f, e.cap));
            if(d > 0)
            {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t)
{
    int flow = 0, f;
    while(true)
    {
        bfs(s);
        if(level[t] < 0) return flow;
        memset(iter, 0, sizeof iter);
        while(f = dfs(s, t, INF), f > 0)
            flow += f;
    }
}

int main()
{
    int n, m, a;
    int cnt[1500];

    while(~ scanf("%d%d", &n, &m))
    {
        memset(cnt, 0, sizeof cnt);
        int max1 = -1;
        for(int i = 0; i < n; i++)
        {
            int tmp = 0;
            for(int i = 0; i < m; i++)
            {
                scanf("%d", &a);
                tmp = tmp * 2 + a;
            }
            max1 = max(max1, tmp);
            cnt[tmp]++; //二进制压缩,统计人数
        }

        for(int i = 0; i <= max1; i++)
        {
            if(cnt[i] == 0) continue;
            add_edge(0, i, cnt[i]); //把0作为超级源点,压缩状态为0时不会连接,所以不必担心和超级源点冲突
        }

        int b = max1 + 1; //b为第一个星球
        for(int i = 0; i <= max1; i++)
        {
            if(cnt[i] == 0) continue;
            int k = 0;
            for(int j = m-1; j >= 0; j--)
            {
                if( ((i >> j) & 1) == 1)
                    add_edge(i, b + k, cnt[i]);
                k++;
            }
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d", &a);
            add_edge(b + i, b + m, a); //b+m为超级汇点,连接星球和超级汇点
        }

        if(max_flow(0, b + m) >= n) printf("YES\n");
        else printf("NO\n");

        for(int i = 0; i <= b+m; i++)
            G[i].clear();
    }

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值