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);
	}
}



全部评论

相关推荐

首先讲三个故事,关于牛客的事件一:2024年,牛客上有一对高学历情侣,求职方向与我当时一致,都是嵌入式方向。他们恰好是我的朋友,专业能力和学历背景都很扎实,也因此拿到了不少优质offer。和很多求职者一样,他们把offer情况整理后发在平台上,本意是记录与交流,但很快引发了争议。有声音指责他们“集邮”“不释放名额”,认为这种展示本身就是一种炫耀。最终讨论失控,当事人删除内容,事件也很快被遗忘。事件二:小红书评论区,一条评价获得了不少共鸣:“感觉牛客就是当年那群做题区毕业了开始找工作还收不住那股味,颇有一种从年级第一掉到年纪第二后抱怨考不上大学的味道”,这条评论被水印里这个同学转发到牛客后,评论...
小型域名服务器:当看到别人比自己强的时候,即便这是对方应得的,很多人会也下意识的歪曲解构对方的意图,来消解自己在这本就不存在的比较中输掉的自信,从而平白制造出很多无谓的争论。比如你会在空余时间来写优质好文,而我回家只会暗区突围,那么我就可以作为键盘侠在这里评论你是不是XXXXXXXX。即便我自己都知道这是假的,但只要这没那么容易证伪,那么当你开始回应的时候,脏水就已经泼出去了,后面可能会有更多的人带着情绪来给我点赞,而毫不关注你写的文章内容本身是啥了。
SAGIMA牛马咖啡
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务