HDU 1533 Going Home(KM算法模板样例)
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4927 Accepted Submission(s): 2594
Problem Description
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.
![](http://acm.hdu.edu.cn/data/images/1533.jpg)
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.
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.
![](http://acm.hdu.edu.cn/data/images/1533.jpg)
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.
Input
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.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
题意:求最小花费。KM通常是来求最大完美匹配,这里只需要把权重变为负数。最后再变回来即可
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #define N 110 5 #define inf 0x3fffffff 6 using namespace std; 7 int mapp[N][N]; 8 struct Kuhn_Munkras{ 9 int nx,ny; 10 int linker[N],lx[N],ly[N],slack[N]; 11 bool visx[N],visy[N]; 12 void init(int n){ 13 this->nx=n; 14 this->ny=n; 15 memset(linker,-1,sizeof(linker)); 16 memset(ly,0,sizeof(ly)); 17 } 18 int dfs(int x){ 19 visx[x]=1; 20 for(int y=1;y<=ny;y++){ 21 if(!visy[y]){ 22 int tmp=lx[x]+ly[y]-mapp[x][y]; 23 if(tmp==0){ 24 visy[y]=true; 25 if(linker[y]==-1||dfs(linker[y])){ 26 linker[y]=x; 27 return 1; 28 } 29 } 30 else if(slack[y]>tmp){ 31 slack[y]=tmp; 32 } 33 } 34 } 35 return 0; 36 } 37 int solve(int n){ 38 init(n); 39 for(int i=1;i<=nx;i++){ 40 lx[i]=-inf; 41 for(int j=1;j<=ny;j++){ 42 if(mapp[i][j]>lx[i]){ 43 lx[i]=mapp[i][j]; 44 } 45 } 46 } 47 for(int x=1;x<=nx;x++){ 48 for(int i=1;i<=ny;i++){ 49 slack[i]=inf; 50 } 51 while(true){ 52 memset(visx,false,sizeof(visx)); 53 memset(visy,false,sizeof(visy)); 54 if(dfs(x)){ 55 break; 56 } 57 int d=inf; 58 for(int i=1;i<=ny;i++){ 59 if(!visy[i]&&d>slack[i]){ 60 d=slack[i]; 61 } 62 } 63 for(int i=1;i<=nx;i++){ 64 if(visx[i]){ 65 lx[i]-=d; 66 } 67 } 68 for(int i=1;i<=ny;i++){ 69 if(visy[i]){ 70 ly[i]+=d; 71 } 72 else{ 73 slack[i]-=d; 74 } 75 } 76 } 77 } 78 int res=0; 79 for(int i=1;i<=ny;i++){ 80 if(linker[i]!=-1){ 81 res+=mapp[linker[i]][i]; 82 } 83 } 84 return res; 85 } 86 }KM; 87 struct node{ 88 int x,y; 89 }mans[N],houses[N]; 90 int main(){ 91 int n,m; 92 char str; 93 int man,house; 94 cin.sync_with_stdio(false); 95 while(cin>>n>>m&&(n||m)){ 96 man=1; 97 house=1; 98 for(int i=1;i<=n;i++){ 99 for(int j=1;j<=m;j++){ 100 cin>>str; 101 if(str=='m'){ 102 mans[man].x=i; 103 mans[man++].y=j; 104 } 105 if(str=='H'){ 106 houses[house].x=i; 107 houses[house++].y=j; 108 } 109 } 110 } 111 for(int i=1;i<man;i++){ 112 for(int j=1;j<house;j++){ 113 mapp[i][j]=abs(mans[i].x-houses[j].x)+abs(mans[i].y-houses[j].y); 114 mapp[i][j]=-mapp[i][j]; 115 } 116 } 117 cout<<-KM.solve(man-1)<<endl; 118 } 119 return 0; 120 }
2017-03-07 16:10:41