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)的更多相关文章

  1. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  2. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  3. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  4. 最小生成树 kruskal算法 codevs 1638 修复公路

    1638 修复公路  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description A地区在地震过后,连接所有村庄的公 ...

  5. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  6. 贪心算法-最小生成树Kruskal算法和Prim算法

    Kruskal算法: 不断地选择未被选中的边中权重最轻且不会形成环的一条. 简单的理解: 不停地循环,每一次都寻找两个顶点,这两个顶点不在同一个真子集里,且边上的权值最小. 把找到的这两个顶点联合起来 ...

  7. 最小生成树---Kruskal/Prime算法

    1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图.      方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...

  8. 【BZOJ-2177】曼哈顿最小生成树 Kruskal + 树状数组

    2177: 曼哈顿最小生成树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 190  Solved: 77[Submit][Status][Discu ...

  9. 最小生成树Kruskal算法(邻接矩阵和邻接表)

    最小生成树,克鲁斯卡尔算法. 算法简述: 将每个顶点看成一个图. 在所有图中找权值最小的边.将这条边的两个图连成一个图, 重复上一步.直到只剩一个图. 注:将abcdef每个顶点看成一个图.将最小权值 ...

随机推荐

  1. eclipse中实现文本换行

    Eclipse 使用系统内置的“ Text Editor ”做为文本编辑器,这个文本编辑器有一个问题,就是文本无法换行. 扩展插件 WordWrap 可以实现文本换行         安装方法:    ...

  2. JQuery 更改属性 JQ对象循环 each 全选反选 三元运算

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue2.0 学习 ,开始学习

    先看官网的介绍上面的教程   https://cn.vuejs.org/v2/guide/ 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.你可以在浏览器新标签 ...

  4. Redirect local emails to a remote email account

    My previous post is a guide for setting up exim4, an SMTP mail server, to use Gmail as a smarthost. ...

  5. 七、angularjs 倒计时

    使用定时器时离开页面需要清除定时器,清除的方法有两种分别针对页面有缓存和没有缓存 1.页面有缓存 2.页面没有缓存 angularjs倒计时首先需要注入:$interval 60s倒计时 vm.sec ...

  6. Jmeter与LoadRunner的异同

    1.jmeter的架构跟loadrunner原理一样,都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务器反馈的结果的一个过程. 2.分布式中间代 ...

  7. 用Appium让Android功能自动化测试飞起来

    前言 做Android端功能自动化已有2年多的时间了,使用过的功能自动化框架有Robotium.Uiautomator.Appium.最近研究自动化case复用的方案,调研了Appium的自动化框架, ...

  8. centos ntfs-3g 安装和使用

    安装fuse 下载fuse(ntfs-3g依赖fuse):http://vdisk.weibo.com/s/ajww5fZsUq50L?from=page_100505_profile&wvr ...

  9. C#多线程顺序依赖执行控制

    在开发过程中,经常需要多个任务并行的执行的场景,同时任务之间又需要先后依赖的关系.针对这样的处理逻辑,通常会采用多线程的程序模型来实现. 比如A.B.C三个线程,A和B需要同时启动,并行处理,且B需要 ...

  10. MVC框架以及实例

    MVC框架 MVC(model,view,controller),一种将业务逻辑.数据.界面分离的方法组织代码的框架.在改进界面及用户交互的同时,不需要重写业务逻辑.MVC将传统的输入.处理和输出分离 ...