[POJ 2195] Going Home【费用流】

• 2018-01-21
• 0
• 0

Problem:

 Time Limit: 1000MS Memory Limit: 65536K

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
```

Source

Pacific Northwest 2004

Code: O(TVαE2) [1148K, 94MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, M;
char line[111];

struct Coordinate{
int x, y;
} man[111], house[111];

int mancnt, housecnt;

struct Edge{
int from, np, cap, cost;
Edge *nxt, *rev;
};

struct Graph{
Edge *V[222], E[44444];
int tope;

inline void clear() {tope = 0, memset(V, 0, sizeof(V));}

inline void addedge(int u, int v, int cap, int cost = 0){
E[++tope].from = u, E[tope].np = v, E[tope].cap = cap, E[tope].cost = cost;
E[tope].nxt = V[u], E[tope].rev = &E[tope + 1], V[u] = &E[tope];
E[++tope].from = v, E[tope].np = u, E[tope].cap = 0, E[tope].cost = -cost;
E[tope].nxt = V[v], E[tope].rev = &E[tope - 1], V[v] = &E[tope];
}
} G;

Edge *prev[222];
int dis[222];
int q[2222], fr, re;  // The queue must be large enough
bool inq[222];

inline int MCMF(){
int mincost = 0;
do{
fr = re = 0, q[re++] = 0;
memset(prev, 0, sizeof(prev));
memset(dis, 0x3f, sizeof(dis)), dis[0] = 0;
memset(inq, 0, sizeof(inq)), inq[0] = 1;
while(fr != re){
int u = q[fr++]; inq[u] = 0;
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
if(ne->np && ne->cap && dis[u] + ne->cost < dis[ne->np]){
dis[ne->np] = dis[u] + ne->cost;
prev[ne->np] = ne;
if(!inq[ne->np]) q[re++] = ne->np, inq[ne->np] = 1;
}
}
if(prev[220]){
int dcost = 0, dflow = 0x3f3f3f3f;
for(register Edge *ne = prev[220]; ne; ne = prev[ne->from]) dflow = min(dflow, ne->cap);
for(register Edge *ne = prev[220]; ne; ne = prev[ne->from])
ne->cap -= dflow, ne->rev->cap += dflow, dcost += dflow * ne->cost;
mincost += dcost;
}
} while(prev[220]);
return mincost;
}

int main(){
while(scanf("%d%d", &N, &M) != EOF && N && M){
mancnt = housecnt = 0;
for(register int i = 1; i <= N; i++){
scanf("%s", line + 1);
for(register int j = 1; j <= M; j++)
if(line[j] == 'H') house[++housecnt].x = i, house[housecnt].y = j;
else if(line[j] == 'm') man[++mancnt].x = i, man[mancnt].y = j;
}
G.clear();
for(register int i = 1; i <= mancnt; i++) G.addedge(0, i, 1, 0);
for(register int i = 1; i <= housecnt; i++) G.addedge(110 + i, 220, 1, 0);
for(register int i = 1; i <= mancnt; i++)
for(register int j = 1; j <= housecnt; j++){
int dis = abs(man[i].x - house[j].x) + abs(man[i].y - house[j].y);
G.addedge(i, 110 + j, 1, dis);
}
int mincost = MCMF();
printf("%d\n", mincost);
}
return 0;
}
```

评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai