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 ...
随机推荐
- const关键字在C和C++区别
1)C++默认为内部链接:C默认为外部链接2)在C++中,一般一个const不会创建内存空间,而是将其保存在符号表(待看).比如: ; char buf[bufsize]; 这里无需为const创建内 ...
- background-clip 背景图片做适当的裁剪
background-clip 用来将背景图片做适当的裁剪以适应实际需要. 语法: background-clip : border-box | padding-box | content-box | ...
- Linux ThunderBird Exchange 过期
在Linux上只用Web版处理邮件,就是因为找不到太好的能支持Exchange的邮件客户端.在网上无意中发现了ExQuilla这个Thunderbird的插件,试用了一下还是不错的,很方便,不过只能免 ...
- 【转】理解JavaScript之闭包
闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面写下我的学习笔记~ 闭包-无处不 ...
- Perl 关于 use strict 的用法
什么场合要用 use strict 当你的程序有一定的行数时,尤其是在一页放不下时,或者是你找不到发生错误的原因时. 为什么要用 use strict? 众多的原因之一是帮你寻找因为错误拼写造成的错误 ...
- 1095. Cars on Campus (30)
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...
- Oracle中的for语句
for语句是一个可预置循环次数的循环控制语句,他是一个循环计数器,通常是一个整形变量,通过这个循环计数器来控制循环执行的次数 语法如下: for variable_counter_name in [e ...
- awk中文手册
1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l awk 适于解决哪些问题 ? l awk 常见的解题模式为何 ? 为使读者快速掌握awk解题的模 ...
- php必看六本书
php和mysql web开发 PHP高级程序设计_模式.框架与测试.pdf PHP专业项目实例开发.pdf PHP5高级应用开发实践.pdf [深入PHP面向对象.模式与实践(第2版)].(美)赞 ...
- 在eclipse中使用jax-ws构建webservices服务端和客户端
服务端: package com.yinfu.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebServi ...