HDU 3605 Escape 最大流+状压

原题链接: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

2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU

题意

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

题解

这是道非常非常日狗的题。。。。。。。由于星球很少,人很多,所以很容易想到将人的居住情况状压,然后再建图。这样还是要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;
}
时间: 2025-01-05 22:15:49

HDU 3605 Escape 最大流+状压的相关文章

Hdu 3605 Escape (最大流 + 缩点)

题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是TLE还是稳稳的.以前只遇到过网络流拆点建图,这个正好是缩点建图.吼吼吼~~~,建图的方式还是值得学习的. 因为星球数目最多十个,那么无论有多少个人,其不同选择也就2^10种咯.把不同的选择作为节点,节点就从10^5减少到了2^10,整整缩小了一个数量级呢.建立源点和汇点,源点和选择链接,边权为这种选

HDU 3605 Escape (最大流)

Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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

HDU 3605 Escape(最大流+缩点转换)

http://acm.hdu.edu.cn/showproblem.php?pid=3605 题目很简单,要求的就是最后能搬到星球上去的人的个数.刚开始看到,知道是最大流,就把人和星球都设为点,能生存就连线,权值为1,最后建立超级源点和超级汇点.求出最大流量即可.先是RE,开大数组后TLE.仔细算了,光光人到星球的便就可达到100w了,超时的概率太大了.后来找了解题报告,知道了缩点这一说,因为星球个数m最大只有10个,所以每个人最多只有1024种情况,把这每一种情况设为点(这里很抽象),将之与符

HDU 4114 Disney&#39;s FastPass (状压DP)

题意:给定 n 个区域,然后给定两个区域经过的时间,然后你有 k 个景点,然后给定个每个景点的区域和有票没票的等待时间,从哪些区域能够得到票,问你从景点1开始,最后到景点1,而且要经过看完这k个景点. 析:一个状压DP,dp[s1][s2][i] 表示已经访问了 s1 中的景点,拥有 s2 的票,当前在 i 区域,然后两种转移一种是去下一个景点,另一种是去下一个区域拿票.当时输入,写错了,卡了好长时间.... 代码如下: #pragma comment(linker, "/STACK:10240

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU 3605 Escape【二分图多重匹配】

题意: 有n个人去m个星球  告诉你每个人想去哪些星球和每个星球最多容纳多少人,问能不能让所有人都满足 分析: 二分图多重匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 100005; 8 const int maxm = 15; 9 10

HDU 4906 Our happy ending (状压DP)

HDU 4906 Our happy ending 题目链接 题意:给定n个数字,每个数字可以是0-l,要选其中一些数字,然后使得和为k,问方案 思路:状压dp,滚动数组,状态表示第i个数字,能组成的数字状态为s的状态,然后每次一个数字,循环枚举它要选取1 - min(l,k)的多少,然后进行状态转移 代码: #include <cstdio> #include <cstring> typedef long long ll; const int N = (1<<20)

hdu 3605 Escape (二分图多重匹配)

Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4298    Accepted Submission(s): 1129 Problem Description 2012 If this is the end of the world how to do? I do not know how. But now scient

HDU 4856 Tunnels(BFS+状压DP)

HDU 4856 Tunnels 题目链接 题意:给定一些管道,然后管道之间走是不用时间的,陆地上有障碍,陆地上走一步花费时间1,求遍历所有管道需要的最短时间,每个管道只能走一次 思路:先BFS预处理出两两管道的距离,然后状态压缩DP求解,dp[s][i]表示状态s,停在管道i时候的最小花费 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using