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 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
随机推荐
- Git Pro读书笔记
本文为Git Pro读书笔记,所有内容均来自Git Pro 1 Git基础 1.1 记录每次更新到仓库 在Git里,文件有4种状态,modified, staged, commited, 还有一种状态 ...
- Windows,Mac与Linux哪个更适合开发者?
以前写的,怕引来口水战,干脆不发.这段时间面试了十来人,用Mac的开发水平明显高于Windows的,挺多感想的,于是改改发了吧. Windows: 对普通用户而言体验最友好,对开发者 ...
- 在vim中使用zencoding/Emmet
zencoding在vim上的插件已经改名为Emmet.vim 1. 安装,推荐使用vundle插件管理器安装,在~/.vimrc中,添加:Bundle 'Emmet.vim',输入命令vim +Bu ...
- View学习(四)-View的绘制(draw)过程
View的draw过程相比之于measrue过程,也是比较简单的.并且在我们自定义View时,也经常需要重写onDraw方法,来绘制出我们要实现的效果. 如之前的文章所说,绘制的流程也是起始于View ...
- ul li内的文字水平居中显示
head><style rel="stylesheet" type="text/css" >#top{height:140px;}#top u ...
- 【转】Tableau 9.3.8 desktop for Mac 中文破解
tableau破解版本下载地址 安装步骤: 1. 编辑hosts 文件 在终端输入:sudo nano /etc/hosts 添加如下内容: 127.0.0.1 licensing.tableauso ...
- c# 岛2 小辅助~~~ 钓鱼 连击
- sync_binlog innodb_flush_log_at_trx_commit 浅析
一 参数意义 innodb_flush_log_at_trx_commit 如果innodb_flush_log_at_trx_commit设置为0,log buffer将每秒一次地写入log fil ...
- mydumper
Mydumper介绍 Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydu ...
- [整理]k-vim-for-server通过vimrc修改vim格式
1.备份原来的vim设置: cp ~/.vimrc ~/.vimrc_bak 2. 下载配置到指定目录 法一: curl https://raw.githubusercontent.com/wklke ...