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
Sample Output
2 10 28
Source
Recommend
lwg | We have carefully selected several similar problems for you: 3416 3491 3338 3081 1569
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
费用流~
用的是紫书上的MCMF算法,好像可以用另一种算法KM,把边权改成负的再跑就可以了~
建图的话,先在输入的时候记下人和房子的横纵坐标,人有k1个,房子有k2栋,然后建立一个源点0,一个汇点k1+k2+1,从源点向人连边,从房子向汇点连边,人向房子连边,跑一遍费用流就可以了~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
#define inf 999999999
int n,m,k1,k2,dis[202],a[202],ne[202],flow,cost;
bool b[202];
char s[101];
struct node{
int x,y;
}men[101],room[101];
struct edge{
int x,y,cap,flow,cost;
edge(int u,int v,int k,int z,int kkz):x(u),y(v),cap(k),flow(z),cost(kkz) {}
};
vector<edge> ed;
vector<int> p[202];
void add(int u,int v,int k,int z)
{
ed.push_back(edge(u,v,k,0,z));
ed.push_back(edge(v,u,0,0,-z));
int tot=ed.size();
p[u].push_back(tot-2);
p[v].push_back(tot-1);
}
bool findd()
{
for(int i=0;i<=n;i++) dis[i]=inf;
memset(b,0,sizeof(b));
queue<int> q;q.push(0);dis[0]=0;b[0]=1;a[0]=inf;
while(!q.empty())
{
int k=q.front();q.pop();b[k]=0;
int tot=p[k].size();
for(int i=0;i<tot;i++)
{
edge z=ed[p[k][i]];
if(z.cap>z.flow && dis[z.y]>dis[k]+z.cost)
{
dis[z.y]=dis[k]+z.cost;
ne[z.y]=p[k][i];
a[z.y]=min(a[k],z.cap-z.flow);
if(!b[z.y])
{
b[z.y]=1;q.push(z.y);
}
}
}
}
if(dis[n]==inf) return 0;
flow+=a[n];cost+=dis[n]*a[n];
for(int i=n;i!=0;i=ed[ne[i]].x)
{
ed[ne[i]].flow+=a[n];
ed[ne[i]^1].flow-=a[n];
}
return 1;
}
int main()
{
while(scanf("%d%d",&n,&m)==2 && n)
{
k1=k2=cost=flow=0;ed.clear();
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
for(int j=1;j<=m;j++)
if(s[j]=='m') men[++k1].x=i,men[k1].y=j;
else if(s[j]=='H') room[++k2].x=i,room[k2].y=j;
}
n=k1+k2+1;
for(int i=0;i<=n;i++) p[i].clear();
for(int i=1;i<=k1;i++)
{
add(0,i,1,0);
for(int j=1;j<=k2;j++)
add(i,j+k1,1,abs(men[i].x-room[j].x)+abs(men[i].y-room[j].y));
}
for(int i=k1+1;i<n;i++) add(i,n,1,0);
while(findd());
printf("%d\n",cost);
}
return 0;
}