hdu4619(二分图+匈牙利算法)
2013-07-26 13:06
253 查看
Warm up 2
[b]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 445 Accepted Submission(s): 223
[/b]
[align=left]Problem Description[/align]
Some 1×2 dominoes are placed on a plane. Each dominoe is placed either horizontally or vertically. It's guaranteed the dominoes in the same direction are not overlapped, but horizontal and vertical dominoes may overlap with each
other. You task is to remove some dominoes, so that the remaining dominoes do not overlap with each other. Now, tell me the maximum number of dominoes left on the board.
[align=left]Input[/align]
There are multiple input cases.
The first line of each case are 2 integers: n(1 <= n <= 1000), m(1 <= m <= 1000), indicating the number of horizontal and vertical dominoes.
Then n lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x + 1, y).
Then m lines follow, each line contains 2 integers x (0 <= x <= 100) and y (0 <= y <= 100), indicating the position of a horizontal dominoe. The dominoe occupies the grids of (x, y) and (x, y + 1).
Input ends with n = 0 and m = 0.
[align=left]Output[/align]
For each test case, output the maximum number of remaining dominoes in a line.
[align=left]Sample Input[/align]
2 3
0 0
0 3
0 1
1 1
1 3
4 5
0 1
0 2
3 1
2 2
0 0
1 0
2 0
4 1
3 2
0 0
[align=left]Sample Output[/align]
4
6
本题在比赛时菜鸟我是没有做出来的,一直想着找规律,或者贪心,但终没有弄出什么名堂来!
下来后看了解题报告,说用二分图,同时个人也觉得二分图很重要,于是花了几个小时学习了二分图,了解了基本定理和二分图最大匹配的匈牙利算法
本题横向和纵向在同一方向是不会重叠的,而不同方向才有可能重叠,这就暗示题目可以对应一个完全二分图,为了简化题目,不重叠的部分不建图,重叠的两个方块连边,最终答案就是
横向数+纵向数-最大匹配
#include<iostream> #include<cstdio> using namespace std; //二分图匹配(匈牙利算法的DFS实现) //初始化:g[][]两边顶点的划分情况 //建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 //g没有边相连则初始化为0 //uN是匹配左边的顶点数,vN是匹配右边的顶点数 //调用:res=hungary();输出最大匹配数 //优点:适用于稠密图,DFS找增广路,实现简洁易于理解 //时间复杂度:O(VE) //**************************************************************** //顶点编号从0开始的 const int MAXN=1100; int uN,vN;//u,v数目 int g[MAXN][MAXN]; int linker[MAXN]; bool visited[MAXN]; bool dfs(int u)//从左边开始找增广路径 { int v; for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改 if(g[u][v]&&!visited[v]) { visited[v]=true; if(linker[v]==-1||dfs(linker[v])) {//找增广路,反向 linker[v]=u; return true; } } return false;//这个不要忘了,经常忘记这句 } int hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=0;u<uN;u++) { memset(visited,0,sizeof(visited)); if(dfs(u)) res++; } return res; } //**************************************************** struct point { int x,y; }u[1000+10],v[1000+10]; bool Judge(point a,point b) { if((a.x==b.x&&a.y==b.y)||(a.x+1==b.x&&a.y==b.y)||(a.x==b.x&&a.y==b.y+1)||(a.x+1==b.x&&a.y==b.y+1)) return true; return false; } int main() { int i,j; while(scanf("%d%d",&uN,&vN),uN||vN) { for(i=0;i<uN;i++) scanf("%d%d",&u[i].x,&u[i].y); for(i=0;i<vN;i++) scanf("%d%d",&v[i].x,&v[i].y); memset(g,0,sizeof(g)); for(i=0;i<uN;i++) { for(j=0;j<vN;j++) if(Judge(u[i],v[j])) g[i][j]=1; } // cout<<"**********"<<endl; printf("%d\n",vN+uN-hungary()); } return 0; }
相关文章推荐
- 二分图最大匹配-匈牙利算法
- 牛客网 素数伴侣(二分图最大匹配,匈牙利算法)
- [HDOJ4185]Oil Skimming(二分图最大匹配,匈牙利算法xKM算法)
- 用匈牙利算法求二分图的最大匹配
- 用匈牙利算法求二分图的最大匹配
- 用匈牙利算法求二分图的最大匹配
- 二分图的最大匹配 (匈牙利算法)
- 2017秦皇岛现场赛H.Prime Set(奇偶二分图 匈牙利算法)
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
- 二分图的最大匹配、完美匹配和匈牙利算法
- 二分图的最大匹配 (匈牙利算法)
- 二分图的最大匹配、完美匹配和匈牙利算法
- 二分图 匈牙利算法
- 二分图的最大匹配匈牙利算法和最小支配集
- HDU2444 二分图判断(BFS 的染色法) + 求最大匹配边数(DFS 的匈牙利算法)
- POJ 3041 Asteroids 匈牙利算法 二分图最大匹配
- 二分图最大匹配 -- 匈牙利算法
- 学习匈牙利算法总结(求解二分图最大匹配)
- hdu1151(二分图+最小路径覆盖数+匈牙利算法)