二分图匹配入门专题1H - Marriage Media light oj 1184二分图最大匹配
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分图匹配入门专题1H - Marriage Media light oj 1184二分图最大匹配相关的知识,希望对你有一定的参考价值。
You run a marriage media. You take some profiles for men and women, and your task is to arrange as much marriages as you can. But after reading their bio-data you have found the following criteria.
- No man will marry a woman if their height gap is greater than 12 inches.
- No woman will marry a man if their age gap is greater than 5 years.
- A couple can be formed if either both are not divorced or both are divorced.
- Of course, a man can marry a single woman and vice versa.
Now you are given the bio-data of some men and women, you have to arrange the maximum number of marriages considering the given criteria.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains two integer m, n (1 ≤ m, n ≤ 50). Each of the next m lines will contain the information for a man, and each of the next n lines will contain the information for a woman. An information will contain three integers denoting the height in inches, age in years and 1 or 0 depending on they are divorced or not respectively. Assume that Height will be between 50 and 80, age will be between 20 and 50.
Output
For each case, print the case number and the maximum number of marriages you can arrange.
Sample Input
2
2 2
70 30 0
60 20 0
71 25 0
71 35 0
1 1
70 30 1
70 30 0
Sample Output
Case 1: 2
Case 2: 0
题意:男的和女的要结婚得满足四个条件,1,男的身高不能高于女生12英尺;2,男的年龄不能大于女生5岁;3,男的和女的婚姻状况相同才能结婚;4,男的可以找单身女的结婚,反之亦然(废话)。要求输出最大配对数。
思路,就是一个二分图最大匹配模板题,只不过多了判断条件,对男的和女的分别编号,满足条件的进行配对,输出,完。
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #define N 1100 int book[N],e[N][N],match[N]; int n,m; struct criteria { int height; int age; int condition; }; criteria man[N],woman[N]; int dfs(int u) { int j; for(j = 1; j <= m; j ++) { if(!book[j]&&e[u][j]) { book[j] = 1; if(!match[j]||dfs(match[j])) { match[j] = u; return 1; } } } return 0; } int main() { int t,i,j,sum; scanf("%d",&t); int t2 = 0; while ( t--) { t2 ++; scanf("%d%d",&n,&m); for(i = 1; i <= n; i ++) scanf("%d%d%d",&man[i].height ,&man[i].age ,&man[i].condition ); for(i = 1; i <= m; i ++) scanf("%d%d%d",&woman[i].height ,&woman[i].age ,&woman[i].condition ); memset(e,0,sizeof(e)); memset(match,0,sizeof(match)); for(i = 1; i <= n; i ++) { for(j = 1; j <= m; j ++) { if(fabs(man[i].height-woman[j].height )<=12) if(fabs(man[i].age -woman[j].age )<=5) if(man[i].condition==woman[j].condition) { e[i][j] = 1; } } } sum = 0; for(i = 1; i <= n; i ++) { memset(book,0,sizeof(book)); if(dfs(i)) sum ++; } printf("Case %d: %d\n",t2,sum); } return 0; }
以上是关于二分图匹配入门专题1H - Marriage Media light oj 1184二分图最大匹配的主要内容,如果未能解决你的问题,请参考以下文章
二分图匹配入门专题1K - Going Home hdu1533km匹配
二分图匹配入门专题1F - COURSES poj1469最大匹配--匈牙利算法模板题
二分图匹配入门专题1D - Matrix hdu2119最小顶点覆盖