Going Home (最小权值完备匹配)

传送门HDU1533

描述

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
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.

输入

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

输出

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

样例

  • Input
    2 2
    .m
    H.
    5 5
    HH..m
    …..
    …..
    …..
    mm..H
    7 8
    …H….
    …H….
    …H….
    mmmHmmmm
    …H….
    …H….
    …H….
    0 0
  • Output
    2
    10
    28

题解

  • 题意:给出n*m的地图,其中H表示房子,m表示人,每个房子只能装一个人,每个人都要走到一个房子里去,走进去需要消费相当于人与房子间的曼哈顿距离的代价,求最小代价和。
  • 以人和房子为点,每个人与每个房子间的曼哈顿距离为边构成二分图。找出最小权值的完备匹配
  • 最小权值的完备匹配,在最大权值匹配上,将边取负,其他变,得出答案后再取负的即可(负的最大即为正的最小)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//边权改负  wx改负
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e2+7;
const int mod=1e9+7;
int line[maxn][maxn],wx[maxn],wy[maxn],visx[maxn],visy[maxn],match[maxn];
typedef struct node{int x,y;}Node;
int sn,sm,n=0,m=0;
Node man[maxn];
Node house[maxn];
int minsub=inf;
int Find(int x){
visx[x]=1;
for(int i=1;i<=m;i++){
if(visy[i]||line[x][i]==0)continue;
int t=wx[x]+wy[i]-line[x][i];
if(t==0){
visy[i]=1;
if(!match[i]||Find(match[i])){
match[i]=x;
return true;
}
}
else minsub=min(minsub,t);
}
return false;
}
int Km(){
for(int i=1;i<=n;i++){
while(1){
INIT(visx,0);INIT(visy,0);
minsub=inf;
if(Find(i))break;
for(int j=1;j<=n;j++)
if(visx[j]) wx[j]-=minsub;
for(int j=1;j<=m;j++)
if(visy[j]) wy[j]+=minsub;
}
}
int ans=0;
for(int i=1;i<=m;i++)
if(match[i]) ans+=line[match[i]][i];
return -ans;
}
int main(){
while(~scanf("%d%d",&sn,&sm)&&(sn&&sm)){
INIT(line,0);INIT(match,0);
n=0,m=0;
char s;
for(int i=1;i<=sn;i++){
getchar();
for(int j=1;j<=sm;j++){
scanf("%c",&s);
if(s=='H') house[++m]=(Node){i,j};
if(s=='m') man[++n]=(Node){i,j};
}
}
INIT(wy,0);
for(int i=1;i<=n;i++){
wx[i]=-inf;
for(int j=1;j<=m;j++){
int dis=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
line[i][j]=-dis;
wx[i]=max(wx[i],-dis);
}
}
printf("%d\n",Km());
}

return 0;
}
Donate comment here