HUD:2853-Assignment(KM算法+hash)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2853
Assignment
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Last year a terrible earthquake attacked Sichuan province. About 300,000 PLA soldiers attended the rescue, also ALPCs. Our mission is to solve difficulty problems to optimization the assignment of troops. The assignment is measure by efficiency, which is an integer, and the larger the better.
We have N companies of troops and M missions, M>=N. One company can get only one mission. One mission can be assigned to only one company. If company i takes mission j, we can get efficiency Eij.
We have a assignment plan already, and now we want to change some companies’ missions to make the total efficiency larger. And also we want to change as less companies as possible.
Input
For each test case, the first line contains two numbers N and M. N lines follow. Each contains M integers, representing Eij. The next line contains N integers. The first one represents the mission number that company 1 takes, and so on.
1<=N<=M<=50, 1
Output
For each the case print two integers X and Y. X represents the number of companies whose mission had been changed. Y represents the maximum total efficiency can be increased after changing.
Sample Input
3 3
2 1 3
3 2 4
1 26 2
2 1 3
2 3
1 2 3
1 2 3
1 2
Sample Output
2 26
1 2
解题心得:
题意就是先给你一个匹配,要你重新匹配,匹配之后的权值最大,并且要求你改变原先匹配次数要最小。
匹配出来的权值最大,就是一个KM算法,但是要改变次数最小就要一点小技巧了,原本想的是在原先的匹配方案上加一就可以了,这样在面对大小一样的情况下可以优先选择,但是有个问题就是很可能加一之后和另一个更大值重合了。所以需要先乘以一个很大的值,在加一就可以可以避免重合的问题,也就是hash的问题。然后再直接对比现在的匹配情况和之前的匹配情况。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 55;
int maps[maxn][maxn];
int ly[maxn],lx[maxn],match[maxn],pre_match[maxn],slack[maxn];
bool visx[maxn],visy[maxn];
int n,m;
void init()
{
memset(ly,0,sizeof(ly));
memset(lx,0,sizeof(lx));
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%d",&maps[i][j]);
maps[i][j] *= 100;
if(maps[i][j] > lx[i])
lx[i] = maps[i][j];
}
for(int i=1;i<=n;i++)
scanf("%d",&pre_match[i]);
}
bool dfs(int x)
{
visx[x] = true;
for(int i=1;i<=m;i++)
{
if(visy[i])
continue;
int temp;
temp = lx[x] + ly[i] - maps[x][i];
if(!temp)
{
visy[i] = true;
if(match[i] == -1 || dfs(match[i]))
{
match[i] = x;
return true;
}
}
else if(temp < slack[i])
slack[i] = temp;
}
return false;
}
int get_pre_sum()
{
int sum = 0;
for(int i=1;i<=n;i++)
{
sum += maps[i][pre_match[i]]/100;
maps[i][pre_match[i]]++;
if(maps[i][pre_match[i]] > lx[i])
lx[i] = maps[i][pre_match[i]];
}
return sum;
}
void KM()
{
int pre_sum = get_pre_sum();
for(int i=1;i<=n;i++)
{
memset(slack,0x3f,sizeof(slack));
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(dfs(i))
break;
int temp = 0x3f3f3f3f;
for(int j=1;j<=m;j++)
if(!visy[j] && temp > slack[j])
temp = slack[j];
for(int j=1;j<=n;j++)
{
if(visx[j])
lx[j] -= temp;
}
for(int j=1;j<=m;j++)
{
if(visy[j])
ly[j] += temp;
else
slack[j] -= temp;
}
}
}
int ans1,ans2;
ans1 = ans2 = 0;
for(int i=1;i<=m;i++)
ans1 += maps[match[i]][i];
for(int i=1;i<=n;i++)
{
if(match[pre_match[i]] != i)//前后对比
ans2++;
}
printf("%d %d\n",ans2,ans1/100-pre_sum);
//也可以写成:printf("%d %d\n",ans1%100,ans1/100-pre_sum);
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
KM();
}
return 0;
}
HUD:2853-Assignment(KM算法+hash)的更多相关文章
- hdu 2853 Assignment KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Last year a terrible earthquake attacked Sichuan ...
- 【HDU 2853】 KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题意:有n个公司,m个任务,每个公司做每个任务都有一个效率值,最开始每个公司都指派了一个任务,现 ...
- HDU 2853 Assignment(KM最大匹配好题)
HDU 2853 Assignment 题目链接 题意:如今有N个部队和M个任务(M>=N),每一个部队完毕每一个任务有一点的效率,效率越高越好.可是部队已经安排了一定的计划,这时须要我们尽量用 ...
- hdu 2426 Interesting Housing Problem 最大权匹配KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...
- 【UVA 11383】 Golden Tiger Claw (KM算法副产物)
Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gong Wu. But EvilBoy Geni ...
- KM算法专题
原文:http://972169909-qq-com.iteye.com/blog/1184514 题目地址:这里. 1)求图中所有环的总长度(环的长度不唯一)的最小值.当无法得到完备匹配时说明环不存 ...
- 匈牙利算法与KM算法
匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...
- 【HDU2255】奔小康赚大钱-KM算法
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...
随机推荐
- ruby YAML.load 和YAML.load_file区别
1. load( io ) Load a document from the current io stream. File.open( 'animals.yaml' ) { |yf| YAML::l ...
- java的三大特性之一多态概述
多态---概念 所谓多态就是一个引用在不同情况下的多种状态.多态是指通过指向父亲的指针,来调用在不同的子类中实现的方法. 多态---注意事项 00.java允许父类的引用变量引用它的子类的实例(对象) ...
- Android Doze模式源码分析
科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...
- SQLServer 2012 Always on配置全过程
AlwaysOn取数据库镜像和故障转移集群之长.AlwaysOn不再像故障转移集群那样需要共享磁盘,从而主副本和辅助副本可以更容易的部署到不同的地理位置:AlwaysOn还打破了镜像只能1对1的限制, ...
- Python +selenium之设置元素等待
注:本文转载http://www.cnblogs.com/mengyu/p/6972968.html 当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给元素的定位增加了困难.如果因为在加 ...
- asterisk-java ami2 事件监听
asteriskServer文章1提到啦怎么获取,就不解释 asteriskServer.addChainListener(new AsteriskeventListenerInit());//整个服 ...
- javaSe-反射2
//创建实体类 package com.java.chap07.sec03; public class Student { private String name; private Integer a ...
- 【Python图像特征的音乐序列生成】关于图像特征的描述词
查阅了很久的资料,决定依据Yoshida的<Image retrieval system using impression words>这篇论文里的词语来定义. Yoshida 等的 Ar ...
- C# 生成条形码图片
在网上看到一些人写关于条形码的代码都很长,有的甚至拿来卖,所以查了下资料,希望能对大家有帮助. 我的实现原理是: 其实Windows本身就有一个字体是用来显示条形码的. 只要将数字改为这种字体就变成了 ...
- VB SMTP用户验证发送mail
转自 http://www.jishuzh.com/program/vb-smtp%E7%94%A8%E6%88%B7%E9%AA%8C%E8%AF%81%E5%8F%91%E9%80%81mail. ...