Going Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20617 | Accepted: 10449 |
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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
Source
Pacific Northwest 2004
题目链接:http://poj.org/problem?id=2195
题目大意:n个人n个房子,每间房只能有一个人,每个人走一步花费1元,求每个人都到房子里所花费的最小总费用
题目分析:基础的最小费用流,建图(除了超源和超汇其实是个完全二分图):超级源点到每个man容量为1,费用为0,每个man到每个house容量为1,费用为两点间的曼哈顿距离,每个house到超级汇点容量为1,费用为0,跑一下最小费用流即可
题目链接:http://poj.org/problem?id=2195
题目大意:n个人n个房子,每间房只能有一个人,每个人走一步花费1元,求每个人都到房子里所花费的最小总费用
题目分析:基础的最小费用流,建图(除了超源和超汇其实是个完全二分图):超级源点到每个man容量为1,费用为0,每个man到每个house容量为1,费用为两点间的曼哈顿距离,每个house到超级汇点容量为1,费用为0,跑一下最小费用流即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdlib>
#define x first
#define y second
#define mk make_pair
using namespace std;
int const INF = 0x3fffffff;
int const MAX = 205;
int n, m;
char a[105][105];
int head[MAX], cnt;
int pre[MAX], vis[MAX], dis[MAX];
int src, sk, ans;
struct EGDE
{
int to, nxt, cap, cost;
}e[MAX * MAX / 2];
pair < int, int > house[105];
pair < int, int > man[105];
int hnum, mnum;
void Init()
{
hnum = 1;
mnum = 1;
cnt = 0;
ans = 0;
memset(head, -1, sizeof(head));
}
void Add(int u, int v, int cap, int cost)
{
e[cnt].to = v;
e[cnt].cap = cap;
e[cnt].cost = cost;
e[cnt].nxt = head[u];
head[u] = cnt ++;
e[cnt].to = u;
e[cnt].cap = 0;
e[cnt].cost = -cost;
e[cnt].nxt = head[v];
head[v] = cnt ++;
}
int Get_dis(int i, int j)
{
return abs(house[i].x - man[j].x) + abs(house[i].y - man[j].y);
}
void Build_graph()
{
for(int i = 1; i <= n; i++)
{
scanf("%s", a[i] + 1);
for(int j = 1; j <= m; j++)
{
if(a[i][j] == 'H')
house[hnum ++] = mk(i, j);
if(a[i][j] == 'm')
man[mnum ++] = mk(i, j);
}
}
src = 0;
sk = hnum + mnum - 1;
for(int i = 1; i < mnum; i++)
Add(src, i, 1, 0);
for(int i = 1; i < mnum; i++)
for(int j = 1; j < hnum; j++)
Add(i, mnum + j - 1, 1, Get_dis(i, j));
for(int j = 1; j < hnum; j++)
Add(mnum + j - 1, sk, 1, 0);
}
bool SPFA()
{
memset(vis, false, sizeof(vis));
for(int i = 0; i < MAX; i++)
dis[i] = INF;
memset(pre, -1, sizeof(pre));
queue <int> q;
q.push(src);
vis[src] = true;
dis[src] = 0;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = e[i].nxt)
{
int v = e[i].to;
int cap = e[i].cap;
int cost = e[i].cost;
if(cap > 0 && dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
return dis[sk] != INF;
}
void Augment()
{
int mi = INF;
while(SPFA())
{
for(int i = sk; i != src; i = e[pre[i] ^ 1].to)
mi = min(mi, e[pre[i]].cap);
for(int i = sk; i != src; i = e[pre[i] ^ 1].to)
{
ans += mi * e[pre[i]].cost;
e[pre[i]].cap -= mi;
e[pre[i] ^ 1].cap += mi;
}
}
}
int main()
{
while(scanf("%d %d", &n, &m) && (n + m))
{
Init();
Build_graph();
Augment();
printf("%d\n", ans);
}
}