Title Link: https://vjudge.net/problem/UVA-10462
Nasa, being the most talented programmer of he time, can ' t think things to being so simple. Recently all neighbors has decided to connect themselves over a network (actually all of the them want to share a BROADBA nd Internet connection:-)). But he wants to minimize the total cost of cable required as he was a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second the left. I mean, he wants to know the second best cost (if there are any which could be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he's very busy with his private affairs (?) and he'll remain so. So, it's your turn to prove yourself a good programmer. Take the challenge (if brave enough) ...
Input
Input starts with a integer t≤1000 which denotes the number of test cases to handle. Then follows t datasets where every datasets starts with a pair of integers V (1≤v≤100) and E (0≤e≤200). V denotes the number of neighbors and E denotes the number of allowed direct connections among them. The following E lines contain the description of the allowed direct connections where each line is of the form ' Start end Cost ', where start and end were the ends of the connection and cost was the cost of the connection. All connections is bi-directional and there may be multiple connections between.
Output
There may is three cases in the output 1. No-to-complete the task, 2. There is only one-to-complete the task, 3. There is more than one. Output ' No ' to the first case, ' no second-the-the-the-the-second case and a integer c for the third case where C is the Second best cost. Output for a case should start with a new line.
Sample Input 4 5 4 1 2 5 3 2 5 4 2 5 5 4 5 5 3 1 2 5 3 2 5 5 4 5 5 5 1 2 5 3 2 5 4 2 5 5 4 5 4 5 6 1 0
Sample Output
Case #1: No second
Case #2: No
Case #3:21
Case #4: No second
Exercises
1. Seek the niche into a tree. But the problem requires a heavy edge, and the prim algorithm deals with the point-to-point relationship and cannot (at least) handle graphs with heavy edges.
2. The minimum spanning tree, in addition to the prim algorithm, also has a kruskal algorithm, and because it is directly based on the edge to operate, so it is good to handle the heavy edge.
3. Step: First use the Kruskal algorithm to find the minimum spanning tree, and record the minimum spanning tree edge. Then the enumeration deletes each of the minimum spanning tree edges, then the smallest spanning tree, and finally the sub-niche tree.
4. Complexity analysis: O (n*m), calculated number of 2e4, then multiply the case number, the number of calculations is 2E7, so it is feasible.
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6typedefLong LongLL;7 Const DoubleEPS = 1e-6;8 Const intINF =2e9;9 ConstLL LNF =9e18;Ten Const intMOD = 1e9+7; One Const intMAXN = 1e2+Ten; A - structEdge - { the intu, V, W; - BOOL operator< (ConstEdge &a)Const{ - returnw<A.W; - } +}edge[maxn<<1]; - intFA[MAXN], USED[MAXN]; + A intFindintx) {returnfa[x]==-1? x:x=find (Fa[x]);} at - intKruskal (intNintMintpointed) - { - intCNT =0, sum =0; -memset (FA,-1,sizeof(FA)); - for(inti =1; i<=m; i++) in { - if(i==pointed)Continue;//if it is the smallest spanning tree edge that is specified for deletion, skip to + intU =find (EDGE[I].U); - intv =find (EDGE[I].V); the if(u!=v) * { $Fa[u] =v;Panax NotoginsengSum + =EDGE[I].W; -++cnt;//error: Can not put in the next sentence inside, that is used[++cnt]=i, because this statement must be executed!!! the if(pointed==-1) used[cnt] = i;//If the minimum spanning tree is obtained, add the edge numbered I to the spanning tree + if(cnt==n-1)returnSum//merged n-1 times, has become a tree, can be returned directly A } the } + returncnt< (n1)? Inf:sum;//cannot form a tree when the number of merges is less than n-1 - } $ $ intMain () - { - intT, N, M; thescanf"%d", &T); - for(intKase =1; kase<=t; kase++)Wuyi { thescanf"%d%d",&n,&m); - for(inti =1; i<=m; i++) Wuscanf"%d%d%d", &edge[i].u, &EDGE[I].V, &EDGE[I].W); - AboutSort (edge+1, edge+1+m); $ - intT1 = INF, t2 =INF; -T1 = Kruskal (n, M,-1);//Finding the minimum spanning tree - for(inti =1; i<=n-1; i++)//enumeration deletes each of the smallest spanning tree edges, seeking a second niche into a tree A { + intTMP =Kruskal (n, M, Used[i]); thet2 =min (t2, TMP); - } $ the if(T1==inf)//the original is not connected, there is no minimum spanning tree theprintf"Case #%d:no way\n", Kase); the Else if(T2==inf)//no time to become a tree theprintf"Case #%d:no Second way\n", Kase); - Else inprintf"Case #%d:%d\n", Kase, T2); the } the}
View Code
Uva10462is there A Second the left?--sub-niche tree Kruskal algorithm