hdu1533 Going Home 最小费用最大流 构造源点和汇点


Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5504    Accepted Submission(s): 2890


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.

 


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



/*
* 题目:hdu1533 Going Home 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 题意:n个人回到n个房子,每个房子只能住一个人。求最少花费。 思路: 构造一个源点,到达所有的人,cap = 1, cost = 0; 构造一个汇点,所有的房子到汇点,cap = 1, cost = 0; 所有的人到所有的房子,cap = 1, cost = 人到房子的最短距离。 然后最小费用最大流算法。 */ #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int INF = 0x3f3f3f3f; typedef long long LL; const int N = 210; struct Edge{ int from, to, cap, flow, cost; Edge(int u,int v,int c,int f,int cost):from(u),to(v),cap(c),flow(f),cost(cost){} }; struct MCMF { int n, m, s, t; vector<Edge> edges; vector<int> G[N]; int inq[N]; int d[N]; int p[N]; int a[N]; 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,int &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 < 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 += d[t]*a[t]; int u = t; while(u!=s){ edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; u = edges[p[u]].from; } return true; } int Mincost(int s,int t) { int flow = 0, cost = 0; while(BellmanFord(s,t,flow,cost)) ; return cost; } int dis(int x,int y,int xx,int yy) { return abs(x-xx)+abs(y-yy); } }; char s[N][N]; typedef pair<int,int> P; vector<P> man; vector<P> hou; int main() { int n, m; while(scanf("%d%d",&n,&m)==2) { if(n==0&&m==0) break; man.clear(); hou.clear(); for(int i = 0; i < n; i++){ scanf("%s",s[i]); for(int j = 0; j < m; j++){ if(s[i][j]=='m'){ man.push_back(P(i,j)); } if(s[i][j]=='H'){ hou.push_back(P(i,j)); } } } MCMF mcmf; mcmf.s = 0; mcmf.t = man.size()*2+1; mcmf.init(mcmf.t); for(int i = 0; i < man.size(); i++){/// s->man mcmf.AddEdge(mcmf.s,i+1,1,0); } for(int i = 0; i < hou.size(); i++){/// hou->t mcmf.AddEdge(man.size()+i+1,mcmf.t,1,0); } ///man->hou for(int i = 0; i < man.size(); i++){ for(int j = 0; j < hou.size(); j++){ int from, to, cap, cost; from = i+1; to = man.size()+j+1; cap = 1; cost = mcmf.dis(man[i].first,man[i].second,hou[j].first,hou[j].second); mcmf.AddEdge(from,to,cap,cost); } } printf("%d ",mcmf.Mincost(mcmf.s,mcmf.t)); } return 0; }
原文地址:https://www.cnblogs.com/xiaochaoqun/p/7183461.html