HDU 3605 Escape(二分图多重匹配问题)
Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 10382 Accepted Submission(s): 2485
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
If you can output YES, otherwise output NO.
题意:
给你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 ;
}
#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(二分图多重匹配问题)的更多相关文章
- HDU(3605),二分图多重匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others) ...
- HDU3605 Escape —— 二分图多重匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others) ...
- 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 ...
- hdu 3605 Escape 二分图的多重匹配(匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others) ...
- HDU - 3605 Escape (缩点+最大流/二分图多重匹配)
题意:有N(1<=N<=1e5)个人要移民到M(1<=M<=10)个星球上,每个人有自己想去的星球,每个星球有最大承载人数.问这N个人能否移民成功. 分析:可以用最大流的思路求 ...
- hdu 3605(二分图多重匹配)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- HDU 1669 二分图多重匹配+二分
Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...
- hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)
#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...
随机推荐
- 【FICO系列】SAP FICO 基本概念
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO 基本概念 ...
- [开发技巧]·如何让离线安装Python包
[开发技巧]·如何让离线安装Python包 1.问题描述 PyPI(Python Package Index)是python官方的第三方库的仓库,所有人都可以下载第三方库或上传自己开发的库到PyPI. ...
- Oracle的substr函数简单用法(转)
转:http://www.cnblogs.com/nicholas_f/articles/1526063.html substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('H ...
- pycharm中的Terminal 中无法使用git的问题
1.先找到git的安装路径,建议使用Everything工具 2.打开pycharm中的setting > tools > Terminal 3.把git的安装路径加上启动文件 bash. ...
- python字典-基础
一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...
- 版本控制工具 GIT 简要教程
一,Git 简介 其实这个就不用说了 但是国际惯例还是介绍一下吧; Git 是一个开源的分布式版本控制系统,用于敏捷 高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助 ...
- 什么是 Java 对象深拷贝?面试必问!
点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 介绍 在Java语言里,当我们需要拷贝一个对象时,有两种类型的拷贝:浅拷贝与深拷贝. 浅拷贝只是拷贝了源对象的地址 ...
- Python自学第二天学习之《元组与字典》
一. 元组:tuple类型,元组一级元素 不能修改 不能增加 不能删除,是有序的. 格式 :tu=(1,2,3,4,5,6) 1.类型转换: #字符串转换成元组 b=“123” c=tuple(b) ...
- service worker介绍
原文:Service workers explained 译者:neal1991 welcome to star my articles-translator, providing you advan ...
- for循环延伸
经典面试题解析: for(var i = 1 ; i < 5 ; i++){ console.log(i) } //1 2 3 4 ------------------------------- ...