Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 641 Accepted Submission(s): 308
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
二分图匹配入门题 ,KM 算法可以解 , 当然 , 效率没那么高的费用流解法 .
下面是两种解法的代码 :
//费用流解法 #include<cstdio> #include<cmath> #include<queue> #include<string.h> #define inf 2100000000 #define MAXN 205 using namespace std; int dist[MAXN],d[MAXN][MAXN],c[MAXN][MAXN],pre[MAXN],f[MAXN][MAXN],total1,total2,mark[MAXN],dis[MAXN];; struct xy { int i,j; }home[MAXN],man[MAXN]; bool spfa(int s,int t) { int i,x; queue <int> que; bool vis[MAXN]; memset(vis,0,sizeof(vis)); for (i=0;i<=2*total1+1;++i) dis[i]=inf; dis[s]=0; que.push(s); vis[s]=1; while (!que.empty()) { x=que.front(); que.pop(); vis[x]=0; for (i=1;i<=2*total1+1;++i) if ((dis[x]+d[x][i]<dis[i])&&(c[x][i])) { dis[i]=dis[x]+d[x][i]; pre[i]=x; if (!vis[i]) {que.push(i);vis[i]=1;} } } return (dis[2*total1+1]!=inf); } int mcmf(int s,int t) { int mincost=0; memset(mark,0,sizeof(mark)); while (spfa(s,t)) { int x=t; while (x!=s) { c[pre[x]][x]-=1; //f[x][pre[x]]=-f[pre[x]][x]; c[x][pre[x]]+=1; mark[pre[t]]=mark[pre[pre[t]]]=1; x=pre[x]; } mincost+=dis[t]; } return mincost; } int main() { char cc;int n,m,i,j; while (scanf("%d%d",&n,&m)) { total1=total2=1; // memset(f,0,sizeof(f)); memset(d,127,sizeof(d)); memset(c,0,sizeof(c)); if ((n==0)&&(m==0)) break; scanf("%c",&cc); for (i=1;i<=n;++i) { for (j=1;j<=m;++j) { scanf("%c",&cc); if (cc=='H') {home[total2].i=i;home[total2++].j=j;} if (cc=='m') {man[total1].i=i;man[total1++].j=j;} } scanf("%c",&cc); } total1--;total2--; if (total1==0) printf("0/n"); else{ for (i=1;i<=total1;++i) for (j=1;j<=total2;++j) { d[i][total1+j]=abs(double(man[i].i-home[j].i))+abs(double(man[i].j-home[j].j)); d[total1+j][i]=-d[i][total1+j]; c[i][total1+j]=1; } for (i=1;i<=total1;++i) { d[0][i]=d[i][0]=0;d[total1+i][2*total1+1]=d[2*total1+1][total1+i]=0; c[0][i]=1;c[total1+i][2*total1+1]=1; //c[i][0]=0;c[2*total1+1][total1+i]=0; } printf("%d/n",mcmf(0,2*total1+1)); } } return 0; } //KM算法 #include<stdio.h> #include<cmath> #include<string.h> #define inf 2100000000 #define MAXN 105 using namespace std; struct home_man { int i,j; }home[MAXN],man[MAXN]; int res,visx[MAXN],visy[MAXN],g[MAXN][MAXN],pre[MAXN],total1,total2,lx[MAXN],ly[MAXN],lack; int find(int deal) { int i,t; visx[deal]=1; for (i=1;i<=total1;++i) { if (visy[i]) continue; t=g[deal][i]-lx[deal]-ly[i]; if (t==0) { visy[i]=1; if (pre[i]==-1||find(pre[i])) { pre[i]=deal; return g[deal][i]; } } } return 0; } void KM() { int i,j,cost,k; memset(pre,-1,sizeof(pre)); for (i=1;i<=total2;++i) for (j=1;j<=total1;++j) if (g[i][j]<lx[i]) lx[i]=g[i][j]; for (i=1;i<=total2;++i) { while (1) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); lack=inf; if (cost=find(i)) {break;} else { for (k=1;k<=total1;++k) if (visx[k]) for (j=1;j<=total2;++j) if (visy[j]==0 && lack>g[k][j]-lx[k]-ly[j]) lack=g[k][j]-lx[k]-ly[j]; for (j=1;j<=total1;++j) { if (visx[j]) lx[j]+=lack; if (visy[j]) ly[j]-=lack; } } } } for (i=1;i<=total1;++i) res+=g[pre[i]][i]; } int main() { int n,m,i,j;char c; while (scanf("%d%d",&n,&m)) { if ((n==0)&&(m==0)) break; res=0; scanf("%c",&c); total1=total2=1; memset(lx,127,sizeof(lx)); memset(ly,0,sizeof(ly)); for (i=1;i<=n;++i) { for (j=1;j<=m;++j) { scanf("%c",&c); if (c=='H') {home[total1].i=i;home[total1++].j=j;} if (c=='m') {man[total2].i=i;man[total2++].j=j;} } scanf("%c",&c); } total1--;total2--; for (i=1;i<=total2;++i) for (j=1;j<=total1;++j) { g[i][j]=abs(double(home[j].i-man[i].i))+abs(double(home[j].j-man[i].j)); } KM(); printf("%d/n",res); } return 0; }