现在的位置: 首页 > 综合 > 正文

二分图

2013年05月29日 ⁄ 综合 ⁄ 共 3156字 ⁄ 字号 评论关闭
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15216   Accepted: 7786

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

ac代码:
其实最大和最小权匹配都是一样的问题。只要会求最大匹配,如果要求最小权匹配,则将权值取相反数,再把结果取相反数,那么最小权匹配就求出来了。
KM算法。。。
最小权
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include <cctype>
#define rep(i,s,e) for(int i = (s);i<=(e);++i)
#define maxn 150
#define INF 10000000
using namespace std;

int n,m;
int num1,num2;
int visl[maxn],visr[maxn];
int pl[maxn],pr[maxn];
int w[maxn][maxn];
int linker[maxn];
int slack[maxn];

bool match(int u)
{
    visl[u] = 1;//从左边开始
    for(int i = 0;i<num2;++i)//考虑右边部分
    {
        if(visr[i]==0)
        {
            int val = pl[u] + pr[i] - w[u][i];//==0是相等子树的条件
            if(val==0)
            {
                visr[i] = 1;
                if(linker[i]==-1||match(linker[i]))
                {
                    linker[i] = u;
                    return true;
                }
            }else
            {
                if(val < slack[i])
                slack[i] = val;//右边节点的松弛量(最小值)
            }
        }
    }
    return false;
}

int KM()
{
    //初始化顶标
    //初始化原则:右边节点顶标值为0
    //左边节点顶标值为:他出发的所有边的最大权值
    for(int i = 0;i<num1;++i)
    {
        pr[i] = 0;
        pl[i] = -INF;
     for(int j = 0;j<num2;++j)
     {
         if(w[i][j] > pl[i])
         pl[i] = w[i][j];
     }
    }

    memset(linker,-1,sizeof(linker));

    for(int k = 0;k<num1;++k)
    {
        for(int i = 0;i<150;++i)
        {
            slack[i] = INF;//初始化slack
        }

        while(1)
        {
            memset(visl,0,sizeof(visl));
            memset(visr,0,sizeof(visr));

            if(match(k))
            break;

            int d = INF;
            for(int i = 0;i<num2;++i)
            {
                if(!visr[i])
                d = min(d,slack[i]);//左侧减去d右边加上d
            }

            for(int i = 0;i<num1;++i)
            {
                if(visl[i])
                pl[i] -= d;
                if(visr[i])
                pr[i] += d;
            }
        }
    }

    int  sum =  0;
    for(int i=  0;i<num1;++i)//到此处已形成完美匹配
    {
        sum += -1*w[linker[i]][i];
    }
    return sum;
}

int house[maxn][4];
int man[maxn][4];
char map[maxn][maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)
        break;
        num1 = num2 = 0;
        for(int i = 0;i<n;++i)
        {
            cin>>map[i];
            for(int j = 0;j<m;++j)
            {
                if(map[i][j]=='H')
                {
                    house[num1][0] = i;
                    house[num1++][1] = j;
                }
                if(map[i][j]=='m')
                {
                    man[num2][0] = i;
                    man[num2++][1] = j;
                }
            }
        }

        for(int i = 0;i<num1;++i)
         for(int j = 0;j<num2;++j)
         {
             w[i][j] = abs(house[i][0] - man[j][0]) + abs(house[i][1] - man[j][1]);
             w[i][j] = -1*w[i][j];
         }

         printf("%d\n",KM());
    }
    return 0;
}

抱歉!评论已关闭.