Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2963 Accepted Submission(s): 1492
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.2cto.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.
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
2 10 28
解题思路:
题意为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;y tmp) slack[y]=tmp; } return false; } int KM() { memset(linked,-1,sizeof(linked)); memset(ly,0,sizeof(ly)); for(int i=0;i lx[i]) lx[i]=g[i][j]; } for(int x=0;x slack[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