Slim Span

Interview

Time limit2sMemory limit128 MB

Summary
Given a weighted graph, find the spanning tree that minimizes the difference between its largest and smallest edge weight, or report -1 if disconnected.
Level

Medium4 of 10

Topics
Union-find, Sorting, Greedy
Solved
No attempts yet

Problem

Given an undirected weighted graph GG, you must find one of its spanning trees as specified below.

The graph GG is an ordered pair (V,E)(V, E), where VV is a set of vertices {v1,v2,…,vn}\{v_1, v_2, \dots, v_n\} and EE is a set of undirected edges {e1,e2,…,em}\{e_1, e_2, \dots, e_m\}. Each edge e∈Ee \in E has a weight w(e)w(e).

A spanning tree TT is a tree (a connected subgraph without cycles) that connects all nn vertices with n−1n - 1 edges. The slimness of a spanning tree TT is defined as the difference between the largest weight and the smallest weight among the n−1n - 1 edges of TT.

Figure 5: A graph GG and the weights of the edges.

For example, a graph GG in Figure 5(a) has four vertices {v1,v2,v3,v4}\{v_1, v_2, v_3, v_4\} and five undirected edges {e1,e2,e3,e4,e5}\{e_1, e_2, e_3, e_4, e_5\}. The weights of the edges are w(e1)=3w(e_1) = 3, w(e2)=5w(e_2) = 5, w(e3)=6w(e_3) = 6, w(e4)=6w(e_4) = 6, w(e5)=7w(e_5) = 7, as shown in Figure 5(b).

Figure 6: Examples of the spanning trees of GG.

There are several spanning trees for GG. Four of them are depicted in Figure 6(a)-(d). The spanning tree TaT_a in Figure 6(a) has three edges whose weights are 33, 66, and 77; the largest weight is 77 and the smallest is 33, so the slimness of TaT_a is 44. The slimnesses of the spanning trees TbT_b, TcT_c, and TdT_d shown in Figure 6(b), (c), and (d) are 33, 22, and 11, respectively. You can easily see that the slimness of any other spanning tree is at least 11, so the spanning tree TdT_d in Figure 6(d) is one of the slimmest spanning trees, with slimness 11.

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
...
am bm wm

Every input item in a dataset is a non-negative integer, and items on a line are separated by a space.

nn is the number of vertices and mm the number of edges. You may assume 2≤n≤1002 \le n \le 100 and 0≤m≤n(n−1)/20 \le m \le n(n - 1)/2. Each aka_k and bkb_k (k=1,…,mk = 1, \dots, m) is a positive integer at most nn, representing the two vertices vakv_{a_k} and vbkv_{b_k} connected by the kk-th edge eke_k. Each wkw_k is a positive integer at most 1000010000, the weight of eke_k. You may assume the graph G=(V,E)G = (V, E) is simple: it has no self-loops (edges connecting a vertex to itself) and no parallel edges (two or more edges whose endpoints are the same pair of vertices).

Output

For each dataset, if the graph has spanning trees, print the smallest slimness among them. Otherwise, print −1-1. The output must not contain extra characters.

Examples1

  1. Example 1

    Input
    4 5
    1 2 3
    1 3 5
    1 4 6
    2 4 6
    3 4 7
    4 6
    1 2 10
    1 3 100
    1 4 90
    2 3 20
    2 4 80
    3 4 40
    2 1
    1 2 1
    3 0
    3 1
    1 2 1
    3 3
    1 2 2
    2 3 5
    1 3 6
    5 10
    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
    
    Expected output
    1
    20
    0
    -1
    -1
    1
    0
    1686
    50