POJ 3522 Slim Span: Enumeration + minimum Spanning tree

Source: Internet
Author: User
Tags integer time limit

Link:

http://poj.org/problem?id=3522

Topic:

Slim Span

Time limit:5000ms Memory limit:65536k
Total submissions:4962 accepted:2587

Description

Given an undirected weighted graph G, and should find one of spanning trees specified as follows.

The graph G is a ordered pair (V, e), where V is a set of vertices {v1, v2, ..., vn} and E is a set of undirected edges {E1 , e2, ..., em}. Each edge e∈e the has its weight w (e).

A spanning tree T are a tree (a connected subgraph without cycles) which connects all of the n vertices with n 1 edges. The slimness of a spanning T is defined as the difference between the largest weight and the smallest weight among th e n 1 edges of T.

Figure 5:a Graph G and the weights of the edges

For example, a graph G in Figure 5 (a) has 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) = 7 as shown in Figure 5 (b).

Figure 6:examples of the spanning trees of G

There are several spanning trees for G. Four of them are depicted in Figure 6 (a) ~ (d). The Spanning Tree Ta Figure 6 (a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight are 3 so, 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) are 3, 2 and 1, respectively. You can easily the slimness of an any other spanning's is greater than or equal to 1, thus the spanning tree Td in Fig Ure 6 (d) is one of the slimmest spanning trees whose slimness is 1.

Your job is to write a computes the smallest slimness.

Input

The input consists of multiple datasets, followed by a line containing two zeros separated by a. Each dataset has the following format.

 
m
b1 w1
     
BM wm

Every input item in a dataset is a non-negative integer. Items in a line are separated by a. n is the number of the vertices and m the number of the edges. You can assume 2≤n≤100 and 0≤m≤n (n 1)/2.ak and BK (k = 1,. ..., m)-are positive integers less than or equal to N, whi Ch represent the two vertices vak and vbk connected by the kth edge Ek. WK is a positive an integer less than or equal to 10000, which indicates the weight of EK. You can assume this graph G = (V, E) is simple, which is, there are no self-loops (that connect the same vertex) nor PA Rallel edges (that is are two or more edges the whose both ends the are same).

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 3 1 3 5 1 4 6 2 4 6 3 4 7 4 6 1 2 1 3 1/4 2/3
80
  3 4,
2 1 1 2 1 3 0 3 1 1 2 1 3 3 1 2 2 2 3 5 1 3 6 5
ten
1 2 11 0
1 3,
1 4 130 1 5, 2 3, 2 4, 2 5 130 3 4, 3 5, 4
5 120
  5
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 3 4 4 5 1 5 2 5 3
50
  
   4 1
0 0
  

Sample Output

1
0
-1
-1 1 0
1686
50

Source

Japan 2007

The main effect of the topic:

A diagram may be composed of multiple spanning trees, then the difference between the smallest edge of the spanning tree and the largest edge is the smallest.

Analysis and Summary:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.