POJ - 2195 Going Home(最小费用最大流)
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.
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
分析:题目让我们找最小的花费,那么我们可以将所有的house和man进行连边,并且让它们之间的流量为1,让对应的边的费用为它们之间的路程。
然后再跑 最小费用最大流,即可满足题目的要求
代码如下:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int MAX_E=10010; const int MAX_N=10010; #define INF 0x3f3f3f3f int size1,ansf,ansc,n,m; struct node { int to; int flow; int cost; int Next; }edge[MAX_E<<2]; int vis[MAX_N]; int first[MAX_N]; int dis[MAX_N]; int pre[MAX_N]; int flow[MAX_N]; int num[MAX_N]; int que[MAX_N<<3]; void add_edge(int from,int to,int flow,int cost) { size1++; edge[size1].to=to; edge[size1].flow=flow; edge[size1].cost=cost; edge[size1].Next=first[from]; first[from]=size1; } int spfa(int S,int T) { memset(dis,0x7f,sizeof(dis)); flow[S]=INF; dis[S]=0; int head=0,tail=1; que[1]=S; pre[T]=0; while(head<tail) { int p=que[++head]; vis[p]=0; for(int u=first[p];u;u=edge[u].Next) { int to=edge[u].to; if(edge[u].flow&&dis[p]+edge[u].cost<dis[to]) { dis[to]=dis[p]+edge[u].cost; pre[to]=p; num[to]=u; flow[to]=min(flow[p],edge[u].flow); if(!vis[to])vis[to]=1,que[++tail]=to; } } } return pre[T]; } void solve(int S,int T) { while(spfa(S,T)) { ansf+=flow[T]; ansc+=dis[T]; for(int now=T;now!=S;now=pre[now]) { edge[num[now]].flow-=flow[T]; edge[num[now]^1].flow+=flow[T]; } } } char map1[110][110]; void init() { size1=1; memset(first,0,sizeof(first)); ansc=0; ansf=0; } struct node3 { int x; int y; }man[110],house[110]; int S,T; int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; init(); int num_man=0; int num_house=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c",&map1[i][j]); if(map1[i][j]=='H') { num_house++; house[num_house].x=i; house[num_house].y=j; } else if(map1[i][j]=='m') { num_man++; man[num_man].x=i; man[num_man].y=j; } } S=0; T=num_man*2+1; for(int i=1;i<=num_man;i++) for(int j=1;j<=num_man;j++) { int cost=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y); add_edge(i,num_man+j,1,cost); add_edge(num_man+j,i,0,-cost); } for(int i=1;i<=num_man;i++) { add_edge(S,i,1,0); add_edge(i,S,0,0); add_edge(num_man+i,T,1,0); add_edge(T,num_man+i,0,0); } solve(S,T); cout<<ansc<<endl; } return 0; }