POJ1861(Network)-Kruskal
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 ;
}
POJ1861(Network)-Kruskal的更多相关文章
- POJ1861 Network(Kruskal)(并查集)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16047 Accepted: 6362 Spec ...
- POJ1861 Network (Kruskal算法 +并查集)
Network Description Andrew is working as system administrator and is planning to establish a new net ...
- poj1861 network(并查集+kruskal最小生成树
题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...
- ZOJ 1586 QS Network Kruskal求最小生成树
QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...
- POJ1861 Network
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %lld & %llu Description Andrew is ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- POJ-1287.Network(Kruskal + Prim + Prim堆优化)
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19674 Accepted: 10061 Desc ...
- [bzoj 3732] Network (Kruskal重构树)
kruskal重构树 Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1-N. 图中有M条边 (1 <= M <= 30,000) ,第 ...
随机推荐
- C#nameof用法
1.实现代码 using System; namespace NameofUsage { /// <summary> /// C# nameof用法 /// </summary> ...
- Flutter AspectRatio、Card 卡片组件
Flutter AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widg ...
- ISO/IEC 9899:2011 条款6.8.3——表达式与空语句
6.8.3 表达式与空语句 语法 1.expression-statement: expressionopt ; 语义 2.在一条表达式语句中的表达式被计算为一个void表达式作为其副作用.[注 ...
- 实战一:LoadRunner性能测试利器
转自:https://blog.csdn.net/weixin_42350428/article/details/82106603 企业的网络应用环境都必须支持大量用户,网络体系架构中含各类应用环境且 ...
- [Feature] Final pipeline: custom transformers
有视频:https://www.youtube.com/watch?v=BFaadIqWlAg 有代码:https://github.com/jem1031/pandas-pipelines-cust ...
- 【tshark tcpdump】linux网络排查
抓包: 1.tcpdump 2.tshark是wireshark的命令行版. tshark使用示例: ,实时打印当前http请求的url # tshark -s -i eth0 -n -f 'tcp ...
- 【Leetcode_easy】1029. Two City Scheduling
problem 1029. Two City Scheduling 参考 1. Leetcode_easy_1029. Two City Scheduling; 完
- Django_图片的上传下载显示配置
图片上传的配置 image = models.ImageField(upload_to='org/%Y/%m',...) upload_to默认是上传到项目的'MEDIA_ROOT/org/%Y/%m ...
- python虚拟环境的配置: virtualenv 和 virtualenvwrapper-win 的用法
版本:python37, virtualenv==16.7.8, virtualenvwrapper-win==1.2.5 pip37 install virtualenv 安装支持虚拟环境的包,注意 ...
- linux打印指定的行的内容
使用sed打印第99行 sed -n '99,p' test.txt 使用awk打印第99行 awk 'NR==99' test.txt awk 'FNR==99' test.txt perl 完成 ...