HDU 3488 Tour

Tour

Time Limit: 1000ms
Memory Limit: 65535KB
This problem will be judged on HDU. Original ID: 3488
64-bit integer IO format: %I64d      Java class name: Main
 
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.
 

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.
 

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

 
解题:跟前面那道Cyclic Tour一样的说
 
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 310;
 4 const int INF = 0x3f3f3f3f;
 5 int W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn];
 6 int n,Link[maxn];
 7 bool S[maxn],T[maxn];
 8 bool match(int u) {
 9     S[u] = true;
10     for(int v = 1; v <= n; ++v) {
11         if(T[v]) continue;
12         int d = Lx[u] + Ly[v] - W[u][v];
13         if(!d) {
14             T[v] = true;
15             if(Link[v] == -1 || match(Link[v])) {
16                 Link[v] = u;
17                 return true;
18             }
19         }else if(slack[v] > d) slack[v] = d;
20     }
21     return false;
22 }
23 void update() {
24     int d = INF;
25     for(int v = 1; v <= n; ++v)
26         if(!T[v] && slack[v] < d)
27             d = slack[v];
28     for(int u = 1; u <= n; ++u) {
29         if(S[u]) Lx[u] -= d;
30         if(T[u]) Ly[u] += d;
31         else slack[u] -= d;
32     }
33 }
34 int KuhnMunkras() {
35     for(int u = 1; u <= n; ++u) {
36         Lx[u] = -INF;
37         Ly[u] = 0;
38         Link[u] = -1;
39         for(int v = 1; v <= n; ++v)
40             Lx[u] = max(Lx[u],W[u][v]);
41     }
42     for(int u = 1; u <= n; ++u) {
43         for(int v = 1; v <= n; ++v)
44             slack[v] = INF;
45         while(true) {
46             memset(S,false,sizeof S);
47             memset(T,false,sizeof T);
48             if(match(u)) break;
49             update();
50         }
51     }
52     int ret = 0;
53     for(int v = 1; v <= n; ++v)
54         if(Link[v] > -1) ret += W[Link[v]][v];
55     return ret;
56 }
57 
58 int main() {
59     int m,u,v,w,kase;
60     scanf("%d",&kase);
61     while(kase--) {
62         scanf("%d%d",&n,&m);
63         for(int i = 1; i <= n; ++i)
64             for(int j = 1; j <= n; ++j)
65                 W[i][j] = -INF;
66         for(int i = 0; i < m; ++i){
67             scanf("%d%d%d",&u,&v,&w);
68             W[u][v] = max(W[u][v],-w);
69         }
70         printf("%d
",-KuhnMunkras());
71     }
72     return 0;
73 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4678548.html