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 ...
随机推荐
- 浅谈矩阵加速——以时间复杂度为O(log n)的算法实现裴波那契数列第n项及前n之和使用矩阵加速法的优化求法
首先请连矩阵乘法乘法都还没有了解的同学简单看一下这篇博客: https://blog.csdn.net/weixin_44049566/article/details/88945949 首先直接暴力求 ...
- hive DDL操作
1.创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path ...
- poj1305 Fermat vs. Pythagoras(勾股数)
题目传送门 题意: 设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y, ...
- 详解 HiveUDF 函数
更多精彩原创内容请关注:JavaInterview,欢迎 star,支持鼓励以下作者,万分感谢. Hive 函数 相信大家对 Hive 都不陌生,那么大家肯定用过 Hive 里面各种各样的函数.可能大 ...
- 构建CRD工程 - 程序员学点xx 43 k8s
目录 Kubernetes -3- 这是yann的第98篇分享 本日状态: 帮同事排了一天bug. Kubernetes -3- 这是yann的第98篇分享 第 1 部分 承前 昨天用视屏的方式演 ...
- spark复习笔记(3)
在windows上实现wordcount单词统计 一.编写scala程序,引入spark类库,完成wordcount 1.sparkcontextAPI sparkcontext是spark功能的主要 ...
- 【JAVA】input.next().charAt(0);的含义
接收键盘输入的字符串,并且取出它的第一个字符. 分析: Scanner scan=new Scanner(System.in); String s=scan.next(); //返回一个String ...
- CSS文字,文本,背景,盒模型等记录
文字: font-family:" "; /*设置字体*/ font-size:6px;/*设置文字字号*/ color:red;/*设置文字颜色*/ font-weight:bo ...
- Webpack Loader种类以及执行顺序
我们在用webpack构建项目的时候,有两种配置打包文件的方式: import或者require :a-loader!b-loader!.././static/dog.png(打包某一个文件) 配置w ...
- 一条sql引发的“血案”
前几天有一个项目要上线,需要对表的一个字段进行扩充,项目经理让我准备脚本,于是我准备了如下的脚本: )); )); )); 结果上线的时候,ord_log1和ord_log2表中有30万数据,在执行的 ...