杭电2119-Matrix (呵呵,做完这道题感觉自己还可以哈)
2013-08-14 16:05
246 查看
Matrix
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1376 Accepted Submission(s): 599
[align=left]Problem Description[/align]
Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column .
Your task is to give out the minimum times of deleting all the '1' in the matrix.
[align=left]Input[/align]
There are several test cases.
The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.
n=0 indicate the end of input.
[align=left]Output[/align]
For each of the test cases, in the order given in the input, print one line containing the minimum times of deleting all the '1' in the matrix.
[align=left]Sample Input[/align]
3 3
0 0 0
1 0 1
0 1 0
0
[align=left]Sample Output[/align]
2这题大概意思就是给你一个n*m只由0和1组成的矩阵,你可以划掉任意一行或者一列的数字1,问你把矩阵内的1全部划掉最少需要几步?这个题明显的思路是:二分最大匹配,即用匈牙利算法求最大匹配,怎样匹配呢?就是把矩阵的行看做boy,把矩阵的列看做girl,如果矩阵中有数字1,表明这个行(boy)和这个列(girl)想在一起,所以转化一下就是成了HDU上“过山车”这道题了
AC代码+解释:
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<iomanip> #include<queue> #include<stack> #include<set> #include<cmath> #include<map> #include<algorithm> const int MAX=101; int Map[MAX][MAX]; int girl[MAX];//女生 int boy[MAX];//男生 int mark[MAX];//匹配好了就标记 int pipei[MAX];//谁跟谁匹配,例如pipei[1]=2表示男生1号匹配的是2号女生 int g;//表示女生的个数 int b;//表示男生个数 using namespace std; bool find(int x)//寻找男生 { int i; for(i=0;i<b;i++) { if(Map[boy[i]][x]==1&&!mark[boy[i]]) { mark[boy[i]]=1; if(pipei[boy[i]]==-1||find(pipei[boy[i]]))//如果该男生没有被匹配或者已经匹配了但是跟这个男生匹配的女生还有别的男生可以匹配那么就把这个男生让出来 { pipei[boy[i]]=x;//匹配 return true; } } } return false; } int main() { int n,m,sum,i,j; while(cin>>n,n) { cin>>m; memset(Map,0,sizeof(Map));//初始化 memset(pipei,-1,sizeof(pipei));//同上 map<int,int>B;//这里开map容器是记录所有男生以免重复记录 map<int,int>G;//同上 g=0; b=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cin>>Map[i][j]; if(Map[i][j]==1) { if(B[i]==0)//如果这个男生没有被记录 { B[i]++;//就加进来并且标记已加入 boy[b++]=i; } if(G[j]==0)//同上 { G[j]++; girl[g++]=j; } } } } sum=0; for(i=0;i<g;i++)//女生开始匹配男生 { memset(mark,0,sizeof(mark)); if(find(girl[i]))//如果可以匹配就+1 sum+=1; } cout<<sum<<endl; } return 0; }
相关文章推荐
- Html&&Css出错(感觉自己渣得可以)
- 4程序员小飞原计划三天完成某个任务,现在是第三天的下午,他马上就可以做完。但是在实现功能的过程中,他越来越意识到自己原来设计中的弱点,他应该采取另一个办法,才能避免后面集成阶段的额外工作。但是他如果现在就改弦更张,那势必要影响自己原来估计的准确性,并且会花费额外的时间,这样他的老板、同事也许会因此看不起他。如果他按部就班地按既定设计完成,还要花更多时间在后续集成上,但那就不是他个人的问题了,怎么办
- 终于把整个Seafile服务器手册过了一遍,竟然一天就做完了,刺客感觉自己特别伟大!
- poi 和jxl的性能比较,借鉴别人的,本人没有测试过,不过感觉自己可以接收这种解释
- 2012杭电的未理解透 if(p) break;感觉多余 ,却掉也可以
- 2015年3月份推荐的书(自己这个月看过的书感觉还可以)
- 前端进阶试题css(来自js高级前端开发---豪情)既然被发现了HOHO,那我就置顶了嘿嘿!觉得自己技术OK的可以把这套题目做完哦,然后加入高级前端的社区咯
- 最近写了一个红包雨的小功能,但感觉自己的js还有很多地方可以提高,望大神们可以帮忙指点一二
- 如何使自己的桌面应用程序在别人的电脑上也可以用
- 让控件可以处理自己的背景颜色
- 抽象类+接口+内部类(有点晕 写的自己都感觉不太对)
- java软件工程师那里可以学习新的技术,以及怎么提升自己薪资
- 又好多天没有学习网工了!感觉自己很可悲!
- Net程序集混淆加密保护(自己辛辛苦苦搞出来的dll,居然可以很容易的被看到源码!不采取行动行吗!)
- 新手小白昨天成功的把自己绕进去了。今天从头复习,希望可以找到小伙伴一起成长。华为路由基本命令
- hdoj2119_Matrix(二分图匹配)
- WdatePicker.js 日历点击时,触发自定义方法 ,可以调用自己的函数。
- 自己的技术博客开张了,呵呵,就当是个技术资料备份吧
- HTTP错误 大全(方便自己查阅 呵呵)
- 两个感觉还可以的js小实例有注释代码