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 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
随机推荐
- 转 - .net/c# 使用RabbitMQ
背景 最近需要用C#写一个Adapter来做数据传输,合作方使用的是RabbitMQ,所以我这边也要跟着写写... 在网上搜索了一些,发现园子里的这篇写的还是非常好的.虽然有点老了,我自己用的是最新的 ...
- Linux如何让进程在后台运行的三种方法详解
问题分析: 我们知道,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程.因此,我们的解决办法就有两种途径:要么让进程忽略 HUP 信号,要么让进程运 ...
- @Autowired内部实现原理
@Autowiredprivate CustomerDao customerDao; public void addCustomer() { customerDao.add ...
- 分布式监控系统Zabbix3.2添加自动发现磁盘IO并注册监控
zabbix并没有给我们提供这么一个模板来完成在Linux中磁盘IO的监控,所以我们需要自己来创建一个,在此还是在Linux OS中添加. 由于一台服务器中磁盘众多,如果只一两台可以手动添加,但服务 ...
- Java的按位操作符
本文参考:Java的位操作符 Java的位操作符用来操作整数基本数据类型中的单个"比特"(bit),即代进制位.而我们知道比特就是0和1,那么,位操作就是对这些数据进行基本的操作. ...
- Web App、Hybrid App与Native App
在这个App的时代,转战了前端,一直接触的都是pc, 离out不远了. 那么接下来,app是我接下来半年的重点,为什么是半年,因为时间不多了. 因为是前端,那么我的重心肯定是 Web App, Hyb ...
- textarea显示源代码
textarea显示源代码 近期做的项目中,有需要显示源码的效果 最开始使用了很多冗余的办法,使用<pre></pre>和<code></code>标签 ...
- 漫话JavaScript与异步·第三话——Generator:化异步为同步
一.Promise并非完美 我在上一话中介绍了Promise,这种模式增强了事件订阅机制,很好地解决了控制反转带来的信任问题.硬编码回调执行顺序造成的"回调金字塔"问题,无疑大大提 ...
- webapi框架搭建-创建项目(一)
本文只是一些基本的vs操作,供初学者参考,有基础的请查看 创建项目(二) 创建项目(三) 前言 为了从头了解webapi的技术,创建一个为空的项目 步骤 我用的是vs2017,从文件-->新建- ...
- winform socket编程之TCPListener
运行结果: 服务端代码 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...