题目链接:http://poj.org/problem?id=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
题目翻译
cds大佬突然兴致大发准备免费送房子,m表示人,H表示房子,每个房子只能进一个人,房子数等于人数。黄弘毅为了方便起见决定要让所有人到自己的房子的距离和最小,请问这个距离和是多少?
Input
输入n和m,给出一个n*m的图
Output
输出最小距离和
这个题算是最小费用最大流的模板题。
关于最小费用最大流的求法有三种。一种是bellman_ford,一种是SPFA版,还有一种是zkw费用流
这里采用的是刘汝佳算法紫书里给出的基于bellman_ford的实现。
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=205;
const int INF=0x3f3f3f3f;
struct Edge{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF{
int n,m;
vector<Edge>edges;
vector<int>G[maxn];
int inq[maxn];//是否在队列中
int d[maxn];//Bellman-Ford
int p[maxn];//上一条弧
int a[maxn];//可改进量
void init(int n){
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost){
edges.push_back(Edge(from,to,cap,0,cost));
edges.push_back(Edge(to,from,0,0,-cost));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int s,int t,int &flow,long long &cost){
for(int i=0;i<n;i++)d[i]=INF;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;
queue<int>Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
inq[u]=0;
for(int i=0;i<(int)G[u].size();i++){
Edge &e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]){Q.push(e.to);inq[e.to]=1;}
}
}
}
if(d[t]==INF)return false;
flow+=a[t];
cost+=(long long)d[t]*(long long)a[t];
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return true;
}
//需要保证初始网络中没有负权圈
int MincostMaxflow(int s,int t,long long &cost){
int flow=0;cost=0;
while(BellmanFord(s,t,flow,cost));
return flow;
}
}MM;
pair<int,int> people[105];
pair<int,int> house[105];
char str[105];
int my_abs(int x){
if(x>0) return x;
else return -x;
}
long long mincost;
int n,m;
int main(){
while(~scanf("%d%d",&n,&m)&&n&&m){
int people_num=0;
int house_num=0;
for(int i = 0;i<n;++i){
scanf("%s",str);
for(int j = 0;j<m;++j){
if(str[j]=='H') people[people_num].first=i,people[people_num++].second=j;
if(str[j]=='m') house[house_num].first=i,house[house_num++].second=j;
}
}
MM.init(house_num+people_num+7);
for(int i = 0;i<people_num;++i){
for(int j = 0;j<house_num;++j){
int dis=my_abs(people[i].first-house[j].first)+my_abs(people[i].second-house[j].second);
MM.AddEdge(i,people_num+j,1,dis);
}
}
int s=house_num+people_num;
int t=house_num+people_num+1;
for(int i = 0;i<people_num;++i)
MM.AddEdge(s,i,1,0);
for(int i = 0;i<house_num;++i)
MM.AddEdge(people_num+i,t,1,0);
int maxflow=MM.MincostMaxflow(s,t,mincost);
printf("%lld\n",mincost);
}
return 0;
}