码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2195 Going Home

时间:2015-02-05 23:24:51      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

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

题意:m(人)回h(家)问所有的最小值。

最小权匹配:我们将正向的值转变为负的,然后就是二分图最大匹配后输出相反值即可

至于m到各个h的距离,我们直接暴力或者BFS都可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int mod = 1000000007;
const int INF=0x3f3f3f3f;
const int maxn=110;
int mp[maxn][maxn];
int link[maxn],lx[maxn],ly[maxn];
int slack[maxn],visx[maxn],visy[maxn];
struct node{
    int x,y;
    int step;
};
int n,m;
int nx,ny;
bool dfs(int x)
{
    visx[x]=1;
    for(int y=0;y<ny;y++)
    {
        if(visy[y])  continue;
        int temp=lx[x]+ly[y]-mp[x][y];
        if(temp==0)
        {
            visy[y]=true;
            if(link[y]==-1||dfs(link[y]))
            {
                link[y]=x;
                return true;
            }
        }
        else if(slack[y]>temp)
            slack[y]=temp;
    }
    return false;
}
int KM()
{
    CLEAR(link,-1);
    CLEAR(ly,0);
    for(int i=0;i<nx;i++)
    {
        lx[i]=-INF;
        for(int j=0;j<ny;j++)
            if(mp[i][j]>lx[i])
               lx[i]=mp[i][j];
    }
    for(int x=0;x<nx;x++)
    {
        for(int i=0;i<ny;i++)
            slack[i]=INF;
        while(1)
        {
            CLEAR(visx,0);
            CLEAR(visy,0);
            if(dfs(x))  break;
            int d=INF;
            for(int i=0;i<ny;i++)
                if(!visy[i]&&d>slack[i])
                   d=slack[i];
            for(int i=0;i<nx;i++)
                if(visx[i])
                   lx[i]-=d;
            for(int i=0;i<ny;i++)
            {
                if(visy[i])  ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    int res=0;
    for(int i=0;i<ny;i++)
        if(link[i]!=-1)
           res+=mp[link[i]][i];
    return res;
}
char str[maxn][maxn];
int s[maxn][maxn];
int vis[maxn][maxn];
int dr[][2]={-1,0,1,0,0,-1,0,1};
void BFS(int sx,int sy)
{
    queue<node>q;
    node st,ed;
    CLEAR(vis,0);
    st.x=sx,st.y=sy;
    st.step=0;
    vis[sx][sy]=1;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(str[st.x][st.y]=='H')
            mp[s[sx][sy]][s[st.x][st.y]]=-st.step;
        REP(i,4)
        {
            int xx=st.x+dr[i][0];
            int yy=st.y+dr[i][1];
            if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy])
            {
                ed.x=xx,ed.y=yy;
                ed.step=st.step+1;
                vis[xx][yy]=1;
                q.push(ed);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        REP(i,n)
           REP(j,m)  mp[i][j]=-INF;
        CLEAR(s,0);
        int H=0,M=0;
        REP(i,n)
        {
            scanf("%s",&str[i]);
            REP(j,m)
            {
                if(str[i][j]=='H')  s[i][j]=H++;
                if(str[i][j]=='m')  s[i][j]=M++;
            }
        }
        REP(i,n)
        {
            REP(j,m)
            {
                if(str[i][j]=='m')
                    BFS(i,j);
            }
        }
        nx=H,ny=M;
        printf("%d\n",-KM());
    }
    return 0;
}



POJ 2195 Going Home

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/43539999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!