3 4 2 0 1 3 0 2 4 1 3 2 2 3 2 0 0 0
14
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> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 using namespace std; 16 const int maxn = 600; 17 const int INF = 0x3f3f3f; 18 struct arc{ 19 int to,flow,cost,next; 20 arc(int x = 0,int y = 0,int z = 0,int nxt = -1){ 21 to = x; 22 flow = y; 23 cost = z; 24 next = nxt; 25 } 26 }; 27 arc e[maxn*maxn]; 28 int head[maxn],d[maxn],p[maxn],dis[maxn][maxn]; 29 int tot,S,T,N,M,K; 30 void add(int u,int v,int flow,int cost){ 31 e[tot] = arc(v,flow,cost,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0,-cost,head[v]); 34 head[v] = tot++; 35 } 36 bool in[maxn]; 37 bool spfa(){ 38 queue<int>q; 39 const int inf = 0x3f3f3f3f; 40 for(int i = 0; i < maxn; ++i){ 41 p[i] = -1; 42 in[i] = false; 43 d[i] = inf; 44 } 45 d[S] = 0; 46 q.push(S); 47 while(!q.empty()){ 48 int u = q.front(); 49 q.pop(); 50 in[u] = false; 51 for(int i = head[u]; ~i; i = e[i].next){ 52 if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){ 53 d[e[i].to] = d[u] + e[i].cost; 54 p[e[i].to] = i; 55 if(!in[e[i].to]){ 56 in[e[i].to] = true; 57 q.push(e[i].to); 58 } 59 } 60 } 61 } 62 return p[T] > -1; 63 } 64 int solve(){ 65 int ans = 0; 66 while(spfa()){ 67 int minF = INF; 68 for(int i = p[T]; ~i; i = p[e[i^1].to]) 69 minF = min(minF,e[i].flow); 70 for(int i = p[T]; ~i; i = p[e[i^1].to]){ 71 e[i].flow -= minF; 72 e[i^1].flow += minF; 73 } 74 ans += d[T]*minF; 75 } 76 return ans; 77 } 78 void Floyd(){ 79 for(int k = 0; k <= N; ++k){ 80 for(int i = 0; i <= N; ++i) 81 for(int j = 0; j <= N; ++j) 82 dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]); 83 } 84 } 85 int main(){ 86 int u,v,w; 87 while(scanf("%d %d %d",&N,&M,&K),N||M||K){ 88 memset(head,-1,sizeof(head)); 89 for(int i = tot = 0; i <= N; ++i){ 90 for(int j = 0; j <= N; ++j) 91 dis[i][j] = i == j?0:INF; 92 } 93 for(int i = 0; i < M; ++i){ 94 cin>>u>>v>>w; 95 dis[u][v] = dis[v][u] = min(dis[u][v],w); 96 } 97 S = 2*N + 1; 98 T = S + 1; 99 Floyd(); 100 add(S,0,K,0); 101 add(0,T,K,0); 102 for(int i = 1; i <= N; ++i){ 103 add(0,i,1,dis[0][i]); 104 add(i+N,T,1,dis[0][i]); 105 add(i,i+N,1,-INF); 106 for(int j = i + 1; j <= N; ++j) 107 add(i + N,j,1,dis[i][j]); 108 } 109 cout<<solve() + N*INF<<endl; 110 } 111 return 0; 112 }