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 ...
随机推荐
- age
#include<iostream> #include<math.h> #define pi 3.14 using namespace std; int main() { in ...
- 解决Eclipse中Java工程间循环引用而报错的问题
如果myeclipse 报如下错误 A cycle was detected in the build path of project 如果我们的项目包含多个工程(project),而它们之间又是循 ...
- 使用ANT 生成Xfire 客户端端文件
这里需要用到的JAR包 : XmlSchema-1.1.jar activation-1.1.jar commons-codec-1.3.jar commons-httpclient-3.0.jar ...
- WIN2003跳出res://C:WINDOWSsystem32mys.dll/mys.hta解决方法
出现这个问题的时候 @echo off 请将以下语句复制到记事本中,另存为后缀为.cmd的文件,并运行.当然在命令行下一句句运行也没问题. echo 正在修复,这个过程可能需要几分钟,请稍候…… ru ...
- MyEclipse中新建html5中文乱码
近期刚开始入门html5 和 javascript 用MyEclipse2104创建html5时,遇到浏览器显示中文乱码的问题 [MyEclipse中设置的html5,jsp编码都采用的UTF-8] ...
- 初测WIN10
WIN10已经发布,通过百度直通车把WIN7升级成了WIN10,改变较大,不太习惯,用着不是很顺手. 吐槽几个问题 1.微软的Visual Studio 2015 Community版本,宣布是免费的 ...
- CSS3制作立体导航
<ul class="nav"> <li><a href="">首页</a></li> <li ...
- Android:WebView中对图片注册上下文菜单
前言 今天一朋友问我一个问题,就是如何在WebView控件中的图片增加上下文菜单,以便增加保存图片等功能.今天就给他简单做了一个演示Demo,现写下来,给有相同问题的朋友提供些许思路吧. 概要实现 其 ...
- python之域与属性
python, javascript中域与属性是二个不同的概念, 域就是变量, 而属性则是符合某些约束, 例如getter, setter...等的特殊"变量". python中使 ...
- How to install DIG dns tool on windows 7
This guide explain how to install dig dns tool on windows 7 in few steps: 1. First go to http://www. ...