这道题只是一道模板题,感到唯一的坑点就是n,m容易打错,一定要注意结构体要开到Max(M)+n; 之前便是因为这个地方Runtime Error了两次;顺便注意最后输出的答案 为long long型

Kruskal算法通过把所有的边从小到大排列后,不断取权值最小的边加入最小生成树(起初可能是离散的多个树,最终连成一个整体),并通过并查集来舍弃形成回路的边。

题目

**Description
Bessie has been hired to build a cheap internet network among Farmer John’s N (2 <= N <= 1,000) barns that are conveniently numbered 1…N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn’t even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a “tree”.
Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2…M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
    Output
  • Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
    Sample Input
    5 8
    1 2 3
    1 3 7
    2 3 10
    2 4 4
    2 5 8
    3 4 6
    3 5 2
    4 5 17
    Sample Output
    42
    Hint
    OUTPUT DETAILS:
    The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.**

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100+20000;
struct Node
{int a;int b;long long price;
}s[N];
int ran[N];
int uset[N];
bool cmp(struct Node a,struct Node b)  //按权值由大到小排序
{return a.price>b.price;
}
void makeset(int n)   //并查集初始化
{for(int i=1;i<=n;i++)uset[i]=i,ran[i]=0;
}
int find(int x)   // 循环实现路基压缩
{int r=x,t;while(r!=uset[r])r=uset[r];while(x!=r){t=uset[x];uset[x]=r;x=t;}return x;
}
/*int find(int x)
{if(x!=uset[x])uset[x]=find(uset[x]);return uset[x];
}*/    //递归实现路径压缩
void unionset(int x,int y)   //按秩合并
{int  a=find(x),b=find(y);if(a==b)return ;if(ran[a]>ran[b])uset[b]=a;else{if(ran[a]==ran[b])ran[b]++;uset[a]=b;}
}
long long Kruskal(int n,int m)  //Kruskal生成最小生成树
{int i,nedge=0; //nedge 记录边的数量 最大为谷仓总数减1 为n-1long long res=0;sort(s+1,s+1+m,cmp);for(i=1;i<=m&&nedge!=n-1;i++){if(find(s[i].a)!=find(s[i].b)){unionset(s[i].a,s[i].b);res+=s[i].price;nedge++;}}if(nedge<n-1)res=-1;return res;
}
int main()
{int n,m;while(cin>>n>>m){long long ans=0;for(int i=1;i<=m;i++)scanf("%d%d%lld",&s[i].a,&s[i].b,&s[i].price);makeset(n);ans=Kruskal(n,m);cout<<ans<<endl;}return 0;
}

(poj 2377)Kruskal算法 最大生成树相关推荐

  1. POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)

    题目 Language:Default Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36414 Accept ...

  2. * poj 1251 JungleRoad 最小生成树 Kruskal算法、Prim算法

    文章目录 Kruskal算法 模板:https://blog.csdn.net/Rain722/article/details/65642992 Prim算法 模板: poj 1251 JungleR ...

  3. 生成树的概念,最小生成树Prim算法 Kruskal算法

    求解最小生成树可以用Prim算法 Kruskal算法

  4. 最小生成树(prime算法、kruskal算法) 和 最短路径算法(floyd、dijkstra)

    带权图分为有向和无向,无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法:有向图的最短路径算法有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包 ...

  5. 数据结构与算法(7-3)最小生成树(普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法)

    目录 一.最小生成树简介 二.普里姆算法(Prim) 1.原理 2.存储 2-1.图顶点和权: 2-3. 最小生成树: 3.Prim()函数 3-1.新顶点入树 3-2.保留最小权 3-3. 找到最小 ...

  6. Kruskal算法 - C语言详解

    最小生成树 在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树.  例如,对于如上图G4所示的连通网可以有多棵权值总 ...

  7. Prim算法和Kruskal算法求最小生成树

    Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...

  8. 生成随机数放入整型数组怎么判断有没有重复_图的应用(1)-连通图的最小生成树(Prim算法和Kruskal算法)...

    连通图的生成树: 是一个极小的连通图,它含有图中全部的N个顶点,但是只足以构成一颗树的N-1条边. 必须满足三个条件: 图是连通图: 图中包含了N个结点 图中边的数量等于N-1条. 连通图生成树的判断 ...

  9. C++编程练习(10)----“图的最小生成树“(Prim算法、Kruskal算法)

    1.Prim 算法 以某顶点为起点,逐步找各顶点上最小权值的边来构建最小生成树. 2.Kruskal 算法 直接寻找最小权值的边来构建最小生成树. 比较: Kruskal 算法主要是针对边来展开,边数 ...

最新文章

  1. mybatis简化实现思路
  2. VC 6.0中添加库文件和头文件
  3. 双重检查锁模式导致空指针
  4. 蓝桥杯单片机基础学习00_1
  5. JS——阶乘的三种做法(正向逆向递归)
  6. spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
  7. 中班音乐活动计算机反思,中班音乐教学反思
  8. hadoop单击模式环境搭建
  9. 机器学习之集成学习概述
  10. Axure9元件库,如何自建,如何利用下载的元件库
  11. MMORPG大型游戏设计与开发(构架)
  12. iphone配置实用工具iPhone Configuration Utility
  13. jsp登录注册页面代码
  14. 2021-CSP-J2/S2 自我题解
  15. window清理系统垃圾文件代码
  16. 同工作组计算机连接用户名和密码错误,登录失败: 未知的用户名或错误密码
  17. 数学建模学习(100):交通运输问题建模
  18. t台式计算机如何安装2个硬盘,台式机械硬盘怎么安装?机械硬盘安装图解教程(SATA固态可参考)(2)...
  19. Java中的异常分类[乐乐独记]
  20. (适配方案总结)客户薅公司两台ipad,我还要给做适配?

热门文章

  1. java开学考试感想及代码
  2. 计算机打印错误,打印机错误正在打印处理方法,详细教您电脑打印机错误正在打印处理方法...
  3. apicloud总结
  4. java web 开发资料链接
  5. Leco题目:整数反转
  6. Android 基于google Zxing实现二维码 条形码扫描,仿微信二维码扫描效果
  7. 让割草类游戏更有趣的攻击动作设计技巧
  8. 没考驾照的恭喜了!上海打响第一枪!
  9. 自定义ListView实现任意View跑马灯效果
  10. 小迪-65-内网安全