传送门: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)的更多相关文章

  1. hdu 2853 Assignment KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Last year a terrible earthquake attacked Sichuan ...

  2. 【HDU 2853】 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 题意:有n个公司,m个任务,每个公司做每个任务都有一个效率值,最开始每个公司都指派了一个任务,现 ...

  3. HDU 2853 Assignment(KM最大匹配好题)

    HDU 2853 Assignment 题目链接 题意:如今有N个部队和M个任务(M>=N),每一个部队完毕每一个任务有一点的效率,效率越高越好.可是部队已经安排了一定的计划,这时须要我们尽量用 ...

  4. 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 ...

  5. 【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 ...

  6. KM算法专题

    原文:http://972169909-qq-com.iteye.com/blog/1184514 题目地址:这里. 1)求图中所有环的总长度(环的长度不唯一)的最小值.当无法得到完备匹配时说明环不存 ...

  7. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  8. 【HDU2255】奔小康赚大钱-KM算法

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  9. HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法

    二分图最大权值匹配问题.用KM算法. 最小权值的时候把权值设置成相反数 /*-------------------------------------------------------------- ...

随机推荐

  1. linux批量替换指定文件夹中所有文件的指定内容

    命令:sed -i "s#https#http#g" `grep http -rl VEROMODA` 功能:用来替换当前目录VEROMODA文件夹及子文件夹中所有文件中的http ...

  2. MCS-51单片机的定时器/计数器概念

    一.MCS-51单片机的定时器/计数器概念 单片机中,脉冲计数与时间之间的关系十分密切,每输入一个脉冲,计数器的值就会自动累加1,而花费的时间恰好是1微秒;只要相邻两个计数脉冲之间的时间间隔相等,则计 ...

  3. Dev控件工具箱安装

    安装目录\Components\Tools,打开命令行 安装DEV工具 $ ToolboxCreator.exe /ini:toolboxcreator.ini 移除DEV工具 $ ToolboxCr ...

  4. equals()方法详解

    Java语言中equals()方法的使用可以说比较的频繁,但是如果轻视equals()方法,一些意想不到的错误就会产生.哈哈,说的有点严重了~ 先谈谈equals()方法的出身.equals()方法在 ...

  5. SubSonic框架使用图解

    简介:SubSonic框架是一个优秀的.开源的ORM映射框架,同时提供符合自身需要的代码生成器. 官方下载地址:http://subsonicproject.com/Download 明白了SubSo ...

  6. python_105_类的特殊成员方法

    aa.py class C(): def __init__(self): self.name='QiZhiguang' 类的特殊成员方法: # 1. __doc__ 表示类的描述信息 class Do ...

  7. Opencascade术语笔记。

    1. chamfer 倒角 vs fillet  圆角: 2.boolean operatiron(布尔操作): common(相加),fuse(相交),cut(相减): 3.depressions( ...

  8. E - Polycarp and Snakes

    E - Polycarp and Snakes 题意:在一个全是点的图上开始画线,每次将一行或一列任意长度染成字母,一笔染一种字母,字母必须从a开始连续到后面某个字母可以覆盖. 问所给图案是否满足 , ...

  9. linux程序安装及包管理

    程序包的封装类型: RPM软件包:扩展名为“.rpm”,使用rpm命令安装. DEB软件包:扩展名为“.deb”,使用DPKG包管理器. 源代码软件安装:程序员开发完成的原始代码,一般制作成“.tar ...

  10. java ArrayList remove 2 及正确方法

    https://www.cnblogs.com/chrischennx/p/9610853.html 正确方式 方法一,还是fori,位置前挪了减回去就行了, remove后i--: public v ...