HDU 1533

Going Home


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




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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 10000+10
#define inf 0x7fffffff
char s[maxn][maxn];
int n, m, k, p;
int tot;
int hx[maxn],hy[maxn],mx[maxn],my[maxn];
int head[maxn],flag[maxn],p1[maxn],p2[maxn],dis[maxn];
struct Edge
{
    int to,next,c,f;
}edge[43000];
void addedge(int u,int v,int c,int f)
{
    edge[tot].next = head[u];
    edge[tot].to = v;
    edge[tot].c = c;
    edge[tot].f = f;
    head[u] = tot ++;
}
bool spfa(int s, int t)
{
    queue<int> q;
    memset(flag, 0, sizeof(flag));
    memset(p1, -1, sizeof(p1));
    memset(p2, -1, sizeof(p2));
    for(int i = 0;i <= t;i ++)
    dis[i] = inf;
    q.push(s);
    dis[s] = 0;
    flag[s] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        flag[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
           if(edge[i].c)
           {
               int v = edge[i].to;
               if(dis[v] > dis[u] + edge[i].f)
               {
                   dis[v] = dis[u] + edge[i].f;
                   p1[v] = u;
                   p2[v] = i;
                   if(!flag[v])
                   {
                       q.push(v);
                       flag[v] = 1;
                   }

               }
           }

        }
    }
    if(dis[t] == inf) return false;
    else return true;
}
int min_cost_flow(int s,int t)
{
   int ans_cost = 0;
   int u, minn;
   while(spfa(s, t))
   {
       u = t;
       minn = inf;
       while(p1[u] != -1)
       {
           minn = min(minn, edge[p2[u]].c);
           u = p1[u];
       }
       u = t;
       while(p1[u] != -1)
       {
           edge[p2[u]].c -= minn;
           edge[p2[u] ^ 1].c += minn;
           u = p1[u];
       }
       ans_cost += dis[t] * minn;
   }
   return ans_cost;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF && (n+m))
    {
        memset(head, -1, sizeof(head));
        tot = 0;
        for(int i = 0;i < n;i ++)
            scanf("%s",s[i]);
        k = p = 1;
        for(int i = 0;i < n;i ++)
         for(int j = 0;j < m;j ++)
         {
             if(s[i][j] == 'H')
             {
                 hx[k] = i+1;
                 hy[k] = j+1;
                 k++;
             }
             else if(s[i][j] == 'm')
             {
                 mx[p] = i+1;
                 my[p] = j+1;
                 p++;
             }
         }
         k--;
        for(int i = 1;i <= k;i ++)
            for(int j = 1;j <= k;j ++)
        {
            int len = abs(hx[i] - mx[j]) + abs(hy[i] - my[j]);
            addedge(i, k+j, 1, len);
            addedge(k+j, i, 0, -len);
        }
        for(int i = 1;i <= k;i ++)
        {
            addedge(0, i, 1, 0);
            addedge(i, 0, 0, 0);
        }
        for(int i = k+1;i <= 2*k;i ++)
        {
            addedge(i, 2*k+1, 1, 0);
            addedge(2*k+1, i, 0, 0);
        }
        int ans = min_cost_flow(0, 2*k+1);
        printf("%d\n", ans);
    }
    return 0;
}


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章