Poj 3522 slim Span

Source: Internet
Author: User

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. The slimness 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) values (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

You need to find the maximum side of all spanning trees minus the minimum value of the smallest side.

Idea: You can use the Kruskal idea. Connect to a tree each time, and then find the maximum edge of the tree minus the minimum edge.

AC code:

#include<stdio.h>#include<algorithm>using namespace std;#define maxn  105const int maxnb = maxn*(maxn-1)/2;int n,m;int f[maxn];struct p{    int x,y,w;}num[maxnb];int find(int x){    if(x!=f[x])        f[x]=find(f[x]);    return f[x];}bool cmp(p a,p b){    return a.w<b.w;}int kruskal(int a){    int i,tot=n,ans=-1;    for(i=1;i<=n;i++)        f[i]=i;    for(i=a;i<m;i++)    {        int u=find(num[i].x);        int v=find(num[i].y);        if(u==v)            continue;        f[u]=v;        tot--;        if(tot==1)        {            ans=num[i].w;            break;        }    }    return ans-num[a].w;}int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        if(n==0&&m==0)break;        int i,j,sum;        for(i=0;i<m;i++)            scanf("%d%d%d",&num[i].x,&num[i].y,&num[i].w);        sort(num,num+m,cmp);        sum=kruskal(0);        if(sum<0)        {            printf("-1\n");            continue;        }        for(i=1;i<m;i++)        {            int ans=kruskal(i);            if(ans<0)break;            if(sum>ans)                sum=ans;        }        printf("%d\n",sum);    }    return 0;}


Poj 3522 slim 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.