Escape

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

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
 
Recommend
lcy
 
 

题意:

  给你n个人m个星球,和第i个人能否适应第j个星球,1为适应,0为不适应。问你全部人能不能去星球上。

  矩阵建边,跑一下二分图多重匹配。如果这个人无法去任意星球,直接break。

  

普通版:1560ms

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
const int maxm = +;
int n, m, uN, vN;
int g[maxn][maxm];
int linker[maxm][maxn];
bool used[maxm];
int num[maxm];
bool dfs(int u)
{
for(int v = ;v<vN;v++)
if(g[u][v] && !used[v]){
used[v] = true;
if(linker[v][]<num[v]){
linker[v][++linker[v][]] = u;
return true;
}
for(int i = ;i<=num[];i++)
if(dfs(linker[v][i])){
linker[v][i] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = ;
for(int i = ;i<vN;i++){
linker[i][] = ;
}
for(int u = ;u<uN;u++){
ms(used, false);
if(dfs(u)) res++;
else return res;
}
return res;
}
void init() {
ms(g, );
}
void solve() {
for(int i = ;i<n;i++){
for(int j = ;j<m;j++){
int x;
scanf("%d", &x);
if(x==){
g[i][j] = ;
}
else{
g[i][j] = ;
}
}
}
for(int i = ;i<m;i++)
scanf("%d", &num[i]);
vN = m, uN = n;
int ans = hungary();
// printf("%d\n", ans);
if(ans==n){
printf("YES\n");
}
else{
printf("NO\n");
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while(~scanf("%d%d", &n, &m)){
init();
solve();
}
return ;
}
fread版:249ms
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
const int maxm = +;
//输入挂
const int MAXBUF = ;
char buf[MAXBUF], *ps = buf, *pe = buf+;
inline void rnext()
{
if(++ps == pe)
pe = (ps = buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);
}
template <class T>
inline bool in(T &ans)
{
ans = ;
T f = ;
if(ps == pe) return false;//EOF
do{
rnext();
if('-' == *ps) f = -;
}while(!isdigit(*ps) && ps != pe);
if(ps == pe) return false;//EOF
do
{
ans = (ans<<)+(ans<<)+*ps-;
rnext();
}while(isdigit(*ps) && ps != pe);
ans *= f;
return true;
}
const int MAXOUT=;
char bufout[MAXOUT], outtmp[],*pout = bufout, *pend = bufout+MAXOUT;
inline void write()
{
fwrite(bufout,sizeof(char),pout-bufout,stdout);
pout = bufout;
}
inline void out_char(char c){ *(pout++) = c;if(pout == pend) write();}
inline void out_str(char *s)
{
while(*s)
{
*(pout++) = *(s++);
if(pout == pend) write();
}
}
template <class T>
inline void out_int(T x) {
if(!x)
{
out_char('');
return;
}
if(x < ) x = -x,out_char('-');
int len = ;
while(x)
{
outtmp[len++] = x%+;
x /= ;
}
outtmp[len] = ;
for(int i = , j = len-; i < j; i++,j--) swap(outtmp[i],outtmp[j]);
out_str(outtmp);
}
//end
int n, m, uN, vN;
int g[maxn][maxm];
int linker[maxm][maxn];
bool used[maxm];
int num[maxm];
bool dfs(int u)
{
for(int v = ;v<vN;v++)
if(g[u][v] && !used[v]){
used[v] = true;
if(linker[v][]<num[v]){
linker[v][++linker[v][]] = u;
return true;
}
for(int i = ;i<=num[];i++)
if(dfs(linker[v][i])){
linker[v][i] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = ;
for(int i = ;i<vN;i++){
linker[i][] = ;
}
for(int u = ;u<uN;u++){
ms(used, false);
if(dfs(u)) res++;
else return res;
}
return res;
}
void init() {
ms(g, );
}
void solve() {
int x;
for(int i = ;i<n;i++){
for(int j = ;j<m;j++){
in(x);
if(x==){
g[i][j] = ;
}
else{
g[i][j] = ;
}
}
}
for(int i = ;i<m;i++)
in(num[i]);
vN = m, uN = n;
int ans = hungary();
if(ans==n){
out_str("YES");out_char('\n');
}
else{
out_str("NO");out_char('\n');
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while(in(n)&&in(m)){
init();
solve();
}
write();
return ;
}

HDU 3605 Escape(二分图多重匹配问题)的更多相关文章

  1. HDU(3605),二分图多重匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  2. HDU3605 Escape —— 二分图多重匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  3. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  5. HDU - 3605 Escape (缩点+最大流/二分图多重匹配)

    题意:有N(1<=N<=1e5)个人要移民到M(1<=M<=10)个星球上,每个人有自己想去的星球,每个星球有最大承载人数.问这N个人能否移民成功. 分析:可以用最大流的思路求 ...

  6. hdu 3605(二分图多重匹配)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. HDU 1669 二分图多重匹配+二分

    Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  8. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  9. hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)

    #1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...

随机推荐

  1. Jenkins持续集成_04_解决HTML测试报告样式丢失问题

    前言 最近进行Jenkins自动化测试持续集成,配置HTML测试报告后,但是点击进去发现测试报告样式丢失,未加载CSS&JS样式,如下图: 由于Jenkins中配置了CSP(Content S ...

  2. 应用安全-Web安全-子域名/相关域名

    技巧 DNS解析记录 主站获取 单点登录接口 crossdomain.xml IP反查 通过HTTPS证书收集 DNS域传送搜集 联系人信息/邮箱反查域名 x-dns-prefetch-control ...

  3. SSM003/构建Maven单模块项目(一)

    一.环境准备 1.开发工具:IDEA 2.JDK版本:jdk1.8 3.Maven版本:apache-maven-3.2.5 4.数据库mysql. 二.基于Maven构建web项目 Step1:Fi ...

  4. 五分钟搞懂 Linux 重点知识,傻瓜都能学会!

    来源:无痴迷,不成功 www.cnblogs.com/justmine/p/9053419.html 写在前面 我们都知道Linux是一个支持多用户.多任务的系统,这也是它最优秀的特性,即可能同时有很 ...

  5. Java JDK在Mac下的配置方法

    Java JDK在Mac.Windows下的配置方法 Mac 第一步:下载JDK 官网下载地址 第二步:安装JDK 安装步骤很简单,一直点击下一步即可. 第三步:配置环境变量 打开terminal(终 ...

  6. Python pass是空语句用法

    在条件判断,还是函数中,有时候不需要输出任何东西,也不能留空,python提供空的语句,下面讲述pass空语句的用法 1,关键词 pass 2,用法 for letter in 'Python': i ...

  7. 可下拉的PinnedHeaderExpandableListView的实现

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/singwhatiwanna/article/details/25546871 转载请注明出处:htt ...

  8. latex算法步骤如何去掉序号

    想去掉latex算法步骤前面的序号,如下 我想去掉每个算法步骤前面的数字序号,1,2,3,因为我已经写了step.我们只需要引用a lgorithmic这个包就可以了,代码如下: \usepackag ...

  9. Jquery Ajax简单封装(集中错误、请求loading处理)

    Jquery Ajax简单封装(集中错误.请求loading处理) 对Jquery Ajax做了简单封装,错误处理,请求loading等,运用到项目中集中处理会很方便. 技术层面没有什么好说的,请求是 ...

  10. [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (3/5)

    进入OEL