D - Going Home POJ - 2195
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
给你一个N*M的点图(人数和房子数自己数,但是房子数会等于人数),每一个人走一格要一美元,每一个格子可以存在多个人,但是一个房子只能最终住一个人(路过没关系)问你如何安排使得最后的每一个人都在房子里且花费最小,输出最小的花费。
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
const int INF=0x7fffffff;
const int maxn=100005;
int n,m;
int st=0,en=100004;
char grid[105][105];
int dis[maxn];
int vis[maxn];
int pre[maxn];//记录增广路径上 到达点i的边的编号
//最小费用最大流模版.求最大费用最大流建图时把费用取负即可。
//无向边转换成有向边时需要拆分成两条有向边。即两次加边。
struct Edge
{
int u;
int v;
int cap;
int cost;
int next;
}edge[4*maxn];
struct node
{
int x,y;
}house[maxn];
int tol;
int head[maxn];
void init()
{
memset(head,-1,sizeof(head));
tol=0;
}
void add_edge(int u,int v,int cap,int cost)
{
edge[tol].u=u;edge[tol].v=v;edge[tol].cap=cap;
edge[tol].cost=cost;edge[tol].next=head[u];
head[u]=tol++;
edge[tol].u=v;edge[tol].v=u;edge[tol].cap=0;
edge[tol].cost=-cost;edge[tol].next=head[v];
head[v]=tol++;
}
bool spfa(int s,int t ,int n)//0表示没有增广路 //寻找花销最少的路径
{
//跑一遍SPFA 找s——t的最少花销路径 且该路径上每一条边不能满流
//若存在 说明可以继续增广,反之不能
for(int i=0;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
pre[i]=-1;
}
dis[s]=0;
vis[s]=1;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for(int k=head[u];k!=-1;k=edge[k].next)
{
int v=edge[k].v;
int cost=edge[k].cost;
if(edge[k].cap && dis[v]> dis[u]+cost) //可以松弛 且 没有满流
{
dis[v]=dis[u]+cost;
pre[v]=k; //记录前驱边 的编号
if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
if(dis[t]==INF)return 0;
return 1;
}
int MCMF(int s,int t,int n)
{
int minflow;//总流量
int mincost=0;//总费用
while(spfa(s,t,n))//每次寻找花销最小的路径
{
minflow=INF;//通过反向弧 在源点到汇点的最少花费路径 找最小增广流
for(int k=pre[t];k!=-1;k=pre[edge[k].u])
{
minflow=min(minflow,edge[k].cap);
}
//增广
for(int k=pre[t];k!=-1;k=pre[edge[k].u])
{
edge[k].cap-=minflow;
edge[k^1].cap+=minflow;//增广流的花销
}
mincost+=dis[t];
}
return mincost;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
int num=0;
if(n==0&&m==0) break;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>grid[i][j];
if(grid[i][j]=='H')
{
num++;
house[num].x=i,house[num].y=j;
}
}
}
for(int i=1;i<=num;i++)
{
add_edge(st,i,1,0); //源点到门起点建立一条容量为1,费用为0的边
add_edge(i,i+5000,1,0); //门起点到门终点建立一条容量无限或1,费用为0的边
add_edge(i+10000,i+15000,1,0);//人起点到人终点建立一条容量无限或1,费用为0的边
add_edge(i+15000,en,1,0); //人终点到汇点建立一条容量为1,费用为0的边
}
int num1=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(grid[i][j]=='m')
{
num1++;
for(int k=1;k<=num;k++)
{
int t=abs(house[k].x-i)+abs(house[k].y-j);//计算人到门的曼哈顿距离,因为同一个格子可以有好多人,直接要曼哈顿距离即可
add_edge(k+5000,num1+10000,1,t);//门终点到人起点建立一条容量为1,费用为t的边
}
}
}
}
printf("%d\n",MCMF(st,en,en));
}
return 0;
}