Arresttime limit:1000msmemory limit:32768kbthis problem'll be judged onHDU. Original id:4411
64-bit integer IO format: %i64d Java class name: Main There is (n+1) cities on TAT Island. City 0 is where police headquarter located. The economy of other cities numbered from 1 to N ruined these years because they is all controlled by mafia. The police plan to catch all of the Mafia gangs in these N cities all over the year, and they want to succeed in a single MIS Sion. They figure out this every city except City 0 lives a mafia gang, and these gangs has a simple urgent message network:if The gang in City I (i>1) is captured, it'll send a urgent message to the gang in city I-1 and the gang in city I-1 Would get the message immediately.
The mission must is carried out very carefully. Once a gang received an urgent message, the mission would be claimed failed.
You were given the map of TAT Island which was an undirected graph. The node on the graph represents a city, and the weighted edge represents a road between both cities (the weight means the L Ength). Police headquarter have sent K squads to arrest all the mafia gangs in the rest N cities. When a squad passes a city, it can choose to arrest the gang in the city or to does nothing. These squads should return to City 0 after the arrest mission.
You should ensure the mission to being successful, and then minimize the total length of these squads traveled.
InputThere is multiple test cases.
Each test case begins with a line with three integers N, M and K, here M is the number of roads among n+1 cities. Then, there is M lines. Each of the these lines contains three integers X, Y, Len, which represents a Len kilometer road between City X and City Y. Th OSE cities including City 0 is connected.
The input is ended by "0 0 0".
restrictions:1≤n≤100, 1≤m≤4000, 1≤k≤25, 0≤len≤1000
OutputFor each test case,output a single line with a single integer that represents the minimum total length of these squads tra Veled.
Sample Input
3 4 20 1 30 2 41 3 22 3 20 0 0
Sample Output
14
SourceACM/ICPC Asia Regional Hangzhou Online Problem Solving:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the using namespacestd; - Const intMAXN = -; - Const intINF =0x3f3f3f; - structarc{ + intTo,flow,cost,next; -Arcintx =0,inty =0,intz =0,intNXT =-1){ +to =x; AFlow =y; atCost =Z; -Next =NXT; - } - }; -Arc e[maxn*MAXN]; - intHEAD[MAXN],D[MAXN],P[MAXN],DIS[MAXN][MAXN]; in inttot,s,t,n,m,k; - voidAddintUintVintFlowintCost ) { toE[tot] =arc (V,flow,cost,head[u]); +Head[u] = tot++; -E[tot] = arc (U,0,-cost,head[v]); theHEAD[V] = tot++; * } $ BOOL inch[MAXN];Panax Notoginseng BOOLSPFA () { -queue<int>Q; the Const intINF =0x3f3f3f3f; + for(inti =0; i < MAXN; ++i) { AP[i] =-1; the inch[I] =false; +D[i] =inf; - } $D[s] =0; $ Q.push (S); - while(!Q.empty ()) { - intU =Q.front (); the Q.pop (); - inch[U] =false;Wuyi for(inti = Head[u]; ~i; i =E[i].next) { the if(E[i].flow && d[e[i].to] > D[u] +e[i].cost) { -D[e[i].to] = D[u] +E[i].cost; WuP[e[i].to] =i; - if(!inch[e[i].to]) { About inch[E[i].to] =true; $ Q.push (e[i].to); - } - } - } A } + returnP[t] >-1; the } - intsolve () { $ intAns =0; the while(SPFA ()) { the intMinF =INF; the for(inti = p[t]; ~i; i = p[e[i^1].to]) theMinF =min (minf,e[i].flow); - for(inti = p[t]; ~i; i = p[e[i^1].to]) { inE[i].flow-=MinF; thee[i^1].flow + =MinF; the } AboutAns + = d[t]*MinF; the } the returnans; the } + voidFloyd () { - for(intK =0; K <= N; ++k) { the for(inti =0; I <= N; ++i)Bayi for(intj =0; J <= N; ++j) theDis[i][j] = min (dis[i][j],dis[i][k]+dis[k][j]); the } - } - intMain () { the intu,v,w; the while(SCANF (" %d%d%d", &n,&m,&k), n| | m| |j) { thememset (head,-1,sizeof(head)); the for(inti = tot =0; I <= N; ++i) { - for(intj =0; J <= N; ++j) theDIS[I][J] = i = = j?0: INF; the } the for(inti =0; i < M; ++i) {94Cin>>u>>v>>W; theDIS[U][V] = Dis[v][u] =min (dis[u][v],w); the } theS =2*n +1;98T = S +1; About Floyd (); -Add (S,0K0);101Add0, T,k,0);102 for(inti =1; I <= N; ++i) {103Add0I1, dis[0][i]);104Add (I+n,t,1, dis[0][i]); theAdd (I,i+n,1,-INF);106 for(intj = i +1; J <= N; ++j)107Add (i + n,j,1, Dis[i][j]);108 }109Cout<<solve () + n*inf<<Endl; the }111 return 0; the}
View Code
Hdu 4411 Arrest