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) ,第 ...
随机推荐
- java 读取CSV数据并写入txt文本
java 读取CSV数据并写入txt文本 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import ja ...
- 虚拟机设置静态ip【实测中标麒麟】
1.打开本机的网络共享中心,记住本机的网络描述 realtek RTL...... 2.右键当前虚拟机,设置桥接模式 3.在虚拟机上方选择编辑->虚拟机网络编辑器,桥接到第一步中的描述 4.修改 ...
- 面向对象(实际就像python跳用自己写的库那样)
被调用的对象(库) FanFa.java 文件 package com.BM; public class FanFa { #变量值 int r=4 #方法 public static void uui ...
- jvm 虚拟机字节码指令表(转)
- 测试框架nunit之assertion断言使用详解
任何xUnit工具都使用断言进行条件的判断,NUnit自然也不例外,与其它的xUnit(如Junit.phpUnit.pythonUnit)相比,由于大量使用了Generic.Attribute等语言 ...
- Difference between Process and thread?
What are the differences between a process and a thread? How are they similar? How can 2 threads com ...
- AI - AutoKeras - 简介
前言 在数据集上训练神经网络时,主要有两个目标: 定义符合数据集特性的神经网络架构. 在许多试验中对一组超参数进行调优,从而使得模型具有较高的准确率并且能够泛化至训练集和测试集之外的数据. 针对不同的 ...
- centos7下使用yum安装mysql5.7.10
原文地址:http://www.mamicode.com/info-detail-503994.html CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql ...
- cordon、drain、delete node区别
cordon.drain.delete node区别 主要目的:导致node处于不可调度状态,新创建的pod容器不会调度在node上. cordon将node置为SchedulingDisabled不 ...
- C++ String 及其与char[]的比较
在学习C++之前 一般都是学过了C语言了 在C语言中 我们对字符串进行保存操作 使用的是char[] 但是在C++中 string比char[]的使用更为频繁 常见 下面稍微讲一 ...