Going Home
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. 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 |
题意:
每一个m要跑到h,求费用。
POINT:
给每一个m到每一个h建边。权就是费用。
#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>
using namespace std;
#define LL long long
typedef pair<int,int> pr;
const int maxn =222;
const int inf = 0x3f3f3f3f;
struct node
{
int x,y;
};
node H[maxn],M[maxn];
int s,t;
struct edge
{
int from,to,cap,flow,cost;
edge(int u,int v,int c,int f,int dd):
from(u),to(v),cap(c),flow(f),cost(dd){}
};
vector<int>G[maxn];
vector<edge>len;
char mp[maxn][maxn];
int hnum,mnum;
void add(int u,int v,int cap,int cost)
{
len.push_back(edge(u,v,cap,0,cost));
len.push_back(edge(v,u,0,0,-cost));
G[u].push_back(len.size()-2);
G[v].push_back(len.size()-1);
}
bool spfa(int &ans)
{
int dis[maxn],pre[maxn],inq[maxn];
for(int i=1;i<=t;i++) dis[i]=inf;
memset(inq,0,sizeof inq);
memset(pre,-1,sizeof pre);
dis[0]=0;
pre[0]=0;
inq[0]=1;
int a=inf;
queue<int> q;
q.push(0);
while(!q.empty())
{
int u=q.front();q.pop();inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
edge e = len[G[u][i]];
if(e.cap>e.flow&&dis[e.to]>dis[u]+e.cost)
{
dis[e.to]=dis[u]+e.cost;
a=min(a,e.cap-e.flow);
pre[e.to]=G[u][i];
if(!inq[e.to])
{
q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(dis[t]==inf) return 0;
ans+=dis[t];
int u=t;
while(u!=s)
{
len[pre[u]].flow+=a;
len[pre[u]^1].flow-=a;
u=len[pre[u]].from;
}
return 1;
}
int mincost()
{
int cost=0;
while(spfa(cost))
{
}
return cost;
}
void init()
{
hnum=mnum=0;
for(int i=0;i<=maxn;i++)
G[i].clear();
len.clear();
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
if(n==m&&m==0) return 0;
init();
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='H')
{
hnum++;
H[hnum].x=i;
H[hnum].y=j;
}
else if(mp[i][j]=='m')
{
mnum++;
M[mnum].x=i;
M[mnum].y=j;
}
}
}
s=0,t=hnum+mnum+1;
for(int i=1;i<=hnum;i++)
{
for(int j=1;j<=mnum;j++)
{
add(i,hnum+j,1,abs(H[i].x-M[j].x)+abs(H[i].y-M[j].y));//u,v,cap,d;
}
}
for(int i=1;i<=hnum;i++)
add(0,i,1,0);
for(int i=1;i<=mnum;i++)
add(hnum+i,t,1,0);
printf("%d\n",mincost());
}
}