程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [ACM] HDU 1533 Going Home (二分圖最小權匹配,KM算法)

[ACM] HDU 1533 Going Home (二分圖最小權匹配,KM算法)

編輯:C++入門知識

[ACM] HDU 1533 Going Home (二分圖最小權匹配,KM算法)


Going Home

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


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 Z喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vciBtb3JlIHRlc3QgY2FzZXMgaW4gdGhlIGlucHV0LiBFYWNoIGNhc2Ugc3RhcnRzIHdpdGggYSBsaW5lIGdpdmluZyB0d28gaW50ZWdlcnMgTiBhbmQgTSwgd2hlcmUgTiBpcyB0aGUgbnVtYmVyIG9mIHJvd3Mgb2YgdGhlIG1hcCwgYW5kIE0gaXMgdGhlIG51bWJlciBvZiBjb2x1bW5zLiBUaGUgcmVzdCBvZiB0aGUgaW5wdXQgd2lsbCBiZSBOIGxpbmVzIGRlc2NyaWJpbmcgdGhlCiBtYXAuIFlvdSBtYXkgYXNzdW1lIGJvdGggTiBhbmQgTSBhcmUgYmV0d2VlbiAyIGFuZCAxMDAsIGluY2x1c2l2ZS4gVGhlcmUgd2lsbCBiZSB0aGUgc2FtZSBudW1iZXIgb2Yg"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

解題思路:

題意為n*m的方格中有p個房子和p個人,人每走一步花費1美元,要求每個人都要找到一個房子,且每個房子裡只能有一個人,問p個人都找到各自的房子,最小的花費是多少。

可以用KM算法求出最大的花費,要求最小花費只要把鄰接矩陣中的權值取反,然後用Km算法最後返回 最大值的相反數就是要求的最小花費。

代碼:

#include 
#include 
#include 
#include 
using namespace std;
const int maxn=102;
const int inf=0x3f3f3f3f;
char mp[maxn][maxn];
int n,m;
int npeople;
int g[maxn][maxn];
int nx,ny;
int linked[maxn],lx[maxn],ly[maxn];
int slack[maxn];
bool visx[maxn],visy[maxn];

struct House//存放房子的坐標
{
    int x,y;
}house[maxn];

struct Man//存放人的坐標
{
    int x,y;
}man[maxn];

bool DFS(int x)//hungary
{
    visx[x]=true;
    for(int y=0;ytmp)
            slack[y]=tmp;
    }
    return false;
}

int KM()
{
    memset(linked,-1,sizeof(linked));
    memset(ly,0,sizeof(ly));
    for(int i=0;ilx[i])
            lx[i]=g[i][j];
    }
    for(int x=0;xslack[i])
                d=slack[i];
            for(int i=0;i>n>>m&&(n||m))
    {
        int h1=-1,m1=-1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='H')
                house[++h1].x=i,house[h1].y=j;
            else if(mp[i][j]=='m')
                man[++m1].x=i,man[m1].y=j;
        }
        npeople=(++h1);
        memset(g,inf,sizeof(g));
        for(int i=0;i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved