HDU 1533 Going Home (KM)
来源:互联网 发布:以撒的结合mac版下载 编辑:程序博客网 时间:2024/05/16 09:56
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5632 Accepted Submission(s): 2962
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
KM算法详解:http://www.cnblogs.com/wenruo/p/5264235.html
这道题因为是求最小的花费,而KM是求好感度和的最大值,所以这里就要建立负权边,其他就是套用KM算法模板
#include<cstdio>#include<algorithm>#include<iostream>#include<queue>#include<cstring>#define MAXN 150#define INF 999999999#define Min(a,b) a>b?b:ausing namespace std;struct Mymap{ int x; int y; int value;};int map[MAXN][MAXN]; //map[i][j]第i个人和第j个房子的距离Mymap man[MAXN],house[MAXN];int n,m,mannum,housenum;int visgirl[MAXN],visboy[MAXN];int match[MAXN],slack[MAXN]; //match为第i座房子匹配的人int dfs(int girl) //girl为人{ visgirl[girl]=1; for(int i=1;i<=housenum;i++) //i为房子的编号 { if(visboy[i])continue; int gap=man[girl].value+house[i].value-map[girl][i]; if(gap==0) { visboy[i]=1; if(match[i]==-1||dfs(match[i])) { match[i]=girl; return 1; } } else slack[i]=Min(gap,slack[i]); //任意一个参与匹配女生能换到任意一个这轮没有被选择过的男生所需要降低的最小值 //此时i是未被选择过的,而slack[i]表示就所有女生降低期望值的话,需要最少的期望值,使i男得到匹配至少一个女生 //女1选择男1,期望值要降低1。 女2选择男1,期望值要降低1。 女2选择男2,期望值要降低2。于是,只要期望值降低1,就有妹子可能选择其他人。所以妹子们的期望值要降低1点。 } return 0;}int KM(){ int j; memset(match,-1,sizeof(match)); for(int i=1;i<=mannum;i++) //i为人(女生)的标号 { for(j=1;j<=housenum;j++) //因为取最小值,所以初始化无穷大 { slack[j]=INF; } while(1) { memset(visboy,0,sizeof(visboy)); memset(visgirl,0,sizeof(visgirl)); if(dfs(i))break; int d=INF; for(j=1;j<=housenum;j++) //找出最小的改变量 { if(visboy[j]==0)d=Min(d,slack[j]); //只有visboy[i]==0时slack[i]有值 } for(j=1;j<=mannum;j++)//刚才被抢的男生此时非常得意,因为有妹子来抢他,与是他的期望值提高了1点(就是同妹子们降低的期望值相同)。 { //与是期望值变成这样(当然,不参与刚才匹配过程的人期望值不变) if(visgirl[j])man[j].value-=d; //对参加 if(visboy[j])house[j].value+=d; else slack[j]-=d; //因为女生已经降了期望值,所以i男匹配所需最小期望值也降1,没有访问过的boy 因为girl们的期望值降低,距离得到女生倾心又进了一步! //因为这个slack[i]是根据参加这轮匹配的女生中得到的,即得到该轮匹配中的一个女神的芳心的最小期望值 } } } int res=0; for(int i=1;i<=housenum;i++) { res+=map[match[i]][i]; } return res;}int main(){ int i,j,ans,from,to; char c; while(scanf("%d%d",&n,&m),m+n) { getchar(); memset(map,0,sizeof(map)); mannum=housenum=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cin>>c; if(c=='H') { housenum++; house[housenum].x=i; house[housenum].y=j; house[housenum].value=0; } if(c=='m') { mannum++; man[mannum].x=i; man[mannum].y=j; } } getchar(); } int max=-INF; for(i=1;i<=mannum;i++) { max=-INF; for(int z=1;z<=housenum;z++) { map[i][z]=-abs(man[i].x-house[z].x)-abs(man[i].y-house[z].y); if(max<map[i][z]) max=map[i][z]; } man[i].value=max; } ans=0; ans=KM(); printf("%d\n",0-ans); } return 0;}
- HDU 1533 Going Home (KM)
- hdu 1533 Going Home (KM裸题)
- HDU--1533--Going Home--KM算法
- hdu 1533 Going Home (KM算法)
- HDU 1533 Going Home【km应用】
- hdu 1533 Going Home(KM)
- hdu 1533 Going Home(二分KM算法)
- hdu 1533 Going Home KM算法
- hdu 1533 Going Home【KM匹配】
- hdu 1533 going home KM算法
- HDU 1533 Going Home【KM算法】
- hdu 1533 Going Home【KM算法求最大权匹配】
- hdu 1533 Going Home(最小权匹配KM)
- HDU 1533 Going Home(KM完美匹配)
- hdu 1533 Going Home (最大权匹配,KM算法)
- HDU 1533 Going Home【最小费用流|KM算法】
- HDU 1533 && poj 2195 Going Home KM算法
- HDU 1533 Going Home(KM算法模板样例)
- HDU 6059-Kanade's trio(多校训练第三场->01字典树)
- 打印到TIFF, 图片打印机
- Movie Studio的光效添加
- Redis---- Windows 开机自启的三种方法
- Android 判断App是否在后台运行
- HDU 1533 Going Home (KM)
- ios 各种动画机制
- 自学机器学习技术,网上搜到的一些建议
- Decoding Baby Boos UVALive
- [技术问题]MySQL数据库的“十宗罪”的治理
- cordova打包安卓自己生成keystore文件,用于打包release版本的APK
- linux ulimit
- 2.2.9—单链表—Reverse Nodes in k-Group
- python+Eclipse+pydev环境搭建