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 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
随机推荐
- KVM(二):KVM应用
++++++++++++++++++++++++++++++创建和拍摄快照++++++++++++++++++++++++++++++++++ KVM快照方法常用的是qemu-img snapshot ...
- golang sql database drivers
https://github.com/golang/go/wiki/SQLDrivers SQL database drivers The database/sql and database/sql/ ...
- http1.0 的哑代理问题
感觉这就和回字的四种写法一样,并无卵用 原以为http1.1的新特性是提供了keep-alive,后来才知道,keep-alive选项http1.1已经不支持了,http1.1对该功能进行了改版 关于 ...
- tomcat 设置jvm 参数
在catalina.bat中设置 正确的做法是设置成这样set JAVA_OPTS=%JAVA_OPTS% -Xms256m -Xmx256m,避免JAVA_OPTS参数覆盖
- 解决反序列化(Deserialize)无法找到程序集的错误
http://blog.csdn.net/w_s_q/article/details/5677536 在使用.NET序列化对象时,会将程序集信息也包含进去.如果将序列化之后的字节数组通过网络(或其他传 ...
- canvas(七) 文字编写
/** * Created by xianrongbin on 2017/3/11. */ var dom = document.getElementById('clock'), ctx = dom. ...
- Keras:基于Theano和TensorFlow的深度学习库
catalogue . 引言 . 一些基本概念 . Sequential模型 . 泛型模型 . 常用层 . 卷积层 . 池化层 . 递归层Recurrent . 嵌入层 Embedding 1. 引言 ...
- sql server 各种等待类型-转
等待的类型 资源等待 当某个工作线程请求访问某个不可用的资源(因为该资源正在由其他某个工作线程使用,或者该资源尚不可用)时,便会发生资源等待.资源等待的示例包括锁等待.闩锁等待.网络等待以及磁盘 I/ ...
- swift4.0 正则表达式判断手机号
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Menlo; color: #ffffff; background-color: #282b3 ...
- 嵌入式Tomcat容器的参数(maxParameterCount)设定
背景 昨天同事遇到了error一起看了一下感觉比较重要在这记录一下 基本情况是页面上选中9K+的数据向后台发送请求,然后系统就崩了... error信息如下 More than the maximum ...