hdu 1533 Going Home【KM算法求最大权匹配】

34 篇文章 0 订阅
12 篇文章 0 订阅
本文探讨了一个迷宫问题,其中N个小人在M个房子中移动,每个移动都需支付费用。目标是计算让所有小人进入不同房子的最小费用。通过将每个小人和房子的坐标进行匹配并计算距离,使用匈牙利算法求解最优解。实例演示了如何应用该算法解决实际问题。
摘要由CSDN通过智能技术生成

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533

我的链接:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=17728#problem/B

Going Home

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


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
 

Source
 
Recommend
lwg
 
思路将m的坐标记录到左集,h的坐标记录到右集,w[i][j]表示第i个m到第j个h的距离
          w[i][j]=△x+△y 然后因为是求最小值,而KM是求最大值
          所以只要这样:w[i][j] = -w[i][j]建图再套模板输出【-sum】就ok!

PS:思路看的网上牛人的,初学KM,代码效率较低。

//Accepted	348 KB	62 ms	C++	2069 B
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110;
char map[maxn][maxn];
int w[maxn][maxn];
bool s[maxn],t[maxn];
int match[maxn];
int lx[maxn],ly[maxn];
int n;
bool hungary(int u){
     s[u]=true;
     for(int v=1;v<=n;v++){
         if(!t[v] && lx[u]+ly[v]==w[u][v]){
            t[v]=true;//易遗忘 
            if(match[v]==-1 || hungary(match[v])){
               match[v]=u;
               return true;
            }
         }
     }
     return false;
}
int KM(){
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n;i++){
    	lx[i]=-1<<30;//注意 
    }
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
       for(int j=1;j<=n;j++)
          lx[i]=max(lx[i],w[i][j]);
    for(int i=1;i<=n;i++){
        while(1){
           memset(s,false,sizeof(s));
           memset(t,false,sizeof(t));
           if(hungary(i)) break;
           else{
                int a=1<<30;
                for(int j=1;j<=n;j++) if(s[j]){
                    for(int k=1;k<=n;k++) if(!t[k] && a>lx[j]+ly[k]-w[j][k])
                        a=lx[j]+ly[k]-w[j][k];
                }
                for(int j=1;j<=n;j++){
                   if(s[j]) lx[j]-=a;
                   if(t[j]) ly[j]+=a;
                }
           }
        }
    }
    for(int i=1;i<=n;i++) ans+=w[match[i]][i];
    return -ans;//易遗忘 
}
int main(){
	int row,col,numi,numj;
    while(scanf("%d%d%*c",&row,&col)!=EOF){
    	 if(row==0 || col==0) break;
    	 n=numi=numj=0;
         for(int i=1;i<=row;i++){
         	for(int j=1;j<=col;j++){
	         	scanf("%c",&map[i][j]);
	         	if(map[i][j]=='m') n++;
	         }
	         getchar();
         }
         for(int i=1;i<=row;i++){
         	for(int j=1;j<=col;j++){
	         	if(map[i][j]=='m'){
	         		numi++;numj=1;
	         		for(int k=1;k<=row;k++){
		         		for(int e=1;e<=col;e++){
		         			if(map[k][e]=='H')
		         			w[numi][numj++]=-(abs(i-k)+abs(j-e));
		         		}
		         	}
	         	}
	         }
         }
         printf("%d\n",KM());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值