UVa1395&POJ3522--Slim Span【kruskal】瓶颈生成树

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4141

题意:给出n个顶点,m条边,求一个生成树,使得最大边与最小边的差值最小。

思路:求一个生成树使最大边最小是瓶颈生成树。对于此题,我们枚举每一条边做最小边的情况,找对应的最小生成树的最大边,找出最大边与最小边差值最小的值即可。

#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|1

struct 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;
}
时间: 2024-10-11 11:24:06

UVa1395&POJ3522--Slim Span【kruskal】瓶颈生成树的相关文章

Uva1395 POJ3522 Slim Span (最小生成树)

Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an 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

POJ 3522 Slim Span (Kruskal +枚举 边权差最小的生成树)

Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6685 Accepted: 3544 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a

POJ3522 Slim Span

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3959 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V 

POJ-3522 Slim Span(最小生成树)

Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8633   Accepted: 4608 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V 

POJ - 3522 Slim Span (kruskal+枚举)

Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an 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

【kruscal】【最小生成树】poj3522 Slim Span

求一个生成树,使得最大边权和最小边权之差最小.由于数据太小,暴力枚举下界,求出相应的上界.最后取min即可. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,m,fa[101],rank[101]; 6 void clear(){for(int i=1;i<=n;i++) fa[i]=i; memset(rank,0,sizeof

UVA1395 Slim Span(kruskal算法)

Slim Span [PDF Link] Given an undirected weighted graph G , you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E) , where V is a set of vertices {v1, v2,..., vn} and E is a set of undirected edges {e1, e2,.

UVA1395 Slim Span(kruskal)

题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边,以这条边开始利用Kruskal算法生成树,生成过程中求出权值的最大值,这个最大值减去当前枚举的边的权值就是苗条度,再动态维护一下最小苗条度就可以了. #include <iostream> #include <algorithm> #include <queue> #inc

poj 3522 Slim Span 最大边减最小边最小的生成树

枚举最小边进行kruskal. #include <cstdio> #include <algorithm> using namespace std; #define maxn 120 #define maxm 10000 struct edge { int u,v,w; }e[maxm]; int p[maxn],n,m; int find(int x) { if(x==p[x]) return x; return p[x]=find(p[x]); } void link(int