Tourtime limit:1000msmemory limit:65535kbthis problem'll be judged onHDU. Original id:3488
64-bit integer IO format: %i64d Java class name: Main In the Kingdom of Henryy, there is n (2 <= n <=) cities, with M (M <= 30000) one-way roads connecting them. You is lucky enough to has a chance to has a tour in the kingdom. The route should is designed as:the route should contain one or more loops. (a loop is a route like:a->b->......->p->a.)
Every city should is just in one route.
A Loop should has at least and cities. In one route, each city should is visited just once. (The only exception is, the first and the last city should are the same and this are visited twice.)
The total distance the N roads you has chosen should be minimized.
InputAn integer T in the first line indicates the number of the the test cases.
In each test case, the first line contains integers N and M, indicating the number of the cities and the one-way roads . Then M lines followed, each of the line have three integers U, V and W (0 < W <= 10000), indicating that there is a road fro M U to V, with the distance of W.
It is guaranteed, at least one valid arrangement of the tour is existed.
A Blank line was followed after each test case.
OutputFor each test case, output a line with exactly one integer, which are the minimum total distance.
Sample Input
16 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 4
Sample Output
42
SourceACM-ICPC multi-university Training Contest (6)--host by BIT problem solving: Just like the cyclic tour in front of you.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =310;4 Const intINF =0x3f3f3f3f;5 intW[MAXN][MAXN],LX[MAXN],LY[MAXN],SLACK[MAXN];6 intN,LINK[MAXN];7 BOOLS[MAXN],T[MAXN];8 BOOLMatchintu) {9S[u] =true;Ten for(intv =1; V <= N; ++v) { One if(T[v])Continue; A intD = lx[u] + ly[v]-W[u][v]; - if(!d) { -T[V] =true; the if(Link[v] = =-1||match (Link[v])) { -LINK[V] =u; - return true; - } +}Else if(Slack[v] > D) slack[v] =D; - } + return false; A } at voidUpdate () { - intD =INF; - for(intv =1; V <= N; ++v) - if(! T[V] && Slack[v] <d) -D =Slack[v]; - for(intU =1; U <= N; ++u) { in if(S[u]) Lx[u]-=D; - if(T[u]) Ly[u] + =D; to ElseSlack[u]-=D; + } - } the intKuhnmunkras () { * for(intU =1; U <= N; ++u) { $Lx[u] =-INF;Panax NotoginsengLy[u] =0; -Link[u] =-1; the for(intv =1; V <= N; ++v) +Lx[u] =Max (lx[u],w[u][v]); A } the for(intU =1; U <= N; ++u) { + for(intv =1; V <= N; ++v) -SLACK[V] =INF; $ while(true) { $memset (S),false,sizeofS); -memset (T,false,sizeofT); - if(Match (U)) Break; the update (); - }Wuyi } the intRET =0; - for(intv =1; V <= N; ++v) Wu if(Link[v] >-1) ret + =W[link[v]][v]; - returnret; About } $ - intMain () { - intm,u,v,w,kase; -scanf"%d",&Kase); A while(kase--) { +scanf"%d%d",&n,&m); the for(inti =1; I <= N; ++i) - for(intj =1; J <= N; ++j) $W[I][J] =-INF; the for(inti =0; I < m; ++i) { thescanf"%d%d%d",&u,&v,&W); theW[U][V] = max (w[u][v],-W); the } -printf"%d\n",-Kuhnmunkras ()); in } the return 0; the}View Code
HDU 3488 Tour