• POJ 1861 Network(最小生成树)



    题目链接:http://poj.org/problem?id=1861 

    求出最小生成树边的个数,最大边权,和所有边。 

    代码:

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int N=1001;
    int map[N],ans[N];
    int num,maxn,k,i;
    struct edge
    {
        int a;
        int b;
        int value;
    }p[N*N];
    int find(int x)
    {
        int r=x;
        while(r!=map[r])
        {
            r=map[r];
        }
        return r;
    }
    void merge(int x,int y)
    {
        int x1=find(x);
        int y1=find(y);
        if(x1!=y1)
        {
            if(p[i].value>maxn)
                maxn=p[i].value;
            ans[k++]=i;
            map[x1]=y1;
            num++;
        }
    }
    bool cmp(const struct edge &a,const struct edge &b)
    {
        return a.value<b.value;
    }
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            k=0;
            for(i=0;i<=n;i++)
                map[i]=i;
            for(i=0;i<m;i++)
            {
                scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].value);
            }
            num=maxn=0;
            sort(p,p+m,cmp);
            for(i=0;i<m && num<n;i++)
            {
                merge(p[i].a,p[i].b);
            }
            printf("%d\n",maxn);
            printf("%d\n",num);
            for(i=0;i<num;i++)
            {
                printf("%d %d\n",p[ans[i]].a,p[ans[i]].b);
            }
        }
        return 0;
    }

  • 相关阅读:
    CODE[VS] 3026 恶心的扑克
    CODE[VS] 2951 打字比赛
    CODE[VS] 2774 火烧赤壁
    CODE[VS] 1860 最大数 1998年NOIP全国联赛提高组
    matplotlib学习笔记(一)
    LUNA16数据集(二)肺结节可视化
    [转载]pytorch自定义数据集
    [转载]什么情况下应该设置 cudnn.benchmark = True?
    pytorch构建优化器
    pytorch批训练数据构造
  • 原文地址:https://www.cnblogs.com/pony1993/p/2644567.html
Copyright © 2020-2023  润新知