HDU 1533Going Home(費用流)

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


設一個匯點,一個源點

從源點到所有m連一條流量爲1費用爲0的邊。從所有m到H連一條流量爲1費用爲距離的邊。從所有H連一條流量爲1費用爲0的邊到匯點。

然後套個網絡流模板。這樣連得原因就是保證一一對應。不信你畫個圖自己看看。

 

AC代碼:

 

# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <math.h>
# include <stdlib.h>
# include <vector>
using namespace std;
typedef long long int ll;
struct edge{
	int to, cap, cost, rev;
};
int V;//爲節點總數
const int maxn=10010;
const int inf=2000000000; 
vector<int> H;//H的標號
vector<int> M;//M的標號
int n, m;//n爲行,m爲列
char g[110][110];
vector <edge> G[maxn];
int dist[maxn];
int preve[maxn], prevv[maxn];

void add_edge(int from, int to, int cap, int cost){//加邊 
	G[from].push_back( (edge) { to, cap, cost, G[to].size()});
	G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
}
void init(){//初始化 
	H.clear();
	M.clear();
	for(int i=0; i<10009; i++){
		G[i].clear(); 
	}
    memset(preve, 0, sizeof(preve));
    memset(prevv, 0, sizeof(prevv));
    memset(dist, 0, sizeof(dist));
}
int min_cost_flow(int s, int t, int f){//費用流算法 
	int res=0;
	while(f>0){
		fill(dist, dist+V, inf);
		dist[s]=0;
		bool update=true;
		while(update){
			update=false;
			for(int v=0; v<V; v++){
				if(dist[v]==inf) continue;
				for(int i=0; i<G[v].size(); i++){
					edge &e=G[v][i];
					if(e.cap>0&&dist[e.to]>dist[v]+e.cost){
						dist[e.to]=dist[v]+e.cost;
						prevv[e.to]=v;
						preve[e.to]=i;
						update=true;
					}	
				}
			}
		}
		if(dist[t]==inf){
			return -1;
		}
		int d=f;
		for(int v=t; v!=s; v=prevv[v]){
			d=min(d, G[prevv[v]][preve[v]].cap);
		}
		f-=d;
		res+=d*dist[t];
		for(int v=t; v!=s; v=prevv[v]){
			edge &e=G[prevv[v]][preve[v]];
			e.cap-=d;
			G[v][e.rev].cap+=d; 
		}
	}
	return res;
}

int main(){
	int i, j, k; 
	while(1){
		scanf("%d%d", &n, &m);
		getchar();
		if(m==0&&n==0){
			break;
		}
		for(i=1; i<=n; i++){
			scanf("%s", g[i]+1);
		}
		init();
		for(i=1; i<=n; i++){
			for(j=1; j<=m; j++){
				if(g[i][j]=='H'){
					H.push_back((i-1)*m+j);
				}
				if(g[i][j]=='m'){
					M.push_back((i-1)*m+j);
				}
			}
		}
		//構圖
		int Size=H.size();
	    V=Size*2+2; //定點總數,0爲原點,v-1爲匯點 
	    //s到H構圖 
        for(i=0; i<Size; i++){
        	add_edge(0, i+1, 1, 0);
		}
		//H到M
		 for(i=0; i<Size; i++){
		 	int x1, y1;
		    if(H[i]%m==0){
		    	x1=H[i]/m;y1=m;
			}
			else{
				x1=H[i]/m+1;y1=H[i]%m;
			}
			for(j=0; j<Size; j++){
		 		int x2, y2;
		 		if(M[j]%m==0){
			    	x2=M[j]/m;y2=m;
				}
				else{
					x2=M[j]/m+1;y2=M[j]%m;
				}
				add_edge(i+1, Size+j+1, 1, abs(x1-x2)+abs(y1-y2));
			}
		 }
		 //M到t 
		 for(i=0; i<Size; i++){
		 	add_edge(Size+1+i, 2*Size+1, 1, 0);
		 }
		 printf("%d\n", min_cost_flow(0, 2*Size+1, Size));
	}
	return 0;
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章