题目链接:https://cn.vjudge.net/contest/308879#problem/A
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的地图,地图上有若干个man和house,且man与house的数量一致。man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man。现在要求所有的man都入住house,求最小费用。
第一次写网络流(最小费用最大流),用的bin巨的板子,%大佬
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=10000;
const int MAXM=100000;
const int INF=0x3f3f3f3f;
struct Edge{
int to,next,cap,flow,cost;
//出点,同入点的上一条边的下标 ,容量,流量,花费
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,编号0~N-1
void init(int n){
N=n;//好像并没什么用 ~~~
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)//进,出,容量,花费
{ //原图中的边
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=0;
edge[tol].next=head[u];
head[u]=tol++;
//反向弧
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].cost=-cost;
edge[tol].flow=0;
edge[tol].next=head[v];
head[v]=tol++;
//这样建边原边与反向弧可以通过 异或1 相互得到
}
bool spfa(int s,int t){
queue<int> q;
for(int i=0;i<N;i++){
dis[i]=INF;
vis[i]=false;
pre[i]=-1;
}
dis[s]=0;
vis[s]=true;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost){
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;//由哪条边到的v
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
return pre[t]!=-1;//不等于-1说明有从s到t的增广路径
}
int minCostMaxflow(int s,int t,int &cost){
int flow=0;
cost=0;
while(spfa(s,t)){
int Min=INF;
//找该条增广路径上的最小容量
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
//更新流量
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
}
return flow;//返回最大流
}
char mp[MAXN][MAXN];
struct node{
int x,y;
node(){}
node(int _x,int _y){
x=_x;
y=_y;
}
}H[MAXN],M[MAXN];//房的坐标,人的坐标
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
if(n==0&&m==0) break;
int ch=0,cm=0;//房和人的数目
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
for(int j=0;j<m;j++){
if(mp[i][j]=='H'){
H[ch++]=node(i,j);
}
else if(mp[i][j]=='m'){
M[cm++]=node(i,j);
}
}
}
init(cm+ch+2);//初始化
int begin=0;//超级源点
int end=cm+ch+1;//超级汇点
for(int i=0;i<cm;i++){
addedge(begin,i+1,1,0);//超级源点,人,容量为1,花费为0
}
for(int i=0;i<ch;i++){
addedge(cm+i+1,end,1,0);//房,超级汇点,容量为1,花费为0
}
for(int i=0;i<cm;i++){
for(int j=0;j<ch;j++){
int t=abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y);
addedge(i+1,cm+j+1,1,t);//人与房
}
}
int ans=0;
minCostMaxflow(begin,end,ans);
printf("%d\n",ans);
}
return 0;
}