#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
int s, e;
int w;
}s[100005];
int c[105];
bool cmp(struct node a, struct node b)
{
return a.w < b.w;
}
int fin(int x)
{
int r = x;
while(r != c[r]) r = c[r];
int i = x, j;
while(c[i] != r)
{
j = c[i];
c[i] =r;
i = j;
}
return r;
}
int merge(int x, int y)
{
if(fin(x) != fin(y))
{
c[fin(x)] = fin(y);
return 1;
}
return 0;
}
int main()
{ int n,m,a,b,w;
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = 0; i < m; i ++)
{
scanf("%d%d%d",&a,&b,&w);
s[i].s = a;
s[i].e = b;
s[i].w = w;
}
sort(s,s+m,cmp);
int ans = 0, cnt = 0;
for(int i = 0; i <= n; i ++)c[i] = i;
for(int i = 0; i < m; i ++)
{
if(merge(s[i].s,s[i].e)== 1)
{
ans += s[i].w;
cnt ++;
}
if(cnt == n - 1) break;
}
printf("%d\n",ans);
}
return 0;
}

数据结构实验之图论九:最小生成树 (SDUT 2144)的更多相关文章

  1. SDUT OJ 数据结构实验之图论六:村村通公路(最小生成树)

    数据结构实验之图论六:村村通公路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  2. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  3. SDUT OJ 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  4. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  5. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  6. SDUT OJ 数据结构实验之链表九:双向链表

    数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  7. SDUT 3363 数据结构实验之图论七:驴友计划

    数据结构实验之图论七:驴友计划 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 做为一个资深 ...

  8. SDUT 3364 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...

  9. SDUT 3362 数据结构实验之图论六:村村通公路

    数据结构实验之图论六:村村通公路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 当前农村公 ...

随机推荐

  1. C++开源库大全

        标准库 C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Template Library:标准模板库 ...

  2. 五、eureka客户端自动配置

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 前面的几篇文章中,我们从eureka Server端的角度看了看eureka的几个核心要 ...

  3. window事件

    window事件是较为重要的事件,接下来就讲解一下. 1.获取页面滚动栏的距离 什么是滚动栏,就是网页内容过多时,通过滚轮控制上下显示或者左右显示: 为窗口添加滚动条事件: window.onscro ...

  4. MySQL 启动、登录、退出和目录结构

    一.启动 MySQL 服务器启动方式有两种: (1)通过服务的方式自动启动 (2)手动启动的方式 1.windows 服务方式启动 操作步骤: 也可以在 cmd 窗口 输入 services.msc ...

  5. 安卓开发之cache 的使用(图片查看器案例)

    package com.lidaochen.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; i ...

  6. bootstrap table分页limit计算pageIndex和pageSize

    由于bootstrap table的js无法直接获取pageSize和pageIndex的值,只能通过limit进行计算.

  7. 1.JUC锁的一些概念

    原文链接:http://blog.csdn.net/zteny/article/details/54859228 接下来的几篇文章都转自这里,冒犯之处,多多包涵 原子性: 在看原子性之前,我们先看看什 ...

  8. DHCP服务——服务端 和 客户端 配置

    转载注明出处:https://www.cnblogs.com/kelamoyujuzhen/p/9520341.html  实验环境 rhel-server-6.4-x86_64-dvd(ED2000 ...

  9. es6中的Object.assign

    在写一些插件的时候,我们会经常遇到所传参数需要合并默认参数,并覆盖相同参数的情况,在jQuery中我们可以使用$.extend(),在原生中要想使用得自己封装, 但自从es6出现了Object.ass ...

  10. JSON 的简介与使用

    一.什么是JSON? JSON(javascript object notation)全称是 javascript 对象表示法,是一种数据交换的文本格式,用于读取结构化数据,提出目的是取代繁琐笨重的 ...