eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Slim Span

Slim Span

Given an undirected weighted graph \textbf{G}, you should find one of spanning trees specified as follows. \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} The graph \textbf{G} is an ordered pair \textbf{(V, E)}, where \textbf{V} is a set of vertices \{\textbf{v_1}, \textbf{v_2}, ..., \textbf{v_n}\} and \textbf{E} is a set of undirected edges \{\textbf{e_1}, \textbf{e_2}, ..., \textbf{e_m}\}. Each edge \textbf{eE} has its weight \textbf{w(e)}. A spanning tree \textbf{T} is a tree (a connected subgraph without cycles) which connects all the \textbf{n} vertices with \textbf{n−1} edges. The slimness of a spanning tree \textbf{T} is defined as the difference between the largest weight and the smallest weight among the \textbf{n−1} edges of \textbf{T}. \includegraphics{https://static.e-olymp.com/content/69/6986b3fc6c1be5804388965721a86160434fc3fc.jpg} \textit{\textbf{Figure 1}}: A graph \textbf{G} and the weights of the edges For example, a graph \textbf{G} in \textit{\textbf{Figure 1(a)}} has four vertices \{\textbf{v_1}, \textbf{v_2}, \textbf{v_3}, \textbf{v_4}\} and five undirected edges \{\textbf{e_1}, \textbf{e_2}, \textbf{e_3}, \textbf{e_4},\textbf{e_5}\}. The weights of the edges are \textbf{w(e_1) = 3}, \textbf{w(e_2) = 5}, \textbf{w(e_3) = 6}, \textbf{w(e_4) = 6}, \textbf{w(e_5) = 7} as shown in \textit{\textbf{Figure 1(b)}}. \includegraphics{https://static.e-olymp.com/content/f3/f31f8c53a100ff40a06e3efc197ad4f14939dd4b.jpg} \textit{\textbf{Figure 2}}: Examples of the spanning trees of \textbf{G} There are several spanning trees for \textbf{G}. Four of them are depicted in \textit{\textbf{Figure 2(a)'(d)}}. The spanning tree \textbf{T_a} in\textit{\textbf{Figure 2(a)}} has three edges whose weights are \textbf{3}, \textbf{6} and \textbf{7}. The largest weight is \textbf{7} and the smallest weight is \textbf{3} so that the slimness of the tree \textbf{T_a} is \textbf{4}. The slimnesses of spanning trees \textbf{T_b}, \textbf{T_c} and \textbf{T_d} shown in \textit{\textbf{Figure 2(b)}}, \textit{\textbf{(c)}} and\textit{\textbf{(d)}} are \textbf{3}, \textbf{2} and \textbf{1}, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to \textbf{1}, thus the spanning tree \textbf{T_d} in \textit{\textbf{Figure 2(d)}} is one of the slimmest spanning trees whose slimness is \textbf{1}. Your job is to write a program that computes the smallest slimness. \InputFile The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format. \textbf{n m} \textbf{a_1 b_1 w_1} \textbf{...} \textbf{a_m b_m w_m} Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. \textbf{n} is the number of the vertices and m the number of the edges. You can assume \textbf{2} ≤ \textbf{n} ≤ \textbf{100} and \textbf{0} ≤ \textbf{m} ≤ \textbf{n(n−1)/2}. \textbf{a_k} and \textbf{b_k} (\textbf{k = 1}, ..., \textbf{m}) are positive integers less than or equal to \textbf{n}, which represent the two vertices \textbf{v_ak} and \textbf{v_bk} connected by the \textbf{k}th edge \textbf{e_k}. \textbf{w_k} is a positive integer less than or equal to \textbf{10000}, which indicates the weight of \textbf{e_k}. You can assume that the graph \textbf{G = (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). \OutputFile For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, \textbf{−1} should be printed. An output should not contain extra characters.
Ліміт часу 5 секунд
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
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
Вихідні дані #1
1
20
0
-1
-1
1
0
1686
50
Джерело ACM ICPC Japan Regional 2007