題目鏈接

題意 :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)的更多相关文章

  1. POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解

    题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...

  2. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

  3. POJ 1486 Sorting Slides (KM)

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2831   Accepted: 1076 De ...

  4. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  5. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  6. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  7. Poj 3613 Cow Relays (图论)

    Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...

  8. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  9. poj 1284 Primitive Roots (原根)

    Primitive Roots http://poj.org/problem?id=1284 Time Limit: 1000MS   Memory Limit: 10000K       Descr ...

随机推荐

  1. How to use For loop in CruiseControl.net

    Looping through parameter values in Cruisecontrol.net Example >>>>>>>>>&g ...

  2. NSURLConnection ignore unverified certificate error when sending a synchronise request

    Private API, use with caution. As we all know, it's easy to ignore the unverified certificate error ...

  3. IOS数据解析JSON

    //非原创 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSO ...

  4. unity 3消 游戏

    3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...

  5. HttpWebResponse取不到Cookie?原来是因为被跳转了

    今天做模拟登陆的时候,发现HttpWebResponse的Cookie都为空,但是Fiddler看是有的...后来看见是302状态,才知道请求这个的时候,Response回来已经是跳转了...这样Co ...

  6. PHP时间戳

    strtotime strtotime("Today"); #今天凌晨0点的时间戳 strtotime('now');     #当前时间的时间戳 strtotime ( &quo ...

  7. PHP取二进制文件头快速判断文件类型的实现代码

    通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...

  8. PHP流程控制语句下

    ok,继续搞. while循环: while(expr){ 程序块: } 条件expr成立执行程序块,否则结束. do while循环:do while循环比while循环要多循环一次,它要先执行程序 ...

  9. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  10. PAT IO-02 整数四则运算

    /* *PAT IO-02 整数四则运算 *2015-07-30 *作者:flx413 */ #include<stdio.h> int main() { int a, b; scanf( ...