HDU 1533 Going Home【km應用】http://acm.hdu.edu.cn/showproblem.php?pid=1533
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
給你一個N行M列的矩陣,其中“.”代表空地,“H”代表房子,“m”代表人,其中有n個房子和n個人。現在要求每個人進入一間房子,且人走一步需要支付1美元。
求最小需要花費多少美元才能讓所有人都進入到房子中(每個人只能進入一間房子,每個房子只能容納一個人)。
解題思路:
這道題其實就是二分圖最優匹配的變形而已。
因爲要求的其實是最小權值之和。而KM算法求的是最大權值之和。
爲此,我們可以有兩種方法來解決:
1.在建圖的時候,將每條邊的權值變爲負數。然後lx[i]初始化爲-INT_MAX,結果輸出-ans,就可以得到最小權值。
2.在建圖的時候,用一個相對的大數減去每個權值,比如現在邊爲6,8,10.我用100來減,得到的邊爲94,92,90.最後輸出時,用100*n-ans也可以得到最小的權值。
方法1代碼如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
using namespace std;
const int N = 310;
char mmap[N][N];
int map[N][N],slack[N],match[N],lx[N],ly[N];
bool visx[N],visy[N];
int n;
bool dfs(int x)
{
visx[x] = true;
for(int i=0; i<n; i++)
{
if(visy[i]) continue;
int t = lx[x] + ly[i] - map[x][i];
if(!t)
{
visy[i] = true;
if(match[i] == -1 || dfs(match[i])){
match[i] = x;
return true;
}
}
else
slack[i] = min(slack[i], t);
}
return false;
}
void km()
{
int temp;
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(match,-1,sizeof(match));
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
lx[i] = max(lx[i],map[i][j]);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++) slack[j] = INT_MAX;
while(1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(dfs(i)) break;
else{
temp = INT_MAX;
for(int j=0; j<n; j++)
{
if(!visy[j] && temp>slack[j])
temp = slack[j];
}
for(int j=0; j<n; j++)
{
if(visx[j]) lx[j] -= temp;
if(visy[j]) ly[j] += temp;
else slack[j] -= temp;
}
}
}
}
}
int main()
{
int row,col,ans,numi,numj;
while(scanf("%d %d",&row,&col)!=EOF)
{
if(row == 0 && col == 0) break;
n = ans = numi = numj = 0;
for(int i=0; i<row; i++){
scanf("%s",mmap[i]);
for(int j=0; j<col; j++)
if(mmap[i][j] == 'm') n++;
}
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(mmap[i][j] == 'm')
{
for(int k=0; k<row; k++){
for(int l=0; l<col; l++){
if(mmap[k][l] == 'H')
map[numi][numj++] = - (abs(k - i) + abs(l - j));//變爲負權邊
}
}
numi++;
numj = 0;
}
}
}
km();
for(int i=0; i<n; i++)
ans += map[ match[i] ][i];
printf("%d\n", - ans);//結果取反
}
return 0;
}
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.