题目在这

Sample Input

4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1

Sample Output

1
4
1 2
1 3
2 3
3 4

题目意思:4个点,6个边,每个边有对应的权值。最后输出一行为路径中最大的边的值,第二行为路径上边的总数,

第三行为每条边的始末编号。题目需要求出最小生成树的最大边的最小值。

 /*
Problem: 1861 User:
Memory: 416K Time: 500MS
Language: C++ Result: Accepted
*/
#include <iostream>
#include <algorithm>
using namespace std; #define MAX 15010
int p[];//存放父亲结点 struct Edge
{
int u;
int v;
int w;
}map[MAX],ans[MAX]; bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int Find(int a)
{
return a==p[a]?a:a=Find(p[a]);
} int main()
{
int N,M,i;
int a,b,c;
cin>>N>>M;
for(i=;i<=N;i++)
{
p[i] = i;
}
for(i=;i<M;i++)
{
cin>>a>>b>>c;
map[i].u = a;
map[i].v = b;
map[i].w = c;
}
sort(map,map+M,cmp);
int count = ;
int maxEdge = ;
for(i=;i<M;i++){
int x = Find(map[i].u);
int y = Find(map[i].v);
if(x != y)
{
p[x] = y;//不在一个集合,合并
ans[count].u = map[i].u;
ans[count].v = map[i].v;
count ++;
if(map[i].w>maxEdge)
maxEdge = map[i].w;
}
}
cout<<maxEdge<<endl;//路径中最长的边
cout<<count<<endl;//边的总数
for(i=;i<count;i++)
cout<<ans[i].u<<" "<<ans[i].v<<endl;/输出每条路径
return ;
}

最新文章

  1. python enumerate用法
  2. .NET生成带Logo的二维码
  3. IntelliJ IDEA 修改包名
  4. 转:JS日期加减,日期运算
  5. Linux 使用yum install安装mysql登陆不上解决办法
  6. CSDN总结的面试中的十大算法
  7. IDX爱定客 | 氪加
  8. Ubuntu iptalbes 保存配置
  9. 剑指offer第六天
  10. Cocos2D iOS之旅:如何写一个敲地鼠游戏(四):创建TexturePacker自动脚本
  11. Hadoop系列002-从Hadoop框架讨论大数据生态
  12. javascript的数组之push()
  13. PHPExcel防止大数以科学计数法显示
  14. 今天碰到一个问题,怎么限制用户在固定宽度的input输入框里输入的长度,由此涉猎到了maxlength属性和size属性以及它们的区别。
  15. TensorFlow与caffe中卷积层feature map大小计算
  16. 多层josn数据 修改
  17. zabbix3.0.4-agent通过shell脚本获取mysql数据库登陆用户
  18. python练习笔记——丑数的计算
  19. C#之工厂
  20. QTableWidget与QTableView的区别

热门文章

  1. 从0开始学爬虫12之使用requests库基本认证
  2. Objective-C轻量级泛型
  3. 一、搭建简单的axis web服务
  4. ROS学习笔记(一)
  5. 静态站点生成器-md-pelican
  6. 【error】OutOfRangeError (see above for traceback): RandomShuffleQueue
  7. 安装rpy2 报错&lt;cdef source string&gt;:23:5: before: blah1 解决办法就是直接下载一个rpy2的轮子
  8. 《C语言程序设计》课程教学目录
  9. samtools获取uniq reads
  10. miniconda3 安装tensorflow