hdu1533 Going Home km算法解決最小權完美匹配
阿新 • • 發佈:2017-07-19
number send hdu 所有 end man rest until 相反數
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.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5539 Accepted Submission(s): 2907
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.
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
/** 題目:hdu1533 Going Home km算法解決最小權完美匹配 鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 題意:lv 思路:最優匹配(最大權完美匹配) km算法 如果是求最小權完美匹配,那麽將所有權值取相反數,然後求得最大權,輸出最大權的相反數即可。 */ #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <cmath> #include <queue> using namespace std; const int MAXN = 105; const int INF = 0x3f3f3f3f; int love[MAXN][MAXN]; // 記錄每個妹子和每個男生的好感度 int ex_girl[MAXN]; // 每個妹子的期望值 int ex_boy[MAXN]; // 每個男生的期望值 bool vis_girl[MAXN]; // 記錄每一輪匹配匹配過的女生 bool vis_boy[MAXN]; // 記錄每一輪匹配匹配過的男生 int match[MAXN]; // 記錄每個男生匹配到的妹子 如果沒有則為-1 int slack[MAXN]; // 記錄每個漢子如果能被妹子傾心最少還需要多少期望值 int N;//左側頂點數=右側頂點數=N; bool dfs(int girl) { vis_girl[girl] = true; for (int boy = 0; boy < N; ++boy) { if (vis_boy[boy]) continue; // 每一輪匹配 每個男生只嘗試一次 int gap = ex_girl[girl] + ex_boy[boy] - love[girl][boy]; if (gap == 0) { // 如果符合要求 vis_boy[boy] = true; if (match[boy] == -1 || dfs( match[boy] )) { // 找到一個沒有匹配的男生 或者該男生的妹子可以找到其他人 match[boy] = girl; return true; } } else { slack[boy] = min(slack[boy], gap); // slack 可以理解為該男生要得到女生的傾心 還需多少期望值 取最小值 備胎的樣子【捂臉 } } return false; } int KM() { memset(match, -1, sizeof match); // 初始每個男生都沒有匹配的女生 memset(ex_boy, 0, sizeof ex_boy); // 初始每個男生的期望值為0 // 每個女生的初始期望值是與她相連的男生最大的好感度 for (int i = 0; i < N; ++i) { ex_girl[i] = love[i][0]; for (int j = 1; j < N; ++j) { ex_girl[i] = max(ex_girl[i], love[i][j]); } } // 嘗試為每一個女生解決歸宿問題 for (int i = 0; i < N; ++i) { fill(slack, slack + N, INF); // 因為要取最小值 初始化為無窮大 while (1) { // 為每個女生解決歸宿問題的方法是 :如果找不到就降低期望值,直到找到為止 // 記錄每輪匹配中男生女生是否被嘗試匹配過 memset(vis_girl, false, sizeof vis_girl); memset(vis_boy, false, sizeof vis_boy); if (dfs(i)) break; // 找到歸宿 退出 // 如果不能找到 就降低期望值 // 最小可降低的期望值 int d = INF; for (int j = 0; j < N; ++j) if (!vis_boy[j]) d = min(d, slack[j]); for (int j = 0; j < N; ++j) { // 所有訪問過的女生降低期望值 if (vis_girl[j]) ex_girl[j] -= d; // 所有訪問過的男生增加期望值 if (vis_boy[j]) ex_boy[j] += d; // 沒有訪問過的boy 因為girl們的期望值降低,距離得到女生傾心又進了一步! else slack[j] -= d; } } } // 匹配完成 求出所有配對的好感度的和 int res = 0; for (int i = 0; i < N; ++i) res += love[ match[i] ][i]; return res; } int f[MAXN][MAXN]; char mp[MAXN][MAXN]; typedef pair<int,int> P; vector<P> a, b; int dis(int i,int j) { return abs(a[i].first-b[j].first)+abs(a[i].second-b[j].second); } int main() { int n, m; while (scanf("%d%d",&n,&m)==2) {//N外部變量 if(n==0&&m==0) break; a.clear(), b.clear(); for(int i = 0; i < n; i++){ scanf("%s",mp[i]); for(int j = 0; j < m; j++){ if(mp[i][j]==‘m‘){ a.push_back(P(i,j)); } if(mp[i][j]==‘H‘){ b.push_back(P(i,j)); } } } N = a.size(); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) love[i][j] = -dis(i,j); printf("%d\n", -KM()); } return 0; }
hdu1533 Going Home km算法解決最小權完美匹配