HDU3488 Tour —— 二分图最大权匹配 KM算法
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3488 Tour —— 二分图最大权匹配 KM算法相关的知识,希望对你有一定的参考价值。
题目链接:https://vjudge.net/problem/HDU-3488
Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3720 Accepted Submission(s): 1777
Problem Description
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.)
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Every city should be just in one route.
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.)
The total distance the N roads you have chosen should be minimized.
Input
An integer T in the first line indicates the number of the test cases.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W.
It is guaranteed that at least one valid arrangement of the tour is existed.
A blank line is followed after each test case.
Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
1
6 9
1 2 5
2 3 5
3 1 10
3 4 12
4 1 8
4 6 11
5 4 7
5 6 9
6 5 4
Sample Output
42
Source
Recommend
zhouzeyong
题解:
代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int INF = 0x3f3f3f3f; 5 const LL LNF = 9e18; 6 const int mod = 1e9+7; 7 const int MAXN = 2e2+10; 8 9 int nx, ny; 10 int g[MAXN][MAXN]; 11 int linker[MAXN], lx[MAXN], ly[MAXN]; 12 int slack[MAXN]; 13 bool visx[MAXN], visy[MAXN]; 14 15 bool DFS(int x) 16 { 17 visx[x] = true; 18 for(int y = 1; y<=ny; y++) 19 { 20 if(visy[y]) continue; 21 int tmp = lx[x] + ly[y] - g[x][y]; 22 if(tmp==0) 23 { 24 visy[y] = true; 25 if(linker[y]==-1 || DFS(linker[y])) 26 { 27 linker[y] = x; 28 return true; 29 } 30 } 31 else 32 slack[y] = min(slack[y], tmp); 33 } 34 return false; 35 } 36 37 int KM() 38 { 39 memset(linker, -1, sizeof(linker)); 40 memset(ly, 0, sizeof(ly)); 41 for(int i = 1; i<=nx; i++) 42 { 43 lx[i] = -INF; 44 for(int j = 1; j<=ny; j++) 45 lx[i] = max(lx[i], g[i][j]); 46 } 47 48 for(int x = 1; x<=nx; x++) 49 { 50 for(int i = 1; i<=ny; i++) 51 slack[i] = INF; 52 while(true) 53 { 54 memset(visx, 0, sizeof(visx)); 55 memset(visy, 0, sizeof(visy)); 56 57 if(DFS(x)) break; 58 int d = INF; 59 for(int i = 1; i<=ny; i++) 60 if(!visy[i]) 61 d = min(d, slack[i]); 62 63 for(int i = 1; i<=nx; i++) 64 if(visx[i]) 65 lx[i] -= d; 66 for(int i = 1; i<=ny; i++) 67 { 68 if(visy[i]) ly[i] += d; 69 else slack[i] -= d; 70 } 71 } 72 } 73 74 int res = 0; 75 for(int i = 1; i<=ny; i++) 76 if(linker[i]!=-1) 77 res += g[linker[i]][i]; 78 return res; 79 } 80 81 int main() 82 { 83 int T, n, m; 84 scanf("%d", &T); 85 while(T--) 86 { 87 scanf("%d%d", &n,&m); 88 nx = ny = n; 89 memset(g, 0, sizeof(g)); 90 for(int i = 1; i<=nx; i++) 91 for(int j = 1; j<=ny; j++) 92 g[i][j] = -INF; 93 for(int i = 1; i<=m; i++) 94 { 95 int u, v, w; 96 scanf("%d%d%d", &u, &v, &w); 97 g[u][v] = max(g[u][v], -w); 98 } 99 100 printf("%d\n", -KM()); 101 } 102 }
以上是关于HDU3488 Tour —— 二分图最大权匹配 KM算法的主要内容,如果未能解决你的问题,请参考以下文章