当前位置 : 主页 > 手机开发 > harmonyos >

poj 2195 Going Home km算法

来源:互联网 收集:自由互联 发布时间:2023-08-26
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 ev


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. 




poj  2195 Going Home  km算法_#define


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


210 28


Source


Pacific Northwest 2004

#include<stdio.h> 
 
 #include<string.h>  
 #include<iostream>  
 #include<algorithm>  
 #include<math.h>  
 #include<stdlib.h>  
 using namespace std;  
 #define INF 99999999  
 #define maxn 330  


 int Map[maxn][maxn];  
 int lx[maxn],ly[maxn],match[maxn];  
 int visx[maxn],visy[maxn],w[maxn][maxn];  
 int slack[maxn];  


 struct ele  
 {  
     int x;  
     int y;  
 }px[maxn],py[maxn];  


 bool findpath(int x,int n)  
 {  
     visx[x]=1;  
     int y;  
     for(y=1;y<=n;y++)  
     {  
         if(visy[y])  
             continue;  
         if(lx[x]+ly[y]==w[x][y])  
         {  
             visy[y]=1;  
             if(!match[y]||findpath(match[y],n))  
             {  
                 match[y]=x;  
                 return true;  
             }  
         }  
         slack[y]=min(slack[y],lx[x]+ly[y]-w[x][y]);  
     }  
     return false;  
 }  
 void km(int n)  
 {  
     int i,j,temp;  
     memset(lx,0,sizeof(lx));  
     memset(ly,0,sizeof(ly));  
     memset(match,0,sizeof(match));  
     for(i=1;i<=n;i++)  
         for(j=1;j<=n;j++)  
             lx[i]=max(lx[i],w[i][j]);  
     for(i=1;i<=n;i++)  
     {  
         for(j=1;j<=n;j++)  
             slack[j]=INF;  
         while(1)  
         {  
             memset(visx,0,sizeof(visx));  
             memset(visy,0,sizeof(visy));  
             if(findpath(i,n))  
                 break;  
             temp=INF;  
             for(j=1;j<=n;j++)  
                 if(!visy[j])  
                     if(temp>slack[j])  
                         temp=slack[j];  
             if(temp==INF)  
                 break;  
             for(j=1;j<=n;j++)  
             {  
                 if(visx[j])  
                     lx[j]-=temp;  
                 if(visy[j])  
                     ly[j]+=temp;  
                 else  
                     slack[j]-=temp;  
             }  
         }  
     }  
 }  
 int main()  
 {  
     int ans;  
     int n,m;  
     int i,j;  
     char c;  
     int x,y;  
     while(scanf("%d%d",&n,&m),n&&m)  
     {  
         ans=0;  
         x=y=1;  
         for(i=1;i<=n;i++)  
             for(j=1;j<=m;j++)  
             {  
                 cin>>c;  
                 if(c=='m')  
                 {  
                     px[x].x=i;  
                     px[x].y=j;  
                     x++;  
                 }  
                 if(c=='H')  
                 {  
                     py[y].x=i;  
                     py[y].y=j;  
                     y++;  
                 }  
             }  
         for(i=1;i<x;i++)  
             for(j=1;j<y;j++)  
                 w[i][j]=-(abs(px[i].x-py[j].x)+abs(px[i].y-py[j].y));  
          km(x-1);  
         for(i=1;i<x;i++)  
            ans+=w[match[i]][i];  
         printf("%d\n",-ans);  
     }  
     return 0;  
 }

上一篇:树状数组的模板;
下一篇:没有了
网友评论