hdu 1533 Gonging home

本文探讨了最小费用最大流问题的解决方法,通过SPFA算法和最大流最小费用算法实现,具体应用于网格地图中将人送入房屋的场景。代码示例详细展示了建图过程、费用计算和最大流最小费用的求解。

Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1106    Accepted Submission(s): 555


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.
1533.jpg
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

题意很简单,这是一个最小给用最大流问题,关键是在建图;
#include <iostream>
#include <cstdlib>
#define max 105
#define Max  1000000000

using namespace std;
int flow[2*max][2*max],cost[2*max][2*max];
int map[max][max],pre[2*max],dis[2*max],minflow[2*max],mark[2*max];
char str[max][max];
int q[100000];
struct ss
{
   int x,y;       
}house[max],man[max];

int spfa(int start,int end)
{
    int l, h, i, k;
    for(i=0;i<=end;i++)
    {
      dis[i]=Max;
      pre[i]=-1;mark[i]=0,minflow[i]=Max;                   
    }   
    l = h = 0;
    q[l++] = start;
    dis[start]=0;
    mark[start]=1;
    while(l>h)
    {
        k = q[h++];
       //  puts("bug");
       //  cout<<end<<endl;
        mark[k]=0;     
        for(i=0;i<=end;i++)  
        {
           if(flow[k][i]&&dis[i]>dis[k]+cost[k][i]) 
           {                    
              dis[i]=dis[k]+cost[k][i];
              pre[i]=k;
              minflow[i]=min(minflow[k],flow[k][i]);
              if(!mark[i])
              {
                 mark[i]=1;
                 q[l++]=i;            
              }
        }   
    } 
  }
   return dis[end]!=Max;
}
int maxflow_mincost(int start, int end) 
{    
     int i,x,ans=0;
    // cout<<end<<endl;
     while(spfa(start,end))
     {
         x = end;
         while(pre[x]!=-1)
         {
            flow[pre[x]][x] -= minflow[end];
            flow[x][pre[x]] += minflow[end];
            x=pre[x];                 
         } 
         ans += dis[end]; 
         //cout<<ans<<endl;                    
        // puts("abc");
     }
     return ans;
}
    
int main()
{
    int N, M, i, j, k1,k2 ,n;
    while(scanf("%d%d",&N,&M)&&(N+M))
    {
        k1 = k2 = 1;
        memset(flow,0,sizeof(flow));
        for(int i=0;i<N;i++)
        {
          scanf("%s",str[i]);
          for(j=0;j<M;j++)
           {
               if(str[i][j]=='H')
               {
                  house[k1].x=i;
                  house[k1++].y=j;                  
               }
               else if(str[i][j]=='m')
               {
                  man[k2].x=i;
                  man[k2++].y=j;                  
               }       
           }  
        }
        n=k1-1;
       // cout<<n<<endl;
        for(i=0;i<2*n+1;i++)
         for(j=0;j<2*n+1;j++)
           cost[i][j]=Max;
        for(int i=1;i<=n;i++)
        {
         for(j=n+1;j<2*n+1;j++)
         {
             cost[i][j] = abs(house[j - n].x - man[i].x) + abs(house[j - n].y - man[i].y);//每个人到每个房子的费用 
             cost[j][i] = -cost[i][j];//这点很重要,千万别忘了 
             flow[i][j] = 1; //流量为 1                    
         } 
       //  设置起点为0,终点为 2*n+1 
          cost[i][0]=cost[0][i]=0;
          flow[0][i]=1;
          cost[i + n][2 * n + 1] = cost[2 * n + 1][i + n] = 0;  
          flow[i + n][2*n + 1] = 1;                              
        } 
         printf("%d\n", maxflow_mincost(0, 2*n + 1)); 
    }
   system("pause"); 
   return 0;    
}

转载于:https://www.cnblogs.com/one--world--one--dream/archive/2011/10/11/2207792.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值