POJ 2400 Supervisor, Supervisee(KM)
題意 :N个部门和N个员工,每个部门要雇佣一个工人,部门对每个工人打分,从1~N,1表示很想要,N表示特别不想要,每个工人对部门打分,从1~N。1表示很想去这个部门,N表示特别不想去这个部门,求一个匹配,使每个人的希望值最大。
思路 :KM算法。资料。用深搜构造所有能达到最大值的匹配情况。参考
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; const int maxn = ;
int lx[maxn],ly[maxn] ;
int left[maxn], n;
bool S[maxn],T[maxn] ;
int w[maxn][maxn] ;
int slack[maxn] ;
int y[maxn],cnt ;
int ans; bool match(int x)
{
S[x] = true ;
for(int j = ; j <= n ; j++)
{
if(lx[x]+ly[j] == w[x][j] && !T[j])
{
T[j] = true ;
if(!left[j] || match(left[j]))
{
left[j] = x ;
return true ;
}
}
else slack[j] = min(slack[j],w[x][j]-lx[x]-ly[j]) ;
}
return false ;
} void KM()
{
for(int i = ; i <= n ; i++)
{
left[i] = ly[i] = ;
lx[i] = ;
for(int j = ; j <= n ; j++)
lx[i] = min(lx[i],w[i][j]) ;
}
for(int x = ; x <= n ; x++)
{
for(int i = ; i <= n ; i++)
slack[i] = ;
while()
{
memset(S,false,sizeof(S)) ;
memset(T,false,sizeof(T)) ;
if(match(x)) break ;
int tmp = ;
for(int i = ; i <= n ; i++)
if(!T[i])
tmp = min(tmp,slack[i]) ;
if(tmp == )return ;
for(int i = ; i <= n ; i++)
{
if(S[i]) lx[i] += tmp ;
if(T[i]) ly[i] -= tmp ;
}
}
}
} void dfs(int t,int sum)
{
if(sum > ans) return ;
if(t > n)
{
if(sum != ans) return ;
printf("Best Pairing %d\n",++cnt) ;
for(int j = ; j <= n ; j++)
printf("Supervisor %d with Employee %d\n",j,y[j]) ;
return ;
}
for(int i = ; i <= n ; i++)
{
if(!T[i])
{
y[t] = i ;
T[i] = true ;
dfs(t+,sum+w[t][i]) ;
T[i] = false ;
}
}
return ;
}
int main()
{
int t,x ;
scanf("%d", &t) ;
for(int k = ; k <= t ; k++)
{
for(int i = ; i <= n ; i++)
for(int j = ; j <= n ; j++)
w[i][j] = ;
scanf("%d",&n) ;
for(int i = ; i <= n ; i++)
for(int j = ; j < n ; j++)
{
scanf("%d",&x) ;
w[x][i] += j ;
}
for(int i = ; i <= n ; i++)
for(int j = ; j < n ; j++)
{
scanf("%d",&x) ;
w[i][x] += j ;
}
KM() ;
ans = ;
cnt = ;
for(int i = ; i <= n ; i++)
if(left[i])
ans += w[left[i]][i] ;
printf("Data Set %d, Best average difference: %.6f\n",k,ans/(2.0*n)) ;
memset(T,false,sizeof(T)) ;
dfs(,) ;
printf("\n") ;
}
return ;
}
POJ 2400 Supervisor, Supervisee(KM)的更多相关文章
- 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求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
- POJ 1486 Sorting Slides (KM)
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2831 Accepted: 1076 De ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- poj 1284 Primitive Roots (原根)
Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS Memory Limit: 10000K Descr ...
随机推荐
- Z-Stack学习笔记
Technorati 标签: Z-Stack profile 1. 栈配置profile 栈参数的集合需要被配置为一定的值,连同这些值在一起被称之为栈配置.ZigBee联盟定义了这些由栈配置组成的栈参 ...
- 对 Sea.js 进行配置(一) seajs.config
可以对 Sea.js 进行配置,让模块编写.开发调试更方便. seajs.config seajs.config(options) 用来进行配置的方法. seajs.config({ // 别名配置 ...
- linux命令之grep用法介绍
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- Win7在CMD命令行中使用管理员权限运行命令
使用命令: runas /user:administrator 需要执行的命令 如下:
- 多线程更新已排序的Datagridview数据,造成数据错位
多线程更新已排序的Datagridview数据,触发Datagridview的auto-sort时间,数据重新排序,造成后面更新数据的更新错误. 解决方法: 方法一.设置Datagridview的表头 ...
- php基础小知识
1.php中的双引号可以正确的解析变量与转义序列,而单引号只会按照声明原样显示:双里面的字段会经过编译器解释,然后再当作HTML代码输出:单引号里面的不进行解释,直接输出. 2.转义序列是针对源代码的 ...
- How to enables AX email functionality without Outlook
/***************************************************************** (C) Copyright DENTSPLY Internatio ...
- mac os使用homebrew来管理后台服务
在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...
- andriod
谷歌提供的安卓源码中,网址如下:http://androidxref.com http://blog.csdn.net/zoe6553/article/details/6622258
- cppcheck使用
一.splint介绍 splint是一个动态检查C语言程序安全弱点和编写错误的程序.splint会进行多种常规检查,包括未使用的变量,类型不一致,使用未定义变量,无法执行的代码,忽略返回值,执行路径未 ...