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) ,第 ...
随机推荐
- PHP 浮点型运算相关问题
php 浮点数计算比较及取整不准确.举例: $a = 0.2+0.7; $b = 0.9; var_dump($a == $b); //输出的结果为bool(false) PHP 官方手册说明:显然简 ...
- un-资源-开源-WebGallery:Windows Web App Gallery
ylbtech-资源-开源-WebGallery:Windows Web App Gallery Windows Web App Gallery 1.返回顶部 2.返回顶部 3.返回顶部 ...
- Python设计模式之MVC模式
# -*- coding: utf-8 -*- # author:baoshan quotes = ('A man is not complete until he is married. Then ...
- Python3基础 二、八、十、十六进制转换
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- Python3基础 内置函数 hash
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- opencv图片旋转90度
#include<iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv ...
- 我非要捅穿这 Neutron(二)上层资源模型篇
目录 文章目录 目录 Neutron 的资源模型 Network 运营商网络和租户网络 创建运营商网络 创建租户网络 创建外部网络 Network 小结 Subnet IP 核心网络服务 Subnet ...
- 安卓 android studio 报错 All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译.于是我把我的gradle升级到了 gradle-4.1-milestone-1 版本,是 ...
- django中同通过getlist() 接收页面form的post数组
前端中的一些东西: <form action="people?action=edit" method="post"> <input type= ...
- 改进初学者的PID-积分饱和
最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...