HDU 3605 Escape 最大流+状压

本文介绍了一道名为“Escape”的编程竞赛题目(HDU 3605),该题要求判断一组人员能否成功移民到若干个具有特定容量限制的星球上。文章详细解释了解题思路,并提供了基于最大流算法的具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605

Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7145    Accepted Submission(s): 1553


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
 

 题意

地球人要移民,告诉你每个人适合居住的星球是哪些,每个星球有个容量,问你是否能安排所有人移民。

题解

这是道非常非常日狗的题。。。。。。。由于星球很少,人很多,所以很容易想到将人的居住情况状压,然后再建图。这样还是要T的,这是为什么呢?因为脸黑。。。在尝试了各种输入挂之后,终于998ms过了,老天有眼。

代码

#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#define MAX_S (1<<10)+10
#define MAX_V 1222
#define MAX_N MAX_V
#define INF 2500005
using namespace std;

struct edge {
    int to, cap, rev;
    bool isRev;

    edge(int t, int c, int r, bool i)
            : to(t), cap(c), rev(r), isRev(i) { }

    edge() { }
};

template <class T>
inline bool scan_d(T &ret)
{
    char c;
    int sgn;
    if(c=getchar(),c==EOF) return 0; //EOF
    while(c!=' -' &&(c<'0' ||c>'9' )) c=getchar();
    sgn=(c==' -' )?-1:1;
    ret=(c==' -' )?0:(c-'0' );
    while(c=getchar(),c>='0' &&c<='9' ) ret=ret*10+(c-'0' );
    ret*=sgn;
    return 1;
}

vector<edge> G[MAX_N];
int level[MAX_V];
int iter[MAX_V];

void init(int totNode) {
    for (int i = 0; i <= totNode; i++)
        G[i].clear();
    memset(level, 0, sizeof(level));
    memset(iter, 0, sizeof(iter));
}

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

void bfs(int s) {
    queue<int> que;
    memset(level, -1, sizeof(level));
    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;
    for (; ;) {
        bfs(s);
        if (level[t] < 0)return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
            flow += f;
        }
    }
}

int n,m;
int S=1113;
int T=1114;

int cnt[MAX_S];

int main() {
    while (scanf("%d%d", &n, &m)!=EOF) {
        init(T + 5);
        memset(cnt, 0, sizeof(cnt));
        for (int i = 0; i < n; i++) {
            int s = 0;
            for (int j = 0; j < m; j++) {
                int t;
                scan_d(t);
                if (t)s |= (1 << j);
            }
            cnt[s]++;
        }
        for (int i = 0; i < (1 << m); i++) {
            if (cnt[i]) {
                add_edge(S, i, cnt[i]);
                for (int j = 0; j < m; j++)
                    if ((1 << j) & i)
                        add_edge(i, j + (1 << m), cnt[i]);
            }
        }
        for (int i = 0; i < m; i++) {
            int t;
            scan_d(t);
            add_edge(i + (1 << m), T, t);
        }
        int f = max_flow(S, T);
        if (f == n)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/HarryGuo2012/p/4735407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值