One question per day (12) -- calculate slim span (and query set)

Source: Internet
Author: User

First, summarize and check the set:

Merge by rank: merge by the depth of the tree and the size of the tree, with the same effect.

 

 

 

 

 

Question ID: poj3522

Slim Span
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:5098   Accepted:2671

Description

Given an undirected Weighted GraphG, You shoshould find one of spanning trees specified as follows.

The graphGIs an Ordered Pair (V,E), WhereVIs a set of vertices {V1,V2 ,...,Vn} AndEIs a set of undirected edges {E1,E2,
...,Em}. Each edgeEεEHas its weightW(E).

A Spanning TreeTIs a tree (a connected subgraph without cycles) which connects all the n verticesN−1 edges. The slimness of a spanning treeTIs defined as the difference between the largest weight and the smallest weight
AmongN−1 edgesT.


Figure 5: A Graph GAnd the weights of the edges

For example, a graphGIn Figure 5 (A) has four vertices {V1,V2,V3,V4} and five undirected edges {E1,E2,E3,E4,E5 }.
The weights of the edges areW(E1) = 3,W(E2) = 5,W(E3) = 6,W(E4) = 6,W(E5) = 7 as shown in Figure 5 (B ).


Figure 6: Examples of the spanning trees G

There are several spanning treesG. Four of them are depicted in figure 6 ()~ (D). The Spanning TreeTaIn Figure 6 (a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that
The slimness of the treeTaIs 4. The slimnesses of spanning treesTB,TCAndTDShown in Figure 6 (B), (c) and (d) are 3, 2 and 1, respectively. You can easily see the slimness
Any other spanning tree is greater than or equal to 1, thus the Spanning Tree TD in Figure 6 (d) is one of the slimmest spanning trees whose slimness is 1.

Your job is to write a program that computes the smallest slimness.

Input

The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.

N M  
A1 B1 W1
  Bytes  
AM BM Wm

Every input item in a dataset is a non-negative integer. items in a line are separated by a space. n is the number of the vertices and m the number of the edges. you can assume 2 ≤N≤ 100 and 0 ≤MN(N−1)/2.AK
AndBK(K= 1 ,...,M) Are positive integers less than or equalN, Which represent the two verticesVakAndVbkConnected byKTh edgeEk.WK
Is a positive integer less than or equal to 10000, which indicates the weightEk. You can assume that the GraphG= (V,E) Is simple, that is, there are no self-loops (that connect the same vertex) nor parallel
Edges (that are two or more edges whose both ends are the same two vertices ).

Output

For each dataset, if the graph has spanning trees, the smallest slimness among them shocould be printed. Otherwise, −1 shocould be printed. An output shocould not contain in extra characters.

Sample Input

4 51 2 31 3 51 4 62 4 63 4 74 61 2 101 3 1001 4 902 3 202 4 803 4 402 11 2 13 03 11 2 13 31 2 22 3 51 3 65 101 2 1101 3 1201 4 1301 5 1202 3 1102 4 1202 5 1303 4 1203 5 1104 5 1205 101 2 93841 3 8871 4 27781 5 69162 3 77942 4 83362 5 53873 4 4933 5 66504 5 14225 81 2 12 3 1003 4 1004 5 1001 5 502 5 503 5 504 1 1500 0

Sample output

1200-1-110168650

 

 

 

In this case, the Kruskal algorithm is transformed, instead of the Minimum Spanning Tree, but the spanning tree with the smallest difference between the maximum edge weight and the minimum edge weight. You can also use the implementation method of the Kruskal algorithm, use and query set. If the minimum spanning tree needs to add the edge to the heap, and you do not need to traverse all spanning trees

// This question is distorted by the Kruskal algorithm, instead of finding the Minimum Spanning Tree // but the minimum difference between the maximum edge weight and the minimum edge weight, the Spanning Tree // can also be implemented using the Kruskal algorithm, use and check set # include <iostream> # include <stdlib. h >#include <algorithm> # define INF 10000000 // upper bound of edge weight; using namespace STD; int M, N; // The P array is used to record the parent node, the r array is used to count the number of points in the middle of the Child // tree rooted in R. In fact, the R array can be canceled, because // The number of elements in the set can be directly determined by the absolute value of the root node; int P [105]; int R [105]; struct edge {int U; int V; int W;} e [5000]; bool CMP (const edge & E1, const edge & E2) {return e1.w <e2.w;} void set_ I Nit () {// and check set initialization; For (INT I = 1; I <= N; I ++) {P [I] = I; R [I] = 1; // In the initial state, each root is its own, and the number of sets is 1 }}// int set_find (INT X) {// search for the root of the Set Tree Containing x // while (P [x]> = 0) // {// X = P [x]; ///} // return X; // The above is non-recursive. Recursive Implementation: int set_find (int x) {If (P [x] = X) return X; else P [x] = set_find (P [x]); Return P [X];} // void set_union (INT root1, int root2) {// merge the two sets. // P [root1] + = P [root2]; // P [root2] = root1; // use root root2 to connect to the bottom of root1 /// to prevent tree degradation, skip The union operation of is as follows void set_union (int x, int y) {// merge two sets if (R [x] <= R [y]) {P [x] = y; R [y] + = R [X]; // y as the root} else {P [y] = X; R [x] + = R [y] ;}} int main () {int I, J, K, ANS, PX, Py, A, B, temp; while (CIN> N> m) {If (n = 0 & M = 0) break; for (I = 0; I <m; I ++) {CIN> E [I]. u> E [I]. v> E [I]. w;} If (n = 2 & M = 1) {cout <"0" <Endl; // If the side is at, the continue is processed separately ;} sort (E, E + M, CMP); ans = inf; for (I = 0; I <m; I ++) {k = 0; set_init (); for (j = I; j <m; j ++) {PX = set_find (E [J]. U); py = set_find (E [J]. V); If (PX! = Py) {set_union (PX, Py); // note that survival is a tree, not necessarily an MST. You can select this edge if it is not in the same set, the two endpoints are changed to k ++ in the same set; If (k = 1) {// This is the smallest side A = E [J]. w;} else if (k = N-1) // This is the largest edge and the last edge {B = E [J]. w; temp = B-A; If (temp <ans) {ans = temp;} break ;}}} if (ANS = inf) {cout <"-1" <Endl;} else cout <ans <Endl;} return 0 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.