[POJ 2195] Going Home【费用流】
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.

Input
Output
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
Solution:
以后遇到需要构图的题,数组范围都要开大一些,特别是运用 SPFA 时的队列大小。
这题的构图较简单,直接从每个人到每间房子连一条费用为两者曼哈顿距离的边,容量大于 0 即可。
这样就得到一个带权二分图,可以用 KM 算法求最佳匹配。
但我用的是最小费用最大流。由于网络图是多源多汇的,我们可以建立一个超级源点,向每个人连一条容量为 1,费用为 0 的边,再建立一个超级汇点,从每间房子连出一条容量为 1,费用为 0 的边,跑一遍最小费用流即可。
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; }
发表评论