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 of 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.
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 ≤M≤N(N? 1)/2.AKAndBK(K= 1 ,...,M) Are positive integers less than or equalN, Which represent the two verticesVakAndVbkConnected byKTh edgeEk.WKIs 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 shoshould be printed. An output shoshould not contain 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
1
20
0
-1
-1
1
0
1686
50
Question and analysis:
Multiple Spanning Trees exist in an undirected graph, requiring a special spanning tree. The longest side of the tree minus the shortest side to get the minimum value. Is the most slim spanning tree.
Because it is slim, the difference between the longest side and the shortest side is the smallest. Then, we sort the edge weights from small to large. The number is 1 ~ M. Start from the side numbered 1. Use this edge as the first edge of the tree and locate it until you find a spanning tree. Then, the first side of the tree is generated by side 2, and the Spanning Tree is searched. Until all cases are enumerated. Compare the slim value of the generated tree each time, and take the smallest one.
AC code: