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);
}
}

最新文章

  1. Java中从控制台输入数据的几种常用方法
  2. easyui datagrid 点击表头的事件
  3. 2.5 ListView
  4. 学习总结(annotation)
  5. 虚拟机备份转移后,网络启动异常,提示“SIOCSIFADDR: No such device”的解决方案
  6. Oracle的AUTOTRACE功能
  7. Java多线程初学者指南(5):join方法的使用
  8. Behavioral模式之Observer模式
  9. 配置Windows Server 2012服务器远程连接支持多人同时登陆
  10. 10分钟学会JAVA注解(annotation)
  11. [poj3349]Snowflake Snow Snowflakes_hash
  12. 13-TypeScript单例模式
  13. Linux:Day8(下) RAID
  14. Java多线程-详细版
  15. 【Java】 剑指offer(45) 把数组排成最小的数
  16. 2017第八届蓝桥杯C/C++ B组省赛-购物单
  17. Netty Tutorial Part 1: Introduction to Netty [z]
  18. centos 删除文件和目录
  19. WeakValue &amp; StoreValue
  20. [Erlang33]使用recon从网页查看Erlang运行状态

热门文章

  1. 使用pytesseract识别验证码,报错WindowsError: [Error 2]
  2. Hdu 5445 Food Problem (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online)
  3. HDU 4366 Successor 分块做法
  4. 如何正确从他人机器MySQL数据库下拷贝出.sql,再导入到自己windows下MySQL数据库(图文详解)
  5. JavaScript中函数是不能重载原因
  6. 【C#】枚举
  7. oracle 触发器,序列,索引
  8. 01认识Python和基础知识
  9. 在idea启动tomcat出现The JAVA_HOME environment variable is not defined correctly的解决
  10. xcode6的项目中虚拟键盘无法弹出