您的位置:首页 > 编程语言 > Go语言

poj-2195 Going-Home(最小费用最大流)

2016-09-30 19:30 330 查看
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


模板大法好,直接1A

#include <stdio.h>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
int source = 0, sink = 1, n, m;
int cap[maxn][maxn], pre[maxn], cost[maxn][maxn];
char map[maxn][maxn];
int house_cnt = 0, man_cnt = 0;
struct Node
{
int x, y;
Node(){}
Node(int a, int b) :x(a), y(b){}
}house[maxn],man[maxn];

void init()
{
house_cnt = man_cnt = 0;
memset(cap, 0, sizeof(cap));
memset(cost, 0, sizeof(cost));
}

int cal(int i, int j)
{
return abs(man[i].x - house[j].x) + abs(man[i].y - house[j].y);
}

bool spfa()
{
queue<int> q;
q.push(source);
bool vis[maxn] = { 0 };
vis[source] = true;
int d[maxn];
memset(d, INF, sizeof(d));
d[source] = 0;
memset(pre, 0, sizeof(pre));
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = true;
for (int i = 0; i < maxn; i++)
{
if (cap[u][i] && d[i]>d[u] + cost[u][i])
{
d[i] = d[u] + cost[u][i];
pre[i] = u;
if (!vis[i])
{
vis[i] = true;
q.push(i);
}
}
}
vis[u] = false;
}
if (d[sink] < INF) return true;
return false;
}

int go()
{
int cf = INF, ans = 0;
for (int i = sink; i != 0; i = pre[i])
cf = min(cf, cap[pre[i]][i]);
for (int i = sink; i != 0; i = pre[i])
{
cap[pre[i]][i] -= cf;
cap[i][pre[i]] += cf;
ans += cost[pre[i]][i] * cf;
}
return ans;
}

int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0) break;
init();
for (int i = 0; i < n; i++)
scanf("%s", map[i]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] == 'H')
{
house[house_cnt++] = Node(i, j);
}
if (map[i][j] == 'm')
{
man[man_cnt++] = Node(i, j);
}
}
}
for (int i = 0; i < man_cnt; i++)
{
int u = i + 2;
cap[source][u] = 1;
cost[source][u] = 0;
for (int j = 0; j < house_cnt; j++)
{
int v = j + 2 + man_cnt;
cap[v][sink] = 1;
cost[v][sink] = 0;
cap[u][v] = 1;
cost[u][v] = cal(u - 2, v - 2 - man_cnt);
cost[v][u] = -cost[u][v];
}
}
int ans = 0;
while (spfa())
ans += go();
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: