zoukankan      html  css  js  c++  java
  • 牛客网小白月赛6H(最小生成树【克鲁斯卡尔】)

    #include<bits/stdc++.h>
    using namespace std;
    long long sum=0;
    struct node
    {
        int a,b,len;
    }road[500010];
    int pre[500010];
    bool cmp(node a,node b)
    {
        return a.len<b.len;
    }
    int fin(int x)
    {
        if(x!=pre[x])
        {
            pre[x]=fin(pre[x]);
        }
        return pre[x];
    }
    void connect(int x,int y,int n)
    {
        int fx=fin(x),fy=fin(y);
        if(fx!=fy)
        {
            pre[fx]=fy;
            sum+=road[n].len;
        }
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&road[i].a,&road[i].b,&road[i].len);
        }
        for(int i=1;i<=m;i++)
            pre[i]=i;
        sort(road+1,road+1+m,cmp);
        for(int i=1;i<=m;i++)
        {
            connect(road[i].a,road[i].b,i);
        }
        printf("%lld",sum);
        return 0;
    }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    用php爬取网页
    无论我是一只菜鸟笨鸟
    有线网卡与无线网卡同时使用
    scapy 命令理解
    Wireshark Filter
    python OS/pdb 模块及数据类型基础
    scapy down and install
    python 字符操作函数
    python 类型集
    python 科学计算
  • 原文地址:https://www.cnblogs.com/ldudxy/p/9502524.html
Copyright © 2011-2022 走看看