POJ 3522--slim Span —————— minimum spanning tree, maximum edge and minimum edge

Source: Internet
Author: User

Slim Span
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7102 Accepted: 3761

Description

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

The graph 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 ee have its weight w(e).

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


Figure 5:a Graph GAnd the weights of the edges

For example, a graph G in Figure 5 (a) have four vertices {v1, v2, v3, v4} An D 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 is several spanning trees for G. Four of them is depicted in Figure 6 (a) ~ (d). The Spanning Tree Ta in Figure 6 (a) had 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) is 3, 2 and 1 , respectively. You can easily see the slimness of any other spanning tree are 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 program that computes the smallest slimness.

Input

The input consists of multiple datasets, followed by a line containing and 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. Items in a line is 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. AK and BK (k = 1, ...,m) is positive integers less than or equal to n, which R Epresent the vertices vak and VBK connected by the k-th edge ek. wk is a positive an integer less than or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (V, E) is easy, that's, there is no self-loops (tha T connect the same vertex) nor parallel edges (that is, or more edges whose both ends is the same-vertices).

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 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 12 01 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 49 33 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 Main topic: let you ask for a spanning tree, the maximum value of the tree edge is the smallest difference from the minimum value. The idea of solving problems: in fact, it is kruskal to find the minimum spanning tree. Violent enumeration of different minimum edges.
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream>using namespace    std;const int MAXN = 110;const int maxe = 11010;struct edge{int from,to,dist,idx; Edge () {} edge (int _from,int _to,int _dist,int _idx): From (_from), to (_to), Dist (_dist), idx (_IDX) {}}edges[maxe];struct set{int Pa,rela;} Sets[maxn];int Ans[maxn];bool cmp (Edge A,edge b) {return a.dist < b.dist;}    void init (int n) {for (int i = 0; I <= N; i++) {SETS[I].PA = i;    }}int Find (int x) {if (x = = SETS[X].PA) {return x;    } int tmp = SETS[X].PA;    SETS[X].PA = Find (TMP); return SETS[X].PA;}    int main () {int n, m;        while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {init (n);        int a,b,c;            for (int i = 0; i < m; i++) {scanf ("%d%d%d", &a,&b,&c);        Edges[i] = Edge (a,b,c,i);        } sort (edges,edges+m,cmp);        int pos = 0, cnt = 0;           for (int i = 0; i < m; i++) { Edge & e = edges[i];            int Rootx, Rooty;            Rootx = Find (E.from);            Rooty = Find (e.to);            if (Rootx = = Rooty) {continue;            } cnt++;            SETS[ROOTY].PA = Rootx;        pos = i;            } if (cnt! = n-1) {puts ("-1");        Continue        } int ans = edges[pos].dist-edges[0].dist;            for (int j = 1; J <= M-n + 1; j + +) {cnt = 0;            for (int i = 0; I <= N; i++) {SETS[I].PA = i;                } for (int i = j; i < m; i++) {Edge & e = edges[i];                int Rootx, Rooty;                Rootx = Find (E.from);                Rooty = Find (e.to);                if (Rootx = = Rooty) {continue;                } SETS[ROOTY].PA = Rootx;                cnt++;            pos = i;            } if (CNT < n-1) {break;             }else{   int tmp = edges[pos].dist-edges[j].dist;            ans = min (ans,tmp);    }} printf ("%d\n", ans); } return 0;}

  

POJ 3522--slim Span —————— minimum spanning tree, maximum edge and minimum edge

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.