zoukankan      html  css  js  c++  java
  • 【洛谷 2126】Mzc家中的男家丁

    题目背景

    mzc与djn的…还没有众人皆知,所以我们要来宣传一下。

    题目描述

    mzc家很有钱(开玩笑),他家有n个男家丁,现在mzc要将她们全都聚集起来(干什么就不知道了)。现在知道mzc与男家丁们互相之间通信的时间,请算出把他们每个人叫到需要的总时间(要重复的哦)。保证能把他们每个人叫到。

    输入格式

    第一行有一个数n,表示有n个男家丁。第二行一个数m表示有m条通信路线。之后m行,每行三个数a[i],b[i],c[i],表示第a[i]个男家丁(或mzc)和第b[i]个男家丁(或mzc)通信需要时间(双向)。ai=0表示mzc。

    输出格式

    一行,一个数sum,表示把他们每个人叫到需要的总时间。

    输入输出样例

    输入 #1
    5
    12
    0 2 15
    2 3 20
    3 5 13
    1 3 29
    0 1 30
    2 4 21
    0 3 23
    5 1 48
    0 4 17
    0 5 27
    1 2 43
    2 5 41
    
    输出 #1
    94
    

    说明/提示

    n<=2300

    m<=400000

    题解:被遗忘了的最小生成树。

    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    const int N=400005;
    int n,ans,m,biu;
    struct node{
        int uu,vv,w;
    }e[N];
    int dis[N],head[N],cnt,fa[N];
    bool vis[N];
    bool cmp(node a,node b) 
        { return a.w<b.w; }
    int find(int x){
        if(x!=fa[x]) fa[x]=find(fa[x]);
        return fa[x];
    }
    
    
    void init(){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++){
            scanf("%d %d %d",&e[i].uu,&e[i].vv,&e[i].w);
            e[i].uu++;  e[i].vv++;
        }
            
    }
    void Yao_Chen_Lai_Le(){
        int u,v;
        sort(e+1,e+m+1,cmp);
        for(int i=1;i<=m;i++){
            u=find(e[i].uu);
            v=find(e[i].vv);
            if(u==v) continue;
            ans+=e[i].w;
            fa[u]=v; biu++;
            //if(biu==(n-1)) break;
        }
    }
    int main(){
        freopen("2126.in","r",stdin);
        freopen("2126.out","w",stdout);
        init();
        Yao_Chen_Lai_Le();
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    机器学习 深度学习 计算机视觉 资料汇总
    激活层和pooling的作用
    NVIDIA GPU 计算能力
    TX2 刷机过程
    Anaconda tensorflow 安装笔记
    yolo-开源数据集coco kitti voc
    TX2上yolov3精度和速度优化方向
    yolo原理学习
    ubuntu常用命令
    tensorflow mnist模块详解
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11809276.html
Copyright © 2011-2022 走看看