Slim Span
[PDF Link]
Given an undirected weighted graph G , you should 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 edge e e have its weight W(e).
A Spanning TreeTis a tree (a connected subgraph without cycles) which connects all theNVertices with N -1Edges. Theslimnessof a spanning treeTis defined as the difference between the largest weight and the smallest weight among the N -1Edges ofT.
For example, a graphGIn Figure 5 (a) had four vertices{v1, v2, v3, v4}and five undirected edges{e1, e2, e3, e4, e5 }. The weights of the edges are W(e1) = 3, W(e2) = 5, W(e3) = 6, W(e4) = 6, W(e5) = 7As shown in Figure 5 (b).
=6in
There is several spanning trees forG. Four of them is depicted in Figure 6 (a) ∼ (d). The Spanning tree Ta In Figure 6 (a) have three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight are 3 so that the slimness of the tree Ta is 4. The slimnesses of spanning trees Tb , Tc and Td shown in Figure 6 (b), (c) and (d) is 3, 2 and 1, respectively. can easily see the slimness of any other spanning tree are greater than or equal to 1, thus the spanning tree Td In Figure 6 (d) are 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 and zeros separated by a space. Each dataset has the following format.
Nm
a1 b1 w1
am bm wm
Every input item in a dataset is a non-negative integer. Items in a line is separated by a space.
NIs the number of the vertices andmThe number of the edges. You can assume2Nand0mn(n -1)/2. ak and bk (k = 1,..., m)is positive integers less than or equal toN, which represent the vertices vak and vbk Connected by thek-th Edge ek . wk is a positive integer less than or equal to 10000, which indicates the weight of ek . Can assume that the graph G = (V, E)is simple, which is, there be no self-loops (that connect the same vertex) nor parallel edges Whose both ends is the same of the vertices).
Output
For each dataset, if the graph has spanning trees, the smallest slimness among them should is printed. Otherwise, '-1' should be printed. An output should not contain extra characters.
Sample Input
4 5 1 2 31 3 51 4 62 4 63 4 74 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 13 0 3 1 1 2 13 3 1 2 22 3 5 1 3 6 5 1 0 1 2 110 1 3 120 1 4 130 1 5 120 2 3 110 2 4 120 2 5 130 3 4 120 3 5 110 4 5 120 5 10 1 2 9384 1 3 887 1 4 2778 1 5 6916 2 3 7794 2 4 8336 2 5 5387 3 4 493 3 5 6650 4 5 1422 5 8 1 2 1 2 3 100 3 4 100 4 5 100 1 5 50 2 5 50 3 5 50 4 1 150 0 0
Sample Output
1 20 0-1-1 1 0 1686 50
Main topic:
A graph of N-nodes is given to find the smallest possible spanning tree (the value of the maximum edge minus the minimum edge).
Problem Solving Ideas:
First, the edges are sorted by weight from small to large. For a continuous set of boundary "L,r", if these edges make n points all connected, there must be a slim degree not exceeding w[r]-w[l] of the spanning tree.
From small to large enumeration L, for each l, from small to large enumeration R, at the same time use and check set will first enter the "L,r" at the end of the point to merge into a set, and the same as the Kruskal algorithm. Stop enumerating R when all points are connected, and replace an L to continue enumeration.
UVA1395 Slim Span (Kruskal algorithm)