Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12614
This article link: http://www.cnblogs.com/Ash-ly/p/5495851.html
Test instructions
Give you n points, as well as M-bar, let you calculate whether there is a minimum spanning tree and sub-niche into a tree, if there is print out weights, otherwise print-1.
Ideas:
A very direct question, about the method of finding a niche into a tree, in my other article:http://www.cnblogs.com/Ash-ly/p/5494975.html
Code:
1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <cstdio>6#include <string>7 8 using namespacestd;9typedefLong LongLL;Ten One Const intMAXN = -; A Const intMaxe = -* -; - Const intINF =0x3f3f3f3f; - intPRE[MAXN +7]; the - voidInitpre (intN) { for(inti =0; I <= N; i++) Pre[i] =i;} - - //and check Set + intFind (intx) {returnx = = Pre[x]? X:PRE[X] =Find (pre[x]);} - + voidMergeintXintY) {intFX = Find (x), FY = find (y);if(FX! = FY) Pre[fx] =fy;} A at structedge{//forward-to-star storage edge - intU, v;//Start End - intW; - BOOL Select; -}edge[maxe +7]; - in BOOLCMP (Edge A, Edge b) { - if(A.W! = B.W)returnA.W <B.W; to if(a.u! = b.u)returnA.u <b.u; + returnA.V <B.V; - } the * structnode{//chain forward stars are used to store the edges inside each set $ intto ;Panax Notoginseng intNext; -}LINK[MAXN +7]; the + intHEAD[MAXN +7];//The location of the head node of the adjacency table A intEND[MAXN +7];//The location of the tail node of the adjacency table the intLENGTH[MAXN +7][MAXN +7];//Longest edge on any two-point path in the smallest spanning tree + - intKruskal (intNintm) { $ //initializes the adjacency table, adding an edge to itself for each vertex, representing only the point I in the set of elements represented by I $ for(inti =1; I <= N; i++){ -Link[i].to = i, Link[i].next =Head[i]; -End[i] = i, head[i] =i; the } -Sort (Edge +1, Edge +1+m, CMP);Wuyi intCNT =0; the for(inti =1; I <= m; i++){ - if(cnt = = N-1) Break;//when the number of edges found equals the number of nodes-1, the MST has been found Wu intFX =Find (edge[i].u); - intFY =Find (EDGE[I].V); About if(FX! =FY) { $ for(intj = Head[fx]; J! =-1; j = link[j].next)//modifying the length array - for(intk = Head[fy]; K! =-1; K =link[k].next) - //after each merge of two equivalence classes, the longest edge between the two nodes belonging to the two equivalent classes must be the currently joined Edge -Length[link[j].to][link[k].to] = length[link[k].to][link[j].to] =EDGE[I].W; A //Merging adjacency tables +Link[end[fy]].next =HEAD[FX]; theEND[FY] =END[FX]; - merge (FX, FY); $cnt++; theEdge[i].Select=true; the } the } the if(CNT < n-1)return-1; - return 1; in } the the voidinit () { Aboutmemset (Length,-1,sizeof(length)); theMemset (Head,-1,sizeof(head)); thememset (&edge,0,sizeof(Edge)); thememset (End,0,sizeof(End)); +memset (&link,0,sizeof(Node)); - } the Bayi intMain () { theFreopen ("Input.txt","R", stdin); the intN, M; - while(~SCANF ("%d%d", &n, &m)) { - init (); the initpre (n); the for(inti =1; I <= m; i++) Edge[i].Select=false; the for(inti =1; I <= m; i++) scanf ("%d%d%d", &edge[i].u, &EDGE[I].V, &EDGE[I].W); the intFlag =Kruskal (n, m); - intMST =0; the for(inti =1; I <= m; i++)if(Edge[i].Select) MST + = EDGE[I].W;//Calculate the minimum spanning tree the intSecmst =INF; the //in T/(U,V) + (x, y) to find the sub-niche into a tree94 for(inti =1; I <= m; i++)if(!edge[i].Select) secmst = min (secmst, MST + EDGE[I].W-length[edge[i].u][edge[i].v]); the if(Flag <0) printf ("Cost : -1\ncost: -1\n"); the Else if(n = = m +1) printf ("Cost :%d\ncost: -1\n", MST); the Elseprintf"Cost :%d\ncost:%d\n", MST, SECMST);98 } About return 0; -}
URAL-1416 Confidential (minimum spanning tree and sub-niche into a tree)