传送门: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. java 通用查询框架Querydsl 简介

    Querydsl 是一个通用的查询框架,专注于通过JavaAPI构建类型安全的SQL查询说说Querydsl的优势吧: 1. Querydsl支持代码自动完成,因为才纯Java API编写查询,因此主 ...

  2. centos6.3搭建FTP服务器图文教程

    今天下午不忙,没什么事,看到我大红盟linux版块如此冷清,心不能忍,做了个FTP服务器的搭建教程,大家可以看看, 不会做视频,就图文交叉了,写得不好,望谅解.以后有时间再出一个LNMP的教程. 不磨 ...

  3. h5复制粘贴板,打开APP功能

    <div class="container"> <img src="../themes/mall/img/i_red_ad4.jpg"> ...

  4. Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法

    菜鸟日记: 1:new web project: 2:fix the visiting  path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...

  5. 传入泛型类型(T.class)的方法

    java中当我们需要T.class时会报错,这是我们只需定义一个Class<T>类作为参数传入即可,具体如下: public List<T> findStuByQuery(De ...

  6. 3285 转圈游戏 2013年NOIP全国联赛提高组

    3285 转圈游戏 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description n 个小伙伴 ...

  7. STM8 PIN setting(output)

    今日在设置引脚输出的时候,本想设置为open-drain输出,然后对其输出高低.但是发现无法输出高(初始化为开漏低电平),始终为低.后来改为push-pull 输出,就能输出高低了.真有意思,转到SP ...

  8. Python3+Selenium3+webdriver学习笔记7(选择多链接的结果、iframe、下拉框)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...

  9. python基础教程总结5——函数

    1.函数创建 1).函数代码块以def关键词开头,后接函数标识符名称和圆括号()2).任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数3).函数的第一行语句可以选择性地使用文档字符串 ...

  10. [dp uestc oj] G - 邱老师玩游戏

    G - 邱老师玩游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...