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<cstdio>
#include<algorithm>
using namespace std;
int sum,n,m,i,fa[];
int find(int a)
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
return r;
}
struct stu
{
int a,b,c;
}st[];
bool cmp(stu a,stu b)
{
return a.c>b.c;
}
void f1(int x,int y)
{
int nx,ny;
nx=find(x);
ny=find(y);
if(nx != ny)
{
fa[nx]=ny;
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
sum=;
for(i = ; i <= n ;i++)
{
fa[i]=i;
}
for(i = ; i < m ; i++)
{
scanf("%d %d %d",&st[i].a,&st[i].b,&st[i].c);
}
sort(st,st+m,cmp); //对最远路径排序
for(i = ; i < m ; i++)
{
if(find(st[i].a) != find(st[i].b)) //判断有没有环
{
f1(st[i].a,st[i].b);
sum+=st[i].c;
}
}
int ans=;
for(i = ; i <= n ; i++)
{
if(fa[i] == i) //判断有没有独立点
{
ans++;
}
}
if(ans > )
printf("-1\n");
else
printf("%d\n",sum);
}
}

POJ 2377 (并查集+sort求最远路)的更多相关文章

  1. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  2. Network POJ - 3694(lca并查集+连通图求桥)

    就是求出原先图中的桥的数量,在每一次询问时加入一条新边,求加入当前边后图中剩余的桥的数量 求出原先图中的桥的数量,然后减去新加入边的两端点之间的桥的数量,就是剩余桥的数量.. 用并查集把属于同一集合的 ...

  3. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  4. POJ 3657 并查集

    题意: 思路: 1.二分+线段树(但是会TLE 本地测没有任何问题,但是POJ上就是会挂--) 2.二分+并查集 我搞了一下午+一晚上才搞出来----..(多半时间是在查错) 首先 如果我们想知道这头 ...

  5. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  6. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  7. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  8. POJ - 1417 并查集+背包

    思路:很简单的种类并查集,利用并查集可以将所有的人分成几个集合,每个集合又分为好人和坏人集合,直接进行背包dp判断有多少种方法可以在取了所有集合并且人数正好凑足p1个好人的方案.dp(i, j)表示前 ...

  9. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

随机推荐

  1. Git如何克隆Gitlab?Git本地仓库如何上传Gitlab?

    首先确保本机已经安装上Git,其次确认可以正常访问Gitlab服务器 环境: Git:Centos 7.x  192.168.126.138 Gitlab: Centos7.x 192.168.126 ...

  2. UltraEdit的免费激活方法

    本来前段时间用UE编辑器用的好好的,然后今天突然提示我使用到期,需要购买激活.一脸懵逼中,只好再次激活,谁知道按照原来的方法激活的时候一直提示您输入的许可证id或密码错误 请您检查注册邮件并且重试. ...

  3. python之使用request模块发送post和get请求

    import requestsimport json #发送get请求并得到结果# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马 ...

  4. scikit-learning教程(四)选择合适的估计量

    选择正确的估计 解决机器学习问题的最困难的部分通常是找到合适的工作量. 不同的估计器更适合于不同类型的数据和不同的问题. 下面的流程图旨在给用户一些关于如何处理关于哪些估计器尝试您的数据的问题的粗略指 ...

  5. 525 Contiguous Array 连续数组

    给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...

  6. DoTween学习笔记

    using DG.Tweening:   Tweener 首先dotween在游戏刚开始运行时会默认进行一次初始化 DOTween.Init(); 如果为了有更好的效率,可以手动控制最大同时进行dot ...

  7. P1936 水晶灯火灵

    题目描述 ,刚好符合以下③条规则:①m.n∈{1,2,……,k}②(n²-m*n-m²)²=1③m.n为整数. 输入输出格式 输入格式: Only one:k.(What?还是失败?) 输出格式: 共 ...

  8. RabbitMQ九:远程过程调用RPC

    定义 RPC(Remote Procedure Call Protocol)——远程过程调用协议:它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...

  9. Java GC机制简要总结(Java垃圾回收的基本工作原理)

    第一次编辑 2019-05-07 01:09:39 垃圾回收的对象 程序中的不可用对象(不存活的对象,没有任何引用),或者无用的变量信息等,在程序中长期存在会逐渐占用较多的内存空间,导致没有足够的空间 ...

  10. 边框圆角值的问题、white-space、word-wrap、margin对布局的影响

    1.边框圆角(border-radius)值的问题 border-radius : 7px 7px 7px 0; 四个值的顺序是左上.右上.右下.左下 2.white-space 规定段落中的文本不换 ...