hdu 1533 Going Home
来源:互联网 发布:网络防诈骗小知识 编辑:程序博客网 时间:2024/06/05 13:31
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4173 Accepted Submission(s): 2146
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.
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
KM算法求权值最小,需要标记序号
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 405;
int match[N], vx[N], vy[N], lx[N], ly[N], wight[N][N];
char str[N][N];
int n, mm, low, l, r;
int dfs(int x);
const int inf = 0x3f3f3f;
struct node
{
int x, y;
}m[N],h[N];
int main()
{
while(scanf("%d %d", &n, &mm),mm!=0||n!=0)
{
memset(str,0,sizeof(str));
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
l=0, r=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<mm;j++)
{
if(str[i][j]=='m')
{
l++;
m[l].x=i+1,m[l].y=j+1;
}
else if(str[i][j]=='H')
{
r++;
h[r].x=i+1,h[r].y=j+1;
}
}
}
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(match,0,sizeof(match));
for(int i=1;i<=l;i++)
{
int Min=inf;
for(int j=1;j<=r;j++)
{
wight[i][j]=abs(m[i].x-h[j].x)+abs(m[i].y-h[j].y);
Min=min(Min,wight[i][j]);
}
lx[i]=Min;
}
for(int i=1;i<=l;i++)
{
while(1)
{
memset(vx,0,sizeof(vx));
memset(vy,0,sizeof(vy));
low=inf;
if(dfs(i))
{
break;
}
for(int j=1;j<=r;j++)
{
if(vx[j])
{
lx[j] += low;
}
if(vy[j])
{
ly[j] -= low;
}
}
}
}
int sum=0;
for(int i=1;i<=l;i++)
{
sum+=wight[match[i]][i];
}
printf("%d\n",sum);
}
return 0;
}
int dfs(int x)
{
vx[x]=1;
for(int i=1;i<=r;i++)
{
if(vy[i])
{
continue;
}
int t=wight[x][i]-(lx[x]+ly[i]);
if(t==0)
{
vy[i]=1;
if(match[i]==0||dfs(match[i]))
{
match[i]=x;
return 1;
}
}
else if(low>t)
{
low=t;
}
}
return 0;
}
0 0
- HDU-1533 Going Home
- HDU 1533 Going Home
- HDU 1533 Going Home
- HDU 1533 Going Home
- hdu 1533 Going Home
- hdu 1533 Going Home
- HDU 1533 Going Home
- hdu acm 1533 Going Home
- HDU 1533 Going Home (KM)
- hdu 1533 Going Home (KM裸题)
- hdu(1533)Going Home(poj2195)
- hdu 1533 Going Home(二分图)
- POJ 2195 & hdu 1533 Going Home
- HDU--1533--Going Home--KM算法
- hdu 1533 Going Home 最小费用流
- hdu 1533 Going Home (KM算法)
- HDU 1533 Going Home【km应用】
- hdu 1533 Going Home(KM)
- aapt问题
- 常用图像库整理
- POJ 2947 Widget Factory(高斯消元)
- 容器和String、Date、Calendar
- 【NBUT】1651 - Red packet(二分)
- hdu 1533 Going Home
- Eclispe从SVN检出并启动Tomcat工程
- 深入Windows窗体原理及控件重绘技巧
- InnoDB VS MyISAM
- Mac下的SVN客户端工具Cornerstone使用教程
- java 获得本地IP和远程服务器IP
- easyUI使用
- Swift weakSelf写法
- 当引入多个js库,解决jquery冲突