Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4279 Accepted Submission(s): 2041
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488
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:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define INF 1e9+7 #define mem(x) memset(x,0,sizeof(x)) using namespace std; const int N = 415 , M = 30005 ; int t,n,m,ans; int match[N],w[N][N],l[N],r[N],slack[N],visx[N],visy[N]; void init(){ mem(match);mem(r);ans=0; for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) w[i][j]=INF; for(int i=1;i<=N;i++) l[i]=-INF; } int dfs(int x){ visx[x]=1; for(int i=n+1;i<=2*n;i++){ if(w[x][i]==INF || visy[i]) continue ; int tmp = l[x]+r[i]-w[x][i]; if(!tmp){ visy[i]=1; if(!match[i] || dfs(match[i])){ match[i]=x; return 1; } }else{ slack[i]=min(slack[i],tmp); } } return 0; } void update(){ int d=INF; for(int i=n+1;i<=2*n;i++) if(!visy[i]) d=min(d,slack[i]); for(int i=1;i<=n;i++) if(visx[i]) l[i]-=d; for(int i=n+1;i<=2*n;i++) if(visy[i]) r[i]+=d;else slack[i]-=d; } int KM(){ for(int i=1;i<=n;i++){ fill(slack,slack+N,INF); while(1){ mem(visx);mem(visy); if(dfs(i)) break; update(); } } for(int i=n+1;i<=n*2;i++) ans+=w[match[i]][i]; return ans; } int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); init(); for(int i=1,x,y,c;i<=m;i++){ scanf("%d%d%d",&x,&y,&c); w[x][y+n]=min(w[x][y+n],c); } for(int i=1;i<=n;i++) for(int j=n+1;j<=2*n;j++) if(w[i][j]!=INF) w[i][j]=-w[i][j],l[i]=max(l[i],w[i][j]); printf("%d ",-KM()); } return 0; }