Sicily 1013 Going Home
2016-03-11 19:41
471 查看
Constraints
Time Limit: 10 secs, Memory Limit: 32 MBDescription
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 stephe 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 file e.in. Each case starts with a line giving two integersN 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
Solution
给出一张图,点代表通路,H代表house,m代表man,每个人要走到一个屋子,每个人走一步的cost是1,求一人一窝的最小cost是多少。一开始我看题目没有看仔细,以为一个点只能一个人停留,然后就思考怎么避免相撞,那么肯定是需要思考策略和标记走过的路的。后来仔细看题之后,发现对路线没有太多要求,随你怎么走都是可以的,那么就是一个二分图匹配问题了,求完美匹配的最小权重。当然,初始化的权重需要计算,这里直接算距离即可,直接套用最大权重的KM算法模板即可,注意权重和最后的答案均取负数。用的是大白书的O(n^4)的模板,比较好懂。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 105;
int home[maxn], men[maxn];
int W[maxn][maxn], n;
int Lx[maxn], Ly[maxn];
int left[maxn];
bool S[maxn], T[maxn];
int abs(int i) { return i > 0 ? i : -i; }
bool match(int i)
{
S[i] = true;
for (int j = 1; j <= n; ++j) if (Lx[i] + Ly[j] == W[i][j] && !T[j])
{
T[j] = true;
if (!left[j] || match(left[j]))
{
left[j] = i;
return true;
}
}
return false;
}
void update()
{
int a = 1<<30;
for (int i = 1; i <= n; ++i) if (S[i])
for (int j = 1; j <= n; ++j) if (!T[j])
a = min(a, Lx[i] + Ly[j] - W[i][j]);
for (int i = 1; i <= n; ++i)
{
if (S[i]) Lx[i] -= a;
if (T[i]) Ly[i] += a;
}
}
void KM()
{
for (int i = 1; i <= n; ++i)
{
left[i] = Lx[i] = Ly[i] = 0;
for (int j = 1; j <= n; ++j)
Lx[i] = max(Lx[i], W[i][j]);
}
for (int i = 1; i <= n; ++i)
{
for (;;)
{
for (int j = 1; j <= n; ++j) S[j] = T[j] = 0;
if (match(i)) break;
else update();
}
}
}
int main()
{
int N, M;
while (scanf("%d%d", &N, &M) != EOF && !(N == 0 && M == 0) )
{
char c, s[105];
int counth, countm;
counth = countm = 0;
memset(W, 0, sizeof(W));
for (int i = 0; i < N; ++i)
{
scanf("%s", s);
for (int j = 0; j < M; ++j)
{
c = s[j];
if (c == 'H') home[counth++] = i*M + j;
else if (c == 'm') men[countm++] = i*M + j;
}
}
n = max(counth, countm);
for (int i = 0; i < countm; ++i)
{
for (int j = 0; j < counth; ++j)
{
int tx = abs(home[j]/M - men[i]/M);
int ty = abs(home[j]%M - men[i]%M);
W[i+1][j+1] = -(tx + ty);
//printf("(%d-%d) (%d-%d)->%d\n", men[i]/M, men[i]%M, home[j]/M, home[j]%M, W[i][j]);
}
}
KM();
int res = 0;
for (int i = 1; i <= n; ++i) res += (Lx[i] + Ly[i]);
printf("%d\n", -res);
}
return 0;
}
相关文章推荐
- sicily 1020. Big Integer
- 初学图论-Kahn拓扑排序算法(Kahn's Topological Sort Algorithm)
- 初学图论-Bellman-Ford单源最短路径算法
- 初学图论-DAG单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法
- 初学图论-Dijkstra单源最短路径算法基于优先级队列(Priority Queue)的实现
- 封装好的Folyd建图,C++源码
- LCA模板
- 图论学习笔记之一——Floyd算法
- soj1041. Pushing Boxes
- Sicily 1046, Plane Spotting
- SICILY 1934 移动小球
- 【LCA】SPOJ QTREE2
- poj 3249 Test for Job 最长路
- HDU 2544
- Timus 1557 Network Attack DFS+各种各种...
- HDU1289 Tarjan-模板题
- Poj2638 网络流+最短路+二分答案
- Aizu1311 分层图最短路 (...大概)
- HDU 3631 Shortest Path