首页 > 代码库 > poj 2195(KM算法模板)

poj 2195(KM算法模板)

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18094 Accepted: 9225

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

AC代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
    int x,y;
}house[105],man[105];
int dis(Node t1,Node t2){
    return abs(t1.x-t2.x)+abs(t1.y-t2.y);
}
int N;
int w[105][105];
int lx[105],ly[105],link[105];
int s[105],t[105];
int match(int i){
    s[i]=1;
    for(int j=1;j<=N;j++){
        if(lx[i]+ly[j]==w[i][j] && !t[j]){
            t[j]=1;
            if(!link[j] || match(link[j])){
                link[j]=i;
                return 1;
            }
        }
    }
    return 0;
}
void update(){
    int a=1<<20;
    for(int i=1;i<=N;i++){
        if(s[i]){
            for(int j=1;j<=N;j++){
                if(!t[j]){
                    a=min(a,lx[i]+ly[j]-w[i][j]);
                }
            }
        }
    }

    for(int i=1;i<=N;i++){
        if(s[i])
            lx[i]-=a;
        if(t[i])
            ly[i]+=a;
    }
}
int KM(){
    for(int i=1;i<=N;i++){
        link[i]=ly[i]=0;
        lx[i]=-999999;
        for(int j=1;j<=N;j++){
            lx[i]=max(lx[i],w[i][j]);
        }
    }

    for(int i=1;i<=N;i++){
        while(1){
            for(int j=1;j<=N;j++)
                s[j]=t[j]=0;
            if(match(i))
                break;
            else
                update();
        }
    }

    int ret=0;
    for(int i=1;i<=N;i++){          //取反
        if(link[i]){
            ret-=w[link[i]][i];
        }
    }
    return ret;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)==2){
        if(n==0 && m==0)
            break;
        int k1,k2;
        k1=k2=0;
        for(int i=1;i<=n;i++){
            char ch[105];
            scanf("%s",ch);
            for(int j=0;j<m;j++){
                if(ch[j]=='H'){
                    house[++k1].x=i;
                    house[k1].y=j+1;
                }
                else if(ch[j]=='m'){
                    man[++k2].x=i;
                    man[k2].y=j+1;
                }
            }
        }
        N=k1;
        memset(w,0,sizeof(w));
        for(int i=1;i<=N;i++)        //建图
            for(int j=1;j<=N;j++){
                w[i][j]-=dis(house[i],man[j]);    //要求最小权,所以保存负数,KM后取反就是最小权了
            }
        int ans=KM();
        printf("%d\n",ans);
    }
    return 0;
}


poj 2195(KM算法模板)