Uva1395 & poj3522 -- slim span [Kruskal] bottleneck generation tree

Source: Internet
Author: User

Link:Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4141

Question:N vertices and m edges are given, and a spanning tree is obtained to minimize the difference between the maximum edge and the minimum edge.


Ideas:Finding a spanning tree to minimize the maximum edge is the bottleneck spanning tree. For this question, we enumerate the minimum edge of each edge, find the maximum edge of the corresponding Minimum Spanning Tree, and find the minimum difference between the maximum edge and the minimum edge.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 50100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{    int u,v,w;}edge[MAXN];int n,m,father[110],maxm;bool cmp(node x,node y){    return x.w<y.w;}int Find(int x){    int t = x;    while(t!=father[t])        t = father[t];    int k = x;    while(k!=t){        int temp = father[k];        father[k] = t;        k = temp;    }    return t;}bool kruskal(int x){    int i,j;    maxm = 0;    for(i=0;i<=n;i++)    father[i] = i;    int sum = 0;    for(i=x;i<m;i++){        int a = Find(edge[i].u);        int b = Find(edge[i].v);        if(a!=b){            father[a] = b;            sum++;            maxm = max(maxm,edge[i].w);            if(sum>=n-1)    return true;        }    }    return false;}int main(){    int i,j;    while(scanf("%d%d",&n,&m),n||m){        for(i=0;i<m;i++){            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);        }        sort(edge,edge+m,cmp);        int ans = INF;        for(i=0;i<m;i++){            if(m-i<n-1) break;            if(kruskal(i)){                ans = min(ans,maxm-edge[i].w);            }        }        if(ans==INF)    puts("-1");        else        printf("%d\n",ans);    }    return 0;}


Uva1395 & poj3522 -- slim span [Kruskal] bottleneck generation tree

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.