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. Mysql 免安装版本配置

    1. 安装命令 (制定安装目录的my.ini文件) mysqld --install MySQL --defaults-file="C:\mysql-5.7.26-winx64\bin\my ...

  2. 20190909 SpringBoot集成Swagger

    SpringBoot集成Swagger 1. 引入依赖 // SpringBoot compile('org.springframework.boot:spring-boot-starter-web' ...

  3. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NumberFormatException: For input string: "W%" ### Cause: java.lang.NumberFormatException: For input s

    一个常见的myBatis xml文件中的引号错误: org.apache.ibatis.exceptions.PersistenceException: ### Error querying data ...

  4. JAVA总结--集合

    1.集合树状图 Collection:最基本的集合接口 ----List:有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问 ----ArrayList:异步 ----Linke ...

  5. Ant-编译构建(2)-第3方jar包引入、log4j2

    1.项目目录结构图,lib包引入了一些关于common logging+log4j2相关的jar. 2.编写相关的build.xml <?xml version="1.0" ...

  6. Oracle数据库SQL语句的分类

    1986年10月,美国国家标准协会对SQL进行规范后,以此作为关系式数据库管理系统的标准语言,1987年在国际标准组织的支持下成为国际标准.不过各种通行的数据库系统其实在实践过程中都对SQL规范的作了 ...

  7. 一个阿里云apache服务器配置两个或多个域名forLinux

    一个阿里云apache服务器配置两个或多个域名for Linux: 默认已经配置好了阿里云提供的一键web安装,可以参考:http://www.42iot.com/?id=8 修改/alidata/s ...

  8. Python 的 time 模块导入及其方法

    时间模块很重要,Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能,讲解一下Python 的 time 模块导入及其方法. 1,time 模块导入 import time; # ...

  9. VS2015-MFC基础教程-应用程序工程中文件的组成结构

    VS2015应用程序向导生成框架程序后,我们可以在之前设置的Location下看到此文件夹中包含了几个文件和一个以工程名命名的子文件夹,这个子文件夹中又包含了若干个文件和一个res文件夹,创建工程时的 ...

  10. 数据分析之pandas(1)

    一.Pandas的数据结构 1.Series (1)类似于一维数组 (2)通过list构建Series ser_obj=pd.Series(range(10)) (3)pandas数据结构案例