light oj 1184 Marriage Media
题目:
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
题意描述:
很有意思的题目,给你m个男士和n个女士的身高、年龄、和婚姻状况,问你最多能凑成多少对。
解题思路:
二分最大匹配问题,使用匈牙利算法,改变一下判断条件即可。
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
struct P
{
int h,a,f,p;
};
struct P man[],wom[];
int m,n,book[];
int maxmatch();
int path(int u);
int main()
{
int T,c=,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(i=;i<=m;i++)
scanf("%d%d%d",&man[i].h,&man[i].a,&man[i].f);
for(i=;i<=n;i++)
scanf("%d%d%d",&wom[i].h,&wom[i].a,&wom[i].f); printf("Case %d: %d\n",c++,maxmatch());
/*for(i=1;i<=m;i++)
printf("%d号和%d号般配\n",i,man[i].p);*/
}
return ;
} int maxmatch()
{
int i,res=;
for(i=;i<=n;i++)
wom[i].p=;
for(i=;i<=m;i++)
man[i].p=;
for(i=;i<=m;i++)
{
if(!man[i].p)
{
memset(book,,sizeof(book));
res += path(i);
}
}
return res;
}
int path(int u)
{
int i;
for(i=;i<=n;i++)
{
if(!book[i] && fabs(man[u].h-wom[i].h)<=
&& fabs(man[u].a-wom[i].a)<= && man[u].f==wom[i].f)
{
book[i]=;
if(!wom[i].p || path(wom[i].p))
{
man[u].p=i;
wom[i].p=u;
//printf("男%d号和女%d号般配\n",u,man[u].p);
return ;
}
}
}
return ;
}
light oj 1184 Marriage Media的更多相关文章
- Light OJ 1011 - Marriage Ceremonies(状压DP)
题目大意: 有N个男人,和N个女人要互相匹配,每个男人和每个女人有个匹配值. 并且匹配只能是1对1的. 问所有人都匹配完成,最大的匹配值是多少? 状压DP,暴力枚举就OK了, 这个题目略坑,因为他 ...
- light oj 1011 - Marriage Ceremonies
题目大意: 给出n*n的矩阵Map,Map[i][j]代表第i个男人和第j个女人之间的满意度,求男女一一配对后,最大的满意度之和. 题目思路:状态压缩 题目可看做每行取一点,所有点不同列的情况下,各个 ...
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
随机推荐
- js功能代码大全
1.日期格式化 //化为2017-08-14 function formatDate (date) { var y = date.getFullYear(); var m = date.getMont ...
- whereis 命令详解
whereis 作用:whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 参数:-b 定位可 ...
- 使用JavaScript将图片保存至本地
在最近的开发当中,我们需要为img标签以及canvas动态绘制的图像提供下载功能,下面是经过探索后我们得出的结果. 一.Canvas 版本 // 下载Canvas元素的图片 function down ...
- vue过滤数字为2位小数点,过滤器
过滤数字为2位小数点 filters: { //保留2位小数点过滤器 不四舍五入 number(value) { var toFixedNum = Number(value).toFixed(3); ...
- VS2017 调试期间无法获取到变量值查看
只要把勾去掉就能查看变量的值了
- timeline自适应时间轴
近期项目一直有类似QQ空间那样的时间轴,来展示公司新闻动态,或者流程之类的设计UI. 每每出现,不以为然,这次总结了下,精简下 ================= ================== ...
- 客户端 未登录页面 (clearfix 分割线)
做了一个手机客户端页面,未登录的状态页面 类似 如上图所示 这样 上面列表采取80%宽度,右侧浮动.ul li设置绝对定位.左边icon图标采取决定定位. 为了防止小屏幕手机左边空白缝隙过小,事先给外 ...
- 第七章:Python基础のXML操作和面向对象(一)
本課主題 XML介绍与操作实战 shutil 模块介绍与操作实战 subprocess 模块介绍与操作实战 初探面向对象与操作实战 本周作业 XML介绍和操作实战 對於浏览器返回的字符串有以下幾種: ...
- 基于docker的 Hyperledger Fabric 多机环境搭建(下)
Docker环境部署见上一篇博客:http://www.cnblogs.com/cnblogs-wangzhipeng/p/6994541.html. 我们部署分布式容器服务后就要在上面部署Fabri ...
- python 打印几行空行、 打印不换行
2.x版本中使用print '\n' * n #n为行数 3.x版本中使用print('\n' * n) #n为行数 print 'Hello', :不会换行.[加上逗号(,)]