POJ 2195&&HDU 1533 Going Home(KM算法解决二分图最小权匹配)

Going Home
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5777    Accepted Submission(s): 3060


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

题目的意思说,人要回家,现在人每走一步就要花费一块钱,问所有人回到家的最小花费是多少。
输入数据:先给你一个地图的行和列的个数,然后给你一个地图,“.”代表可以走
“m”代表人,“H”代表房子。人和房子的数目相同,都是N(N不会给出)


每组测试样例输出一个最小花费,让所有人都回到家。
这一道题目分析一下就是二分图最小权值匹配,我们可以用KM算法,稍加变形求得最小权值匹配。

求二分图最大边权匹配我们有KM算法,现在使求二分图最小边权匹配。

其实只需要在KM算法上改一点就可以。把每条边的边权改为负值。

这样求出来的答案就是负值,且是所有可能中负值最大一个。

在去负值,那就是最小了,此时答案就是所有可能中最小的一个。也就是最小边权匹配。

有一道类似的题目,大家可以看看点我传送 (๑ •̀ㅂ•́) ✧


#include<iostream>
#include<cstdio>
#include<cstring>
#include<functional>
#include<algorithm>
#define inf 0x3f3f3f3f
#define maxn 105
using namespace std;
char maps[maxn][maxn];
int ex[maxn],ey[maxn];
int visx[maxn],visy[maxn];
int match[maxn],slack[maxn];
int link[maxn][maxn],n;
bool dfs(int x)
{
	visx[x]=1;
	for(int y=0;y<n;y++)
	{
		if(visy[y])
			continue;
		int gap=ex[x]+ey[y]-link[x][y];
		if(gap==0)
		{
			visy[y]=1;
			if(match[y]==-1||dfs(match[y]))
			{
				match[y]=x;
				return 1;
			}
		}
		else
			slack[y]=min(slack[y],gap);
	}
	return 0;
}

int KM()
{
	memset(match,-1,sizeof match);
	memset(ex,0,sizeof ex);
	memset(ey,0,sizeof ex);
	int i,j;
	for(i=0;i<n;i++)
	{
		ex[i]=link[i][0];
		for(j=1;j<n;j++)
			ex[i]=max(ex[i],link[i][j]);
	}
	for(i=0;i<n;i++)
	{
		for(int j = 0; j < n; ++j)
				slack[j] = inf;
		while(1)
		{
			memset(visx,0,sizeof(visx));
			memset(visy,0,sizeof(visy));
			if(dfs(i))
				break;
			int d=inf;
			for(j=0;j<n;j++)
				if(!visy[j])
					d=min(d,slack[j]);
			for(j=0;j<n;j++)
			{
				if(visx[j])
					ex[j]-=d;
				if(visy[j])
					ey[j]+=d;
				else
					slack[j]-=d;
			}
		}
	}
	int sum=0;
	for(i=0;i<n;i++)
		sum+=link[match[i] ][i];
	return sum;
}
int main()
{
	int i,j,a,b;
	int cntx,cnty;
	int ans,row,col;
	while(scanf("%d%d",&row,&col),row&&col)
	{
		memset(maps,0,sizeof maps);
		n=0;
		for( i=0;i<row;i++)
		{
			getchar();
			for( j=0;j<col;j++)
			{
				scanf("%c",&maps[i][j]);
				if(maps[i][j]=='m')
					n++;
			}
		}
//		for(i=0;i<row;i++)
//		{
//			for(j=0;j<col;j++)
//				printf("%c",maps[i][j]);
//			cout<<endl;
//		}
		cntx=-1;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				link[i][j]=-inf;
		for(i=0;i<row;i++)
		{
			for(j=0;j<col;j++)
				if(maps[i][j]=='m')
				{
					cntx++;
					cnty=0;
					for(int a=0;a<row;a++)
					{
						for(int b=0;b<col;b++)
						{
							if(maps[a][b]=='H')
								link[cntx][cnty++]=-(abs(i-a)+abs(j-b));
					
						}
					}
				}
		}
//		for(i=0;i<n;i++)
//		{
//			for(j=0;j<n;j++)
//				cout<<link[i][j]<<" ";
//			cout<<endl;
//		}
		ans=KM();
		printf("%d\n",-ans);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值