【Kruskal】Slim Span

[Uva1395]Slim Span

题目略……

试题分析:codevs1001舒适的路线上加一个判一下连通性就好,顺便把除改成减

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
    return x*f;
}
int N,M;
struct data{
    int x,y,v;
}e[5001];

bool cmp(data a,data b){
    return a.v<b.v;
}
int fa[5001];
void init(){
    for(int i=1;i<=N;i++) fa[i]=i;
    return ;
}
int find(int x){
    if(x!=fa[x]) return fa[x]=find(fa[x]);
    return x;
}
int ans1,ans2;

int main(){
    while(1){
        N=read(),M=read();
        if(!(N+M)) break;
        if(M==0){puts("-1");continue;}
        for(int i=1;i<=M;i++){
            e[i].x=read(),e[i].y=read();
            e[i].v=read();
        }
        ans1=ans2=-1;
        bool alflag=false;
        sort(e+1,e+M+1,cmp);
        for(int i=1;i<=M;i++){
            init();
            for(int j=i;j<=M;j++){
                int u=find(e[j].x),v=find(e[j].y);fa[v]=u;
                if(ans1!=-1&&e[j].v-e[i].v>=ans2-ans1) break;
                bool flag=false;
                for(int k=1;k<=N;k++) if(find(k)!=find(1)) {flag=true;break;}
                if(!flag){ans1=e[i].v;ans2=e[j].v;break;}
            }
            if(ans1==-1){puts("-1");alflag=true;break;}
        }
        if(!alflag)printf("%d\n",ans2-ans1);
    }
}

  

时间: 2024-11-08 21:57:47

【Kruskal】Slim Span的相关文章

【poj3522】 Slim Span

http://poj.org/problem?id=3522 (题目链接) 题意 求最小生成树的最大边与最小边差最小. Solution 排序后滑动窗口维护 代码 // poj3522 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL long lo

【UVA】1395-Slim Span

数学实在练不下去了,只能来水几个图论了,真想像D神一样来句:这道题很简单,直接AC就可以了. 大体思路:按照边的权值排序,枚举区间,利用并查集判断是否构成通路. 14042663 1395 Slim Span Accepted C++ 0.265 2014-08-15 02:11:53 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<v

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

【UVA 1395】 Slim Span (苗条树)

[题意] 求一颗生成树,满足最大边和最小边之差最小 InputThe input consists of multiple datasets, followed by a line containing two zeros separated by a space.Each dataset has the following format.n ma1 b1 w1...am bm wmEvery input item in a dataset is a non-negative integer.

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

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4141 题意:给出n个顶点,m条边,求一个生成树,使得最大边与最小边的差值最小. 思路:求一个生成树使最大边最小是瓶颈生成树.对于此题,我们枚举每一条边做最小边的情况,找对应的最小生成树的最大边,找出最大边与最小边差值最小的值即可. #include<cstring> #in

POJ2395 Out of Hay【Kruskal】

Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11656Accepted: 4562 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situat

HDU2122 Ice_cream’s world III【Kruskal】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 997    Accepted Submission(s): 321 Problem Description ice_cream's world becomes stronger and stronger; every road is built

POJ1679 The Unique MST【Kruskal】【次小生成树】

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21304 Accepted: 7537 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ