Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2987 Accepted Submission(s): 1513
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0d77b0d8574d5186f189f53f1ecd55a5.jpeg)
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分析:此题为最小费用流基础题。建立源汇点s、t;使每人与s点连接容量为1费用为0,每个屋子与汇点连接容量为1费用为0,每个人与每个屋子连接,容量为1费用为哈曼顿距离。代码示例:#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #define M 100000+10 #define N 100000+10 #define Min(a,b)a<b?a:b #define Max(a,b)a>b?a:b #define inf 0xffffff using namespace std; typedef struct { int to,next,rom; int val,cost; }node; node E[M]; int cnt; int head[N],from[N],dis[N],pre[N],pos[N],visit[N]; int man[100+10][2],house[100+10][2]; void init() { memset(head,-1,sizeof(head)); cnt=0; } void add(int rom,int to,int val,int cost) { E[cnt].rom=rom,E[cnt].to=to,E[cnt].val=val,E[cnt].cost=cost; E[cnt].next=head[rom],head[rom]=cnt++; E[cnt].rom=to,E[cnt].to=rom,E[cnt].val=0,E[cnt].cost=-cost; E[cnt].next=head[to],head[to]=cnt++; } bool SPFA(int s,int t,int n) { int to,val,cost; for(int i=0;i<=n;i++) { pre[i]=-1,visit[i]=0,dis[i]=inf; } queue<int>Q; pre[s]=s,dis[s]=0,visit[s]=1; Q.push(s); while(!Q.empty()) { int k=Q.front(); Q.pop(); visit[k]=0; for(int i=head[k];i!=-1;i=E[i].next) { to=E[i].to; cost=E[i].cost; val=E[i].val; if(val>0&&dis[k]+cost<dis[to]) { dis[to]=dis[k]+cost; pre[to]=k; pos[to]=i; if(visit[to]) continue; visit[to]=1; Q.push(to); } } } if(pre[t]!=-1&&dis[t]<inf) return true; return false; } int MinCostFlow(int s,int t,int n) { int min,flow=0,cost=0; while(SPFA(s,t,n)) { min=inf; for(int i=t;i!=s;i=pre[i]) { min=Min(min,E[pos[i]].val); } flow+=min; cost+=dis[t]*min; for(int i=t;i!=s;i=pre[i]) { E[pos[i]].val-=min; E[pos[i]^1].val+=min; } } return cost; } int dic(int x1,int y1,int x2,int y2) { return abs(x1-x2)+abs(y1-y2); } int main() { int n,m,s,t,iman,ihouse; char x; while(~scanf("%d%d",&n,&m)&&(n+m)) { init(); iman=0,ihouse=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { cin>>x; if(x=='m') { man[iman][0]=i; man[iman][1]=j; iman++; } if(x=='H') { house[ihouse][0]=i; house[ihouse][1]=j; ihouse++; } } s=iman+ihouse+1,t=iman+ihouse+2; for(int i=0;i<iman;i++) { add(s,i,1,0); for(int j=0;j<ihouse;j++) { add(i,j+iman+1,1,dic(man[i][0],man[i][1],house[j][0],house[j][1])); } } for(int i=0;i<ihouse;i++) { add(i+iman+1,t,1,0); } printf("%d\n",MinCostFlow(s,t,t+1)); } return 0; }