HDU 3605 Escape(状压+最大流)

Escape

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

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

题目链接:HDU 3605

比较入门的一道最大流,显然可以想到是超级源点S->人连一条为流量1的边;人->可达的星球连一条流量为1的边;星球->超级汇点T连一条容量为给定的星球容量的边。但是这样就超时了。

膜了一下正确的做法,因为星球数不超过10,显然一个人的选择状态最多$2^{10}=1024$种,然后就把状态代替了人,因此上面的建图中把人改为二进制的状压转化为10进制的数值i即可。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int S=1050;
struct edge
{
    int to,nxt;
    int cap;
};
edge E[S*23];
int head[S],tot;
bitset<S> vis;
int st[S];

void init()
{
    CLR(head,-1);
    tot=0;
    CLR(st,0);
}
inline void add(int s,int t,int c)
{
    E[tot].to=t;
    E[tot].cap=c;
    E[tot].nxt=head[s];
    head[s]=tot++;

    E[tot].cap=0;
    E[tot].to=s;
    E[tot].nxt=head[t];
    head[t]=tot++;
}
int dfs(int s,int t,int f)
{
    if(s==t)
        return f;
    vis[s]=1;
    for (int i=head[s]; ~i; i=E[i].nxt)
    {
        int v=E[i].to;
        if(!vis[v]&&E[i].cap>0)
        {
            int d=dfs(v,t,min(f,E[i].cap));
            if(d>0)
            {
                E[i].cap-=d;
                E[i^1].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int ret=0;
    int f;
    while (true)
    {
        vis.reset();
        f=dfs(s,t,INF);
        if(!f)
            break;
        ret+=f;
    }
    return ret;
}
int main(void)
{
    int n,m,i,j,c,s;
    while (~scanf("%d%d",&n,&m))
    {
        init();
        int S=0;
        int T=(1<<m)+m;
        for (i=1; i<=n; ++i)
        {
            int S=0;
            for (j=0; j<m; ++j)
            {
                scanf("%d",&s);
                if(s)
                    S|=(1<<j);
            }
            ++st[S];
        }
        int R=1<<m;
        for (i=1; i<R; ++i)
        {
            if(st[i])
            {
                add(S,i,st[i]);
                for (j=0; j<m; ++j)
                {
                    if(i&(1<<j))
                        add(i,R+j,st[i]);
                }
            }
        }
        for (i=0; i<m; ++i)
        {
            scanf("%d",&c);
            add(R+i,T,c);
        }
        puts(max_flow(S,T)==n?"YES":"NO");
    }
    return 0;
}
时间: 2024-12-25 21:54:36

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

HDU 3605 —— Escape 状态压缩+最大流

原题:http://acm.hdu.edu.cn/showproblem.php?pid=3605 #include<cstdio> #include<cstring> #include<string> #include<queue> #include<cmath> #include<vector> #include<algorithm> #define inf 1e9 using namespace std; const

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

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

Hdu 3605 Escape (最大流 + 缩点)

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

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 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 4284Travel(状压DP)

HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1走完所有的他必须要去的城市,打完所有的工,并且成功回到起点1 由于H<=15,所以显然可以状压,预处理这些都必须去的城市之间的最短距离(可以floyd),然后状压DP[S][i]表示他到达了S这些城市后正处在第i个城市里(所以S & (1<<i) != 0)的剩余的最大的钱的数量,然

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

hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 8001    Accepted Submission(s): 1758 Problem Description 2012 If this is the end of th

HDU 3001 Travelling 状压DP

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:还是环游地图的问题,只不过这回旅行者对自己有着严格的要求,地图上每个点的经过次数不能超过两次. 思路:依然是状压DP问题,根上一道很像,只不过这次对于每个点来说有三种状态,分别是未经过,经过一次,经过两次.所以要用三进制的数来进行状态压缩,这个关键点想明白了其他的和上一道基本一样了.对于我来说需要注意的是:能够到达某一个点经过了两次的状态的前一个状态是这个点已经经过了一次的状态,而不是从来未