Slim Span, slimspan

Source: Internet
Author: User

Slim Span, slimspan

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 edgeEEHas its weightW(E).

A spanning treeTIs a tree (a connected subgraph without cycles) which connects allNVerticesN-1 edges. Theslimness of a spanning treeTIs defined as the difference between the largest weight and the smallest weight amongN-1 edgesT.

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 ).

= 6in

There are several spanning treesG. Four of them are depicted in Figure 6 (a) â pretty much (d). The spanning treeTA in 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 treeTA is 4. The slimnesses of spanning treesTB,TC andTD shown 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 treeTD 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.


NM
A1B1W1

AMBMWM


Every input item in a dataset is a non-negative integer. Items in a line are separated by a space.


NIs the number of the vertices andMThe number of the edges. You can assume 2N100 and 0MN(N-1)/2.AK andBK (K= 1 ,...,M) Are positive integers less than or equalN, Which represent the two verticesVAk andVBk connected byK-Th 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 shoshould be printed. Otherwise ,'-1'Could be printed. An output shoshould 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 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

Sample Output

1 20 0 -1 -1 1 0 1686 50

N is given. m represents n points and m edges. The next m row is the weight between two vertices and them. The question must be to find the Minimum Spanning Tree; that is, use n-1 lines to connect each point and find the weight.

The difference between the largest edge and the smallest edge of the weight value. Select the smallest difference output;

Train of Thought: Sort each edge by weight from small to large, and then use the enumeration method to select the smallest difference of weight;

<Span style = "font-size: 14px;" >#include <cstdio >#include <vector> # include <algorithm >#include <iostream> using namespace std; int Panel [110], ans, n; const int INF = 10000000; struct node {int s, e, v; node (int s, int e, int v ): s (s), e (e), v (v) {}; bool operator <(const node & n) const {return v <n. v ;}}; vector <node> q; int find (int x) {return upa [x] = x? X: Panel [x] = find (Panel [x]);} // check whether the same ancestor exists, check whether the selected edge will constitute a void solve () {int I, m; m = q. size (); sort (q. begin (), q. end (); for (int L = 0; L <m; L ++) {int cnt = n; for (I = 0; I <= n; I ++) HPA [I] = I; // reset the ancestor of each number; for (int R = L; R <m; R ++) {int s = find (q [R]. s), e = find (q [R]. e); if (s! = E) {upa [s] = e; if (-- cnt = 1) {ans = ans <q [R]. v-q [L]. v? Ans: q [R]. v-q [L]. v; break ;}}}if (ans = INF) cout <-1 <"\ n"; else cout <ans <"\ n "; return;} int main () {// freopen ("a.txt", "r", stdin); int m, s, e, v; while (cin> n> m & n) {q. clear (); int I; for (I = 0; I <m; I ++) {cin> s> e> v; q. push_back (node (s, e, v) ;}ans = INF; solve ();} return 0 ;}</span>



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.