Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1106 Accepted Submission(s): 555
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.

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.

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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
题意很简单,这是一个最小给用最大流问题,关键是在建图;
10
28
题意很简单,这是一个最小给用最大流问题,关键是在建图;
#include <iostream>
#include <cstdlib>
#define max 105
#define Max 1000000000
using namespace std;
int flow[2*max][2*max],cost[2*max][2*max];
int map[max][max],pre[2*max],dis[2*max],minflow[2*max],mark[2*max];
char str[max][max];
int q[100000];
struct ss
{
int x,y;
}house[max],man[max];
int spfa(int start,int end)
{
int l, h, i, k;
for(i=0;i<=end;i++)
{
dis[i]=Max;
pre[i]=-1;mark[i]=0,minflow[i]=Max;
}
l = h = 0;
q[l++] = start;
dis[start]=0;
mark[start]=1;
while(l>h)
{
k = q[h++];
// puts("bug");
// cout<<end<<endl;
mark[k]=0;
for(i=0;i<=end;i++)
{
if(flow[k][i]&&dis[i]>dis[k]+cost[k][i])
{
dis[i]=dis[k]+cost[k][i];
pre[i]=k;
minflow[i]=min(minflow[k],flow[k][i]);
if(!mark[i])
{
mark[i]=1;
q[l++]=i;
}
}
}
}
return dis[end]!=Max;
}
int maxflow_mincost(int start, int end)
{
int i,x,ans=0;
// cout<<end<<endl;
while(spfa(start,end))
{
x = end;
while(pre[x]!=-1)
{
flow[pre[x]][x] -= minflow[end];
flow[x][pre[x]] += minflow[end];
x=pre[x];
}
ans += dis[end];
//cout<<ans<<endl;
// puts("abc");
}
return ans;
}
int main()
{
int N, M, i, j, k1,k2 ,n;
while(scanf("%d%d",&N,&M)&&(N+M))
{
k1 = k2 = 1;
memset(flow,0,sizeof(flow));
for(int i=0;i<N;i++)
{
scanf("%s",str[i]);
for(j=0;j<M;j++)
{
if(str[i][j]=='H')
{
house[k1].x=i;
house[k1++].y=j;
}
else if(str[i][j]=='m')
{
man[k2].x=i;
man[k2++].y=j;
}
}
}
n=k1-1;
// cout<<n<<endl;
for(i=0;i<2*n+1;i++)
for(j=0;j<2*n+1;j++)
cost[i][j]=Max;
for(int i=1;i<=n;i++)
{
for(j=n+1;j<2*n+1;j++)
{
cost[i][j] = abs(house[j - n].x - man[i].x) + abs(house[j - n].y - man[i].y);//每个人到每个房子的费用
cost[j][i] = -cost[i][j];//这点很重要,千万别忘了
flow[i][j] = 1; //流量为 1
}
// 设置起点为0,终点为 2*n+1
cost[i][0]=cost[0][i]=0;
flow[0][i]=1;
cost[i + n][2 * n + 1] = cost[2 * n + 1][i + n] = 0;
flow[i + n][2*n + 1] = 1;
}
printf("%d\n", maxflow_mincost(0, 2*n + 1));
}
system("pause");
return 0;
}