图论(二分图,KM算法):HDU 3488 Tour

Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2628    Accepted Submission(s): 1285


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.
 

 

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
 
   这题就是KM算法的模板。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=220;
 6 const int INF=2147483647;
 7 int w[maxn][maxn],sx[maxn],sy[maxn],lx[maxn],ly[maxn];
 8 int match[maxn],slack[maxn];
 9 int n,m;
10 
11 bool Search(int x){
12     sx[x]=true;
13     for(int y=1;y<=n;y++){
14         if(sy[y])continue;//?
15         int t=lx[x]+ly[y]-w[x][y];
16         if(t)
17             slack[y]=min(slack[y],t);
18         else{
19             sy[y]=true;
20             if(!match[y]||Search(match[y])){
21                 match[y]=x;
22                 return true;
23             }
24         }
25     }
26     return false;
27 }
28 
29 int KM(){
30     memset(lx,0x80,sizeof(lx));
31     memset(ly,0,sizeof(ly));
32     for(int i=1;i<=n;i++)
33         for(int j=1;j<=n;j++)
34             lx[i]=max(lx[i],w[i][j]);
35     
36     for(int i=1;i<=n;i++){
37         memset(slack,127,sizeof(slack));
38         while(true){
39             memset(sx,0,sizeof(sx));
40             memset(sy,0,sizeof(sy));
41             if(Search(i))
42                 break;
43             int minn=INF;
44             for(int j=1;j<=n;j++)
45                 if(!sy[j]&&minn>slack[j])
46                     minn=slack[j];
47             
48             for(int j=1;j<=n;j++)
49                 if(sx[j])
50                     lx[j]-=minn;
51                     
52             for(int j=1;j<=n;j++)
53                 if(sy[j])
54                     ly[j]+=minn;
55                 else 
56                     slack[j]-=minn;
57         }    
58     }    
59     int ret=0;
60     for(int i=1;i<=n;i++)
61         ret+=w[match[i]][i];
62     return ret;    
63 }
64 int main(){
65     int T;
66     scanf("%d",&T);
67     while(T--){
68         scanf("%d%d",&n,&m);
69         memset(w,0x80,sizeof(w));
70         memset(match,0,sizeof(match));
71         for(int i=1,a,b,c;i<=m;i++){
72             scanf("%d%d%d",&a,&b,&c);
73             w[a][b]=max(w[a][b],-c);
74         }
75         printf("%d\n",-KM());
76     }
77     return 0;
78 }

 

posted @ 2016-03-28 18:16  TenderRun  阅读(289)  评论(0编辑  收藏  举报