HDU1533:Going Home(最小费用流)

来源:互联网 发布:东方财富国际证券知乎 编辑:程序博客网 时间:2024/05/21 15:04

Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5474    Accepted Submission(s): 2871


Problem 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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
 

Sample Output
21028
 

Source

Pacific Northwest 2004

题意:人回房子,每走一步花费1,问令所有人回到房子的最小花费。
思路:最小费用流,源点->人->房子->汇点建图,所谓最小费用最大流,就是类似最大流的EK算法,不过增广路不是胡乱找,而是在最短路上找,于是得结合spfa或者dijk搞,同时这题有比费用流更高效的做法。

# include <iostream># include <cstdio># include <cstring># include <vector># include <algorithm>using namespace std;const int maxn = 2e4+100;const int INF = 0x3f3f3f3f;char s[103][103];int cnt=0, Next[maxn], dis[maxn], vis[maxn], pre[maxn], q[maxn];vector<pair<int, int> >v1,v2;struct node{    int u, v, w, c, next;    node(){}    node(int u, int v, int w, int c, int next):u(u),v(v),w(w),c(c),next(next){}}edge[maxn<<1];void add_edge(int u, int v, int w, int c){    edge[cnt] = node(u,v,w,c,Next[u]);    Next[u] = cnt++;    edge[cnt] = node(v,u,0,-c,Next[v]);    Next[v] = cnt++;}bool spfa(){    memset(vis, 0, sizeof(vis));    memset(dis, INF, sizeof(dis));    int l=0, r=0;    dis[0] = 0;    vis[0] = 1;    q[r++] = 0;    while(l<r)    {        int u=q[l];        ++l;        vis[u] = 0;        for(int i=Next[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v, w=edge[i].w, c=edge[i].c;            if(w>0 && dis[v] > dis[u]+c)            {                dis[v] = dis[u] + c;                pre[v] = i;                if(!vis[v])                {                    q[r++] = v;                    vis[v] = 1;                }            }        }    }    return dis[20088] != INF;}int mcmf(){    int cost = 0;    while(spfa())    {        int imin = INF;        for(int i=20088; i!=0; i=edge[pre[i]].u) imin = min(imin, edge[pre[i]].w);        for(int i=20088; i!=0; i=edge[pre[i]].u)        {            edge[pre[i]].w -= imin;            edge[pre[i]^1].w += imin;        }        cost += imin*dis[20088];    }    return cost;}int main(){    int n, m;    while(~scanf("%d%d",&n,&m),n+m)    {        cnt = 0;        v1.clear();        v2.clear();        memset(Next, -1, sizeof(Next));        for(int i=1; i<=n; ++i) scanf("%s",s[i]+1);        for(int i=1; i<=n; ++i)        {            for(int j=1; j<=m; ++j)            {                if(s[i][j] == 'm') v1.push_back(make_pair(i,j));                else if(s[i][j] == 'H') v2.push_back(make_pair(i,j));            }        }        for(int i=0; i<v1.size(); ++i)        {            int x1 = v1[i].first, y1 = v1[i].second;            add_edge(0, i+1, 1, 0);            for(int j=0; j<v2.size(); ++j)            {                int x2 = v2[j].first, y2 = v2[j].second;                add_edge(i+1, j+1+v1.size(), 1, abs(x2-x1)+abs(y2-y1));            }        }        for(int i=0; i<v2.size(); ++i)        {            int x1 = v2[i].first, y1 = v2[i].second;            add_edge(i+1+v1.size(), 20088, 1, 0);        }        printf("%d\n",mcmf());    }    return 0;}