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 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
随机推荐
- 【读书笔记】《Effective Java》——目录
第二章——创建和销毁对象 第1条:考虑用静态工厂方法替代构造器 第2条:遇到多个构造器参数时要考虑用构建器 第3条:用私有构造器或者枚举类型强化Singleton属性 第4条:通过私有构造器强化不可实 ...
- 第六节 etc/passwd 、etc/shadow 、 useradd 、 groupadd
调优方法原理:禁用atime的修改来节省cpu和内存资源.命令:mount noatime disk 1.配置文件1. /etc/passwd文档结构:由":" 分隔成7个字段&q ...
- java 多线程,T1 T2 T3 顺序执行
一.程序设计 1.抽象公共类PublicThread,具有先前线程属性previousThread.父类为Thread 2.在PublicThread的run()方法中判断previousThread ...
- Qt仿win7自动顶部最大化左侧右侧半屏效果
Win7系统不得不说是非常好用的,也是目前为止占用份额最大的操作系统,其中win7有个效果,将窗体拖动到顶部时会自动最大化,拖动到左侧右侧时会自动半屏显示,再次拖动窗体到其他位置,会重新恢复之前的大小 ...
- SQLServer 查看SQL语句的执行时间
在MSSQL Server中通过查看SQL语句执行所用的时间,来衡量SQL语句的性能. 通过设置STATISTICS我们可以查看执行SQL时的系统情况.选项有PROFILE,IO ,TIME.介绍如下 ...
- C#winform程序关闭计算机的正确姿势
/// <summary> /// 计算机电源控制类 /// </summary> public class EnvironmentCheckClass { [DllImpor ...
- Web程序员必备的CSS工具
对于web开发来说,CSS是最有效的美化页面.设置页面布局的技术.但问题是,CSS是一种标记性语言,语法结构非常的松散.不严谨.WEB程序员会经常发现自己的或别人的CSS文件里有大量的冗余代码或错误或 ...
- Go同步和异步执行多个任务封装
同步执行类RunnerAsync 支持返回超时检测,系统中断检测 错误常量定义 //超时错误 var ErrTimeout = errors.New("received timeout&qu ...
- Docker三十分钟快速入门(下)
一.背景 上篇文章我们进行了Docker的快速入门,基本命令的讲解,以及简单的实战,那么本篇我们就来实战一个真实的项目,看看怎么在产线上来通过容器技术来运行我们的项目,来达到学会容器间通信以及dock ...
- 史上最简单的js+css3实现时钟效果
今天我看到百度搜索的时间那个效果不错,于是就产生了模仿一下的效果,不过为了节省时间,就随便布了下局,废话不多说,先看看效果吧,顺便把百度的效果也拿过来. 对比样子差了好多啊,但是基本功能都是实现了的, ...