zoukankan      html  css  js  c++  java
  • [生成树][Uva1395][Slim Span]

    代码:

    #include <set>
    #include <queue> 
    #include <cmath> 
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream> 
    #include <algorithm>
    #include <map>
    using namespace std;
    struct node
    {
        int s,t,w;
    }A[10000];
    int N,M;
    int father[200];
    int tot=0;
    void init()
    {
        for(int i=0;i<200;i++)
        father[i]=i;
        tot=0;
    }
    int find(int x)
    {
        if(x!=father[x])
        father[x]=find(father[x]);
        return father[x];
    }
    bool  cmp(node a,node b)
    {
        return a.w<b.w;
    }
    void input()
    {
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&A[i].s,&A[i].t,&A[i].w); 
        }
        sort(A+1,A+M+1,cmp);
    }
    void solve()
    {
        int ans=0x3f3f3f3f;
        for(int L=1;L<=M;L++)
        {
            init();
            for(int R=L;R<=M;R++)
            {
                int xx=find(A[R].s),yy=find(A[R].t);
                if(xx!=yy)
                {
                    tot++;
                    father[xx]=yy;
                } 
                if(tot==N-1)
                {
                    ans=min(ans,A[R].w-A[L].w);
                    break;
                }
            }
        }
        if(ans==0x3f3f3f3f) printf("%d
    ",-1);
        else printf("%d
    ",ans); 
    }
    int main()
    {
    //  freopen("a.in","r",stdin);
        while(cin>>N>>M&&(N||M))
        {
            input(); 
            solve();
        }
    }
  • 相关阅读:
    Java基础——方法
    JavaScript-JSON解析
    JavaScript—事件
    Window 浏览器窗口对象
    JavaScript 事件
    JavaEE——css字体样式效果
    JavaEE——CSS字体样式
    JavaEE——CSS3选择器
    JavaEE——css边框样式
    JavaEE——XML简介
  • 原文地址:https://www.cnblogs.com/zy691357966/p/5480310.html
Copyright © 2011-2022 走看看