UVALive - 2515 (最小生成树 kruskal)
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal. Input The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second thenumber R ofgivenroutesbetweenthepoints. Thefollowing R linesdefinethegivenroutesbetween the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P = 0 denotes the end of the input. The data sets are separated with an empty line. The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as ‘i j’ or as ‘j i’. Output For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network. Sample Input 1 0
2 3 1 2 37 2 1 17 1 2 68
3 7 1 2 19 2 3 11 3 1 7 1 3 5 2 3 89 3 1 91 1 2 32
5 7 1 2 5 2 3 7 2 4 8 4 5 11 3 5 10 1 5 6 4 2 12
0 Sample Output 0 17 16 26
分析:
最小生成树模板题
克鲁斯卡尔算法
code:
/*#include <iostream>
#include <cstdio>*/
#include<stdio.h>
#include<algorithm>
/*#include<cstring>
#include<math.h>
#include<memory>*/
using namespace std;
typedef long long LL;
#define max_v 55
struct edge
{
int x,y;
int w;
};
edge e[];
int rk[max_v];
int pa[max_v];
int sum;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void make_set(int x)
{
pa[x]=x;
rk[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y,int w)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])
pa[y]=x;
else
{
if(rk[x]==rk[y])
rk[y]++;
pa[x]=y;
}
sum+=w;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
if(n==)
break;
scanf("%d",&m);
sum=;
for(int i=;i<=n;i++)
make_set(i);
for(int i=;i<m;i++)
{
scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].w);
}
sort(e,e+m,cmp);
for(int i=;i<m;i++)
{
union_set(e[i].x,e[i].y,e[i].w);
}
printf("%d\n",sum);
}
return ;
}
UVALive - 2515 (最小生成树 kruskal)的更多相关文章
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- 最小生成树 kruskal算法 codevs 1638 修复公路
1638 修复公路 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description A地区在地震过后,连接所有村庄的公 ...
- 最小生成树——kruskal算法
kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...
- 贪心算法-最小生成树Kruskal算法和Prim算法
Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...
- 最小生成树---Kruskal/Prime算法
1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图. 方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...
- 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组
2177: 曼哈顿最小生成树 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 190 Solved: 77[Submit][Status][Discu ...
- 最小生成树Kruskal算法(邻接矩阵和邻接表)
最小生成树,克鲁斯卡尔算法. 算法简述: 将每个顶点看成一个图. 在所有图中找权值最小的边.将这条边的两个图连成一个图, 重复上一步.直到只剩一个图. 注:将abcdef每个顶点看成一个图.将最小权值 ...
随机推荐
- [转]Shared——回调函数是什么
本文内容转自知乎 作者:no.body 链接:https://www.zhihu.com/question/19801131/answer/27459821 回调函数(callback)是什么? 什么 ...
- Js实现简单的音频播放
现效果如下: 由于我这边不需要其他按钮,就没写 数据是由后台提供,在这做了个小列子 后台代码 ) { MusicPlayerModel model = new MusicPlayerModel(); ...
- BZOJ P1059 [ZJOI2007]矩阵游戏——solution
1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4604 Solved: 2211[Submit][Stat ...
- Ubuntu16.04 安装redis
1. 保证网络畅通,选定好下载工作路径,执行以下命令下载redis-3.2.6: sudo wget http://download.redis.io/releases/redis-3.2.6.tar ...
- SQL server查找指定表的所有索引
WITH tmp AS ( SELECT indexname = a.name , tablename = c.name , indexcolumns = d.name , a.indid FROM ...
- swiper动态改变滑动内容
假设当前显示的是1,往左滑动一个递减1,往右滑动一个递增1 body下面添加如下代码 <div class="swiper-container temp"> <d ...
- Android 编辑框插入表情图片
首先,把整理好的表情图片以及布局用到的一些图片导入到项目的res/drawable目录中. 然后,编辑res/layout目录下布局.xml文件,这里我把oschina客户端的布局代码贴上来,供大家参 ...
- python 待关注库
Python待关注库 GUI 图形 Tkinter/wxPython/PyGTK/PyQt/PySide Web框架 django/web2py/flask/bottle/tornadoweb/web ...
- Ddos 反射性防护 simple
加固NTP服务: 1.通过Iptables配置只允许信任的IP,访问本机的UDP的123端口,修改配置文件执行echo "disable monitor" >> /et ...
- POP3、SMTP端口(SSL、TSL)
POP3服务器地址: 110 995 支持SSLSMTP服务器地址: 25 465 或者 587 支持SSL(TSL) 465端口是SSL/TLS通讯协议的 ...