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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. #include <stack>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. #define ms(a, b) memset(a, b, sizeof(a))
  15. #define pb push_back
  16. #define mp make_pair
  17. const LL INF = 0x7fffffff;
  18. const int inf = 0x3f3f3f3f;
  19. const int mod = 1e9+;
  20. const int maxn = +;
  21. const int maxm = +;
  22. int n, m, uN, vN;
  23. int g[maxn][maxm];
  24. int linker[maxm][maxn];
  25. bool used[maxm];
  26. int num[maxm];
  27. bool dfs(int u)
  28. {
  29. for(int v = ;v<vN;v++)
  30. if(g[u][v] && !used[v]){
  31. used[v] = true;
  32. if(linker[v][]<num[v]){
  33. linker[v][++linker[v][]] = u;
  34. return true;
  35. }
  36. for(int i = ;i<=num[];i++)
  37. if(dfs(linker[v][i])){
  38. linker[v][i] = u;
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. int hungary()
  45. {
  46. int res = ;
  47. for(int i = ;i<vN;i++){
  48. linker[i][] = ;
  49. }
  50. for(int u = ;u<uN;u++){
  51. ms(used, false);
  52. if(dfs(u)) res++;
  53. else return res;
  54. }
  55. return res;
  56. }
  57. void init() {
  58. ms(g, );
  59. }
  60. void solve() {
  61. for(int i = ;i<n;i++){
  62. for(int j = ;j<m;j++){
  63. int x;
  64. scanf("%d", &x);
  65. if(x==){
  66. g[i][j] = ;
  67. }
  68. else{
  69. g[i][j] = ;
  70. }
  71. }
  72. }
  73. for(int i = ;i<m;i++)
  74. scanf("%d", &num[i]);
  75. vN = m, uN = n;
  76. int ans = hungary();
  77. // printf("%d\n", ans);
  78. if(ans==n){
  79. printf("YES\n");
  80. }
  81. else{
  82. printf("NO\n");
  83. }
  84. }
  85. int main() {
  86. #ifdef LOCAL
  87. freopen("input.txt", "r", stdin);
  88. // freopen("output.txt", "w", stdout);
  89. #endif
  90. while(~scanf("%d%d", &n, &m)){
  91. init();
  92. solve();
  93. }
  94. return ;
  95. }
fread版:249ms
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. #include <stack>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. #define ms(a, b) memset(a, b, sizeof(a))
  15. #define pb push_back
  16. #define mp make_pair
  17. const LL INF = 0x7fffffff;
  18. const int inf = 0x3f3f3f3f;
  19. const int mod = 1e9+;
  20. const int maxn = +;
  21. const int maxm = +;
  22. //输入挂
  23. const int MAXBUF = ;
  24. char buf[MAXBUF], *ps = buf, *pe = buf+;
  25. inline void rnext()
  26. {
  27. if(++ps == pe)
  28. pe = (ps = buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin);
  29. }
  30. template <class T>
  31. inline bool in(T &ans)
  32. {
  33. ans = ;
  34. T f = ;
  35. if(ps == pe) return false;//EOF
  36. do{
  37. rnext();
  38. if('-' == *ps) f = -;
  39. }while(!isdigit(*ps) && ps != pe);
  40. if(ps == pe) return false;//EOF
  41. do
  42. {
  43. ans = (ans<<)+(ans<<)+*ps-;
  44. rnext();
  45. }while(isdigit(*ps) && ps != pe);
  46. ans *= f;
  47. return true;
  48. }
  49. const int MAXOUT=;
  50. char bufout[MAXOUT], outtmp[],*pout = bufout, *pend = bufout+MAXOUT;
  51. inline void write()
  52. {
  53. fwrite(bufout,sizeof(char),pout-bufout,stdout);
  54. pout = bufout;
  55. }
  56. inline void out_char(char c){ *(pout++) = c;if(pout == pend) write();}
  57. inline void out_str(char *s)
  58. {
  59. while(*s)
  60. {
  61. *(pout++) = *(s++);
  62. if(pout == pend) write();
  63. }
  64. }
  65. template <class T>
  66. inline void out_int(T x) {
  67. if(!x)
  68. {
  69. out_char('');
  70. return;
  71. }
  72. if(x < ) x = -x,out_char('-');
  73. int len = ;
  74. while(x)
  75. {
  76. outtmp[len++] = x%+;
  77. x /= ;
  78. }
  79. outtmp[len] = ;
  80. for(int i = , j = len-; i < j; i++,j--) swap(outtmp[i],outtmp[j]);
  81. out_str(outtmp);
  82. }
  83. //end
  84. int n, m, uN, vN;
  85. int g[maxn][maxm];
  86. int linker[maxm][maxn];
  87. bool used[maxm];
  88. int num[maxm];
  89. bool dfs(int u)
  90. {
  91. for(int v = ;v<vN;v++)
  92. if(g[u][v] && !used[v]){
  93. used[v] = true;
  94. if(linker[v][]<num[v]){
  95. linker[v][++linker[v][]] = u;
  96. return true;
  97. }
  98. for(int i = ;i<=num[];i++)
  99. if(dfs(linker[v][i])){
  100. linker[v][i] = u;
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. int hungary()
  107. {
  108. int res = ;
  109. for(int i = ;i<vN;i++){
  110. linker[i][] = ;
  111. }
  112. for(int u = ;u<uN;u++){
  113. ms(used, false);
  114. if(dfs(u)) res++;
  115. else return res;
  116. }
  117. return res;
  118. }
  119. void init() {
  120. ms(g, );
  121. }
  122. void solve() {
  123. int x;
  124. for(int i = ;i<n;i++){
  125. for(int j = ;j<m;j++){
  126. in(x);
  127. if(x==){
  128. g[i][j] = ;
  129. }
  130. else{
  131. g[i][j] = ;
  132. }
  133. }
  134. }
  135. for(int i = ;i<m;i++)
  136. in(num[i]);
  137. vN = m, uN = n;
  138. int ans = hungary();
  139. if(ans==n){
  140. out_str("YES");out_char('\n');
  141. }
  142. else{
  143. out_str("NO");out_char('\n');
  144. }
  145. }
  146. int main() {
  147. #ifdef LOCAL
  148. freopen("input.txt", "r", stdin);
  149. // freopen("output.txt", "w", stdout);
  150. #endif
  151. while(in(n)&&in(m)){
  152. init();
  153. solve();
  154. }
  155. write();
  156. return ;
  157. }

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. 【python+selenium自动化】使用pytest+allure2完成自动化测试报告的输出

    pytest的pytest-html插件是一个很方便的测试报告,运行自动化测试用例时,pytest后加上参数即可 allure是一个测试报告的框架,相比pytest-html的优势就是“逼格” 他的优 ...

  2. JS调试分享技巧

    1. 学会使用console.log console.log谁都会用,但是很多同学只知道最简单的console.log(x)这样打印一个对象,当你的代码里面console.log多了之后,会很难将某条 ...

  3. 比Redux更容易上手的状态管理库

    前言 当项目越发复杂时,我们发现仅仅是提升状态已经无法适应如此复杂的状态管理了,程序状态变得比较难同步,操作,到处是回调,发布,订阅,这意味着我们需要更好的状态管理方式,于是就引入了状态管理库,如Re ...

  4. hadoop编程小技巧(1)---map端聚合

    測试hadoop版本号:2.4  Map端聚合的应用场景:当我们仅仅关心全部数据中的部分数据时,而且数据能够放入内存中. 使用的优点:能够大大减小网络数据的传输量,提高效率: 一般编程思路:在Mapp ...

  5. AI-sklearn 学习笔记(二)数据集

    from sklearn import datasets from sklearn.linear_model import LinearRegression loaded_data = dataset ...

  6. more 分页显示文件内容

    1.命令功能 more 分页显示文件内容 2.语法格式 more  option file 参数说明 参数 参数说明 -num 指定屏幕显示大小为num行 +num 从行号num号开始显示 -s 把连 ...

  7. Redis分布式锁【实战】

    概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可 ...

  8. java并发学习--第一章 线程的创建

    所谓的并发就是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行.所以我们看似几个线程在同时进行,其实在操作系统中 ...

  9. 美国知名Cloudflare网络公司遭中国顶尖黑客攻击

    最近中美贸易战愈演愈烈,美国知名Cloudflare网络公司的客户的分布式拒绝服务攻击今天在恶意流量方面达到了新的高度,黑客并袭击了该公司在欧洲和美国的数据中心.根据Cloudflare首席执行官马修 ...

  10. java基础复习(四)

    ---恢复内容开始--- 一.for循环的掌握  语法格式: for( 初始化语句A   ;  条件判断B  ;  循环后功能语句C){ //循环体D } for的执行流程:   整个for循环结构中 ...