hdu trilogy Going Home minimum cost maximum flow algorithm EK

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
 **************************************************************************************************************************
The minimum cost maximum flow algorithm EK
***************************************************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<queue>
  6 #include<stack>
  7 #include<algorithm>
  8 #define inf  0x7fffffff
  9 using namespace std;
 10 int pre[501],cost[501][501];
 11 int dis[522],cap[521][521];
 12 int vis[511 ];
 13 is  int que [ 10000011 ];
 14  int n-, m, K, I, J, the src;
 15  int END1, S;
 16  char STR [ 1001 ];
 . 17  int TOP1, TOP2;
 18 is  struct Node
 . 19  {
 20 is       int X, Y;
 21 is   } H [ 1001 ], man [ 1001 ];
 22 is  
23 is  int BFS () // note that in STL timeout 
24  {
 25      for ( int IT = 0;it<=end1;it++)
 26       dis[it]=inf;
 27     memset(vis,0,sizeof(vis));
 28     int base,top;
 29     base=top=0;
 30     que[top++]=src;
 31     dis[src]=0;
 32     vis[src]=1;
 33     pre[src]=0;
 34     while(base<top)
 35     {
 36         int fs=que[base++ ];
 37 [          VIS [FS] = 0 ;
 38 is          for ( int IT = . 1 ; IT <= END1; IT ++ )
 39           {
 40              IF (! IT = FS && CAP [FS] [IT]> 0 )
 41 is               IF (DIS [ IT]> (DIS [FS] + cost [FS] [IT])) // where to find the minimum cost 
42 is               {
 43 is                  pre [IT] = FS;
 44 is                  DIS [IT] DIS = [FS] + cost [FS] [IT];
 45                  IF (! VIS [IT])
 46 is                  {
 47                     que = [Top ++] IT;
 48                      VIS [IT] = . 1 ;
 49                  }
 50               }
 51 is           }
 52 is      }
 53 is      IF (DIS [END1] == INF)
 54 is        return - . 1 ;
 55      return  . 1 ;
 56 is  
57 is  }
 58  int EK ( ) // calculates the minimum cost maximum flow 
59  {
 60      int JT, KT;
 61 is      int SM = 0 ;
 62 is      int min1=inf;
 63     while(1)
 64     {
 65         kt=bfs();
 66         if(kt==-1)
 67           break;
 68         for(int it=end1;it!=0;it=pre[it])
 69         {
 70             jt=pre[it];
 71             if(min1>cap[jt][it])
 72               min1=cap[jt][it];
 73         }
 74         for(int it=end1;it!=0;it=pre[it])
 75         {
 76             jt=pre[it];
 77             sm+=cost[jt][it]*min1;
 78             cap[jt][it]-=min1;
 79             cap[it][jt]+=min1;
 80         }
 81     }
 82     return sm;
 83 
 84 }
 85 int main()
 86 {
 87   while(scanf("%d%d",&n,&m)!=EOF)
 88   {
 89       if(n==0&&m==0)
 90          break;
 91        top1=top2=0;
 92       for(i=0;i<n;i++)
 93         {
 94             scanf("%s",str);
 95             getchar();
 96             for(j=0;j<m;j++)
 97              {
 98                if(str[j]=='H')
 99                {
100                  H[top1].x=i;
101                  H[top1++].y=j;
102                }
103               if(str[j]=='m')
104               {
105                 man[top2].x=i;
106                 man[top2++].y=j;
107               }
108              }
109         }
110         if(top1==0)
111         {
112             printf("0\n");
113             continue;
114         }
115         memset(cap,0,sizeof(cap));
116         memset(cost,0,sizeof(cost));
117         for(i=1;i<=top2;i++)
118          cap[0][i]=1;
119         for(i=1;i<=top2;i++)
120         {
121             int x1=man[i-1].x;
122             int y1=man[i-1].y;
123             for(j=1;j<=top1;j++)
124             {
125                 int x2=H[j-1].x;
126                 int y2=H[j-1].y;
127                 x2=x2-x1;
128                 if(x2<0)
129                   x2=-x2;
130                 y2=y2-y1;
131                 if(y2<0)
132                   y2=-y2;
133                 cap[i][j+top2]=1;
134                 cost[i][j+top2]=x2+y2;
135                 cost[j+top2][i]=-(x2+y2);
136 
137             }
138         }
139         for(i=1;i<=top2;i++)
140           cap[top2+i][2*top2+1]=1;
141 
142      s=0;
143      src=0;end1=2*top1+1;
144      s=EK();
145      printf("%d\n",s);
146 
147   }
148   return 0;
149 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3352750.html

Guess you like

Origin blog.csdn.net/weixin_33816946/article/details/93432942