Poj 3522 slim span (enumeration + minimal spanning tree)

Source: Internet
Author: User

Link:

Http://poj.org/problem? Id = 3522

Question:

Slim Span
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:4962   Accepted:2587

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.

N M  
A1 B1 W1
  Bytes  
AM BM Wm

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 ≤MN(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 shocould be printed. An output shocould not contain in 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

1200-1-110168650

Source

Japan 2007 Question:A graph may constitute multiple spanning trees, and the minimum difference between the smallest edge and the largest edge in the tree is required. Analysis and Summary:Although this question is not a Minimum Spanning Tree, It is similar. Sort all edges according to the Kruskal method, enumerate the smallest edge in sequence, and start to construct the spanning tree. The smallest difference is. Code:
# Include <cstdio> # include <algorithm> using namespace STD; const int VN = 110; // number of points const int en = Vn * VN; // Number of edges const int INF = 10000000; Template <typename type> class Kruskal {public: // n indicates the number of vertices void Init (INT _ n) {n = _ n; size = 0; make_set (n); mstsum = 0;} void insert (int u, int V, type W) {If (u = V) return; E [size ++]. set (U, V, W);} type Kruskal () {sort (E, E + size); int minlen = inf; For (INT I = 0; I <size-N + 2; ++ I) {make_set (n); int tmp_min = E [I]. w, CNT = 0; For (Int J = I; j <size; ++ J) {If (Union (E [J]. u, E [J]. v) {If (++ CNT = N-1) {If (E [J]. w-tmp_min <minlen) minlen = E [J]. w-tmp_min; If (minlen = 0) return 0; // if it is 0, it is already the smallest break; }}} if (minlen = inf) Return-1; return minlen;} PRIVATE: struct edge {int U, V; type W; void set (int A, int B, type C) {u = A; V = B; W = C;} friend bool operator <(const edge & A, const edge & B ){ Return. W <B. W ;}} E [En]; int f [VN], rank [VN]; int N, size; // n indicates the number of points, M is the number of edges type mstsum; // perform the void make_set (int n) query operation on the set {for (INT I = 0; I <= N; ++ I) f [I] = I, rank [I] = 0;} int find (int x) {int I, j = x; while (J! = F [J]) J = f [J]; while (X! = J) {I = f [X]; F [x] = J; X = I;} return J;} bool Union (INT X, int y) {int A = find (x), B = find (y); if (a = B) Return false; If (rank [a]> rank [B]) f [B] = A; else {If (rank [a] = rank [B]) + + rank [B]; F [a] = B ;} return true ;}}; Kruskal <int> G; int n, m; int main () {int A, B, C; while (~ Scanf ("% d", & N, & M) & N + M) {G. init (n); For (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B, & C); G. insert (a, B, c);} printf ("% d \ n", G. kruskal ();} return 0 ;}
 
  

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

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.