【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2538 | Accepted: 719 |
Description
them in her department (1 being "really want" and N being "really don't want"). In turn, each of the N candidates ranks each of the supervisors as to how much that person would like to work for that supervisor (again, 1 is "really want to work for him/her"
and N is "really don't want to work for him/her"). Given the scores that each supervisor has for each candidate, and the scores each candidate has for each manager, write a computer program to determine the "best match" of candidates to supervisors. The "best
match" is determined by finding the distribution that leads to the highest overall (i.e. sum of) satisfaction for all people. The closer a person is to her number one choice, the better. If everyone gets their number one choice, the average difference will
be 0.
Input
The next line will contain a single integer value N, 0 < N < 15, representing the number of supervisors (and the number of employees - there are N supervisors and N employees). The next N lines will be the preferences of each of the N supervisors. Each line
will contain N integer entries (1 through N for employees 1 through N), each separated by a space character, that represents the preferences of that supervisor from most preferred to least preferred. More specifically, the first entry on the line will represent
that supervisor's first choice, the second entry her second, and so on. The next N lines will be the preferences of the N employees, in the same format as the supervisors.
All lines of data in the input file will end with an empty line.
Output
with 1). On the next N lines, show each supervisor (starting with 1) followed by the employee with which she was matched (1 per line). NOTE: if there is more than one best match, matches should be listed in ascending permuted order (see sample output).
Separate each data set with an empty line.
Sample Input
2
7
1 2 3 4 5 6 7
2 1 3 4 5 6 7
3 1 2 4 5 6 7
4 1 2 3 5 6 7
5 1 2 3 4 6 7
6 1 2 3 4 5 7
7 1 2 3 4 5 6
1 2 3 4 5 6 7
2 1 3 4 5 6 7
3 1 2 4 5 6 7
4 1 2 3 5 6 7
5 1 2 3 4 6 7
6 1 2 3 4 5 7
7 1 2 3 4 5 6 2
1 2
2 1
1 2
1 2
Sample Output
Data Set 1, Best average difference: 0.000000
Best Pairing 1
Supervisor 1 with Employee 1
Supervisor 2 with Employee 2
Supervisor 3 with Employee 3
Supervisor 4 with Employee 4
Supervisor 5 with Employee 5
Supervisor 6 with Employee 6
Supervisor 7 with Employee 7 Data Set 2, Best average difference: 0.250000
Best Pairing 1
Supervisor 1 with Employee 1
Supervisor 2 with Employee 2
Source
题目大意就是n个上司与n名员工。每一个上司相应有想要搭配的员工。相同每一个员工有渴望搭配的上司。
输入第一行为N 之后n行为1~n号上司的期望 从左到右从最好到最差
相同之后n行是1~n号员工
匹配到最渴望的人值为0,否则从左到右一次加1
要求问平均期望的最小值,也就是最小值/2n
最小值用KM最小权匹配计算就可以,因为还要求输出解,有多解则输出多解。所以还要搜一下……
事实上数据非常少。找最小权匹配也用搜的也能够。
。
。
代码例如以下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const double eps = 1e-8; int mp[23][33];
int lx[33],ly[33],link[33],slack[33],next[33];
bool visx[33],visy[33],vis[33];
int n,ans,cnt; bool cal(int x)
{
visx[x] = 1; for(int y = 0; y < n; ++y)
{
if(visy[y]) continue; int t = lx[x]+ly[y]-mp[x][y];
if(t == 0)
{
visy[y] = 1;
if(link[y] == -1 || cal(link[y]))
{
link[y] = x;
return 1;
}
}
else slack[y] = min(slack[y],t);
}
return 0;
} int KM()
{
memset(link,-1,sizeof(link)); for(int i = 0; i < n; ++i)
{
memset(slack,INF,sizeof(slack));
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy)); if(cal(i)) break; int d = INF;
for(int i = 0; i < n; ++i)
if(!visy[i]) d = min(d,slack[i]); for(int i = 0; i < n; ++i)
if(visx[i]) lx[i] -= d; for(int i = 0; i < n; ++i)
if(visy[i]) ly[i] += d;
else slack[i] -= d;
}
} ans = 0;
for(int i = 0; i < n; ++i)
if(link[i] != -1) ans += mp[link[i]][i]; return -ans;
} void dfs(int id,int hs)
{
if(hs < ans) return;
if(id == n)
{
if(hs == ans)
{
printf("Best Pairing %d\n",++cnt);
for(int i = 0; i < n; ++i)
{
printf("Supervisor %d with Employee %d\n",i+1,next[i]+1);
}
}
return;
} for(int i = 0; i < n; ++i)
{
if(vis[i]) continue;
vis[i] = 1;
next[id] = i;
dfs(id+1,hs+mp[id][i]);
vis[i] = 0;
}
} int main()
{
int t,x;
scanf("%d",&t); for(int z = 1; z <= t; ++z)
{
scanf("%d",&n);
memset(ly,0,sizeof(ly)); for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
{
scanf("%d",&x);
mp[x-1][i] = -j;
} for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
{
scanf("%d",&x);
mp[i][x-1] -= j;
if(j == 0) lx[i] = mp[i][x-1];
else lx[i] = max(lx[i],mp[i][x-1]);
} printf("Data Set %d, Best average difference: %.6f\n",z,KM()*0.5/n); cnt = 0;
memset(vis,0,sizeof(vis));
dfs(0,0);
puts("");
} return 0;
}
【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)的更多相关文章
- poj 2195(KM求最小权匹配)
题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解
题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...
- POJ 2400 Supervisor, Supervisee(KM)
題目鏈接 題意 :N个部门和N个员工,每个部门要雇佣一个工人,部门对每个工人打分,从1~N,1表示很想要,N表示特别不想要,每个工人对部门打分,从1~N.1表示很想去这个部门,N表示特别不想去这个部门 ...
- Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配
/** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 ...
- poj 3686(拆点+最小权匹配)
题目链接:http://poj.org/problem?id=3686 思路:显然工件为X集,机器为Y集合.由于每个机器一次只能加工一个部件,因此我们可以将一台机器拆成N个点,至于部件与机器之间连多大 ...
- POJ 2400 最小权匹配
吐槽:首先,这道题的输入居然是错的.要将上下两个矩阵的位置换一下才可以出样例,也就是上面那个矩阵是employee对Supervisor的打分,下面那个矩阵才是Supervisor对employee的 ...
- poj3565 Ants km算法求最小权完美匹配,浮点权值
/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...
- Poj(3686),最小权匹配,多重匹配,KM
题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...
随机推荐
- 牛客小白月赛6 指纹锁(set容器的骚操作)
原题地址: 题目描述 HA实验有一套非常严密的安全保障体系,在HA实验基地的大门,有一个指纹锁. 该指纹锁的加密算法会把一个指纹转化为一个不超过1e7的数字,两个指纹数值之差越小,就说 ...
- 中国石油大学(华东)OJ题目的HTML爬取
这几天刷华东OJ的题,写博客还要复制HTML的代码,感觉麻烦的一批,然后就去摸鱼写了个小爬虫.. 看一下运行效果吧- 输入详细的pid.cid或id即可爬取相应的html代码 一些注意要点: 关键的还 ...
- Redis2.8.7配置文件说明
Redis master配置文件说明 daemonize no 默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes daemonize yes # 当redis在后台运 ...
- stack栈和Queue队列
1.push将对象插入 System.Collections.Generic.Stack<T> 的顶部. Stack st = new Stack(); //栈是先进后出 st.Push( ...
- mysql如何用jsp代码进行数据库备份
mysql如何用jsp代码进行数据库备份 //导出 String mysql="mysqldump -uroot -proot --opt databasename > d:/test ...
- linux-shell父子进程
用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令声明变量,也可以创建并运行 shell脚本程序.运行shell脚本程序时,系统将创建一个 ...
- Java杂谈1——虚拟机内存管理与对象访问
1.理解JAVA虚拟机的内存管理 运行时的数据区 从java虚拟机的内存分配来看,一个java程序运行时包含了如下几个数据区: a) 程序计数寄存器(Program Counter Regis ...
- iptables 要点总结
http://jiayu0x.com/2014/12/02/iptables-essential-summary/
- Winform打砖块游戏制作step by step第6节---画墙(砖块集合)以及双缓冲实现
一 引子 为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持. 预备知识,无GDI画图基础的童鞋请先阅读一篇文章让你彻底弄懂WinFor ...
- Nand 的几个名词:oob,bbt,ecc
转:http://blog.csdn.net/lanmanck/article/details/4230904 例如Samsung K9F1208U0B,数据存储容量为64MB,采用块页式存储管理.8 ...