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. mooc-IDEA 高效定位代码--004

    十.IntelliJ IDEA -高效定位代码-精准搜索 1.快速定位类:Navigate->Class...   [Ctrl+N] 2.文件:Navigate->File..   [Ct ...

  2. Linux的简介安装与简单使用

    一: 适合初学者的最佳Linux发行版:Linux Mint 老旧硬件的最佳Linux发行版:Ubuntu MATE 安全行业的最佳Linux发行版:Kali Linux 专属游戏的Linux发行版: ...

  3. Redis介绍及入门安装及使用

    Redis介绍及入门安装及使用 什么是Redis Redis is an open source (BSD licensed), in-memory data structure store, use ...

  4. MySQL-第十一篇JDBC典型用法

    1.JDBC常用方式      1>DriverManager:管理JDBC驱动的服务类.主要用于获取Connection.其主要包含的方法: public static synchronize ...

  5. RESUful风格

    1.7 RESTful风格 1.7.1 RESTful风格介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和de ...

  6. [常用类]Number & Math 类(转载)

    下面的表中列出的是 Number & Math 类常用的一些方法: 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo( ...

  7. Python pass是空语句用法

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

  8. 使用IP在局域网内访问System.Net.HttpListenerException:“拒绝访问。”

    记录一下,自己写的程序之前运行没有遇到这个问题,突然遇到这个问题,找了一圈没有找到有效的解决方案,到最后发现,以管理员身份运行程序即可.简单记录一下. 还有就是 .UseUrls("http ...

  9. Vue+Element-Ui 异步表单效验

    简单的效验 Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名 /* ruleForm :表单绑定的数 ...

  10. windows下使用命令行获取管理员权限

    在win下运行npm install安装依赖出现错误: Error: EBUSY, resource busy or locked 搜索错误信息后发现是由于没有管理员权限,在bash中输入以下命令后运 ...