ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman
After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelling between different cities. He decided to buy a new car to help him in his job, but he has to decide about the capacity of the fuel tank. The new car consumes one liter of fuel for each kilometer.
Each city has at least one gas station where Bahosain can refill the tank, but there are no stations on the roads between cities.
Given the description of cities and the roads between them, find the minimum capacity for the fuel tank needed so that Bahosain can travel between any pair of cities in at least one way.
Input
The first line of input contains T (1 ≤ T ≤ 64) that represents the number of test cases.
The first line of each test case contains two integers: N (3 ≤ N ≤ 100,000) and M (N-1 ≤ M ≤ 100,000), where N is the number of cities, and M is the number of roads.
Each of the following M lines contains three integers: X Y C (1 ≤ X, Y ≤ N)(X ≠ Y)(1 ≤ C ≤ 100,000), where
C is the length in kilometers between city X and city Y. Roads can be used in both ways.
It is guaranteed that each pair of cities is connected by at most one road, and one can travel between any pair of cities using the given roads.
Output
For each test case, print a single line with the minimum needed capacity for the fuel tank.
|
Sample Input |
Sample Output |
||
|
2 |
4 |
||
|
6 |
7 |
2 |
|
|
1 |
2 |
3 |
|
|
2 |
3 |
3 |
|
|
3 |
1 |
5 |
|
|
3 |
4 |
4 |
|
|
4 |
5 |
4 |
|
|
4 |
6 |
3 |
|
|
6 |
5 |
5 |
|
|
3 |
3 |
||
|
1 |
2 |
1 |
|
|
2 |
3 |
2 |
|
|
3 |
1 |
3 |
|
/*
题意:
旅游者想走遍全世界,一共有N个城市,他需要买一辆车,但是他抠,想买便宜点的就是油箱最小的
每条路走过需要消耗 cost的油,找出最小的油箱需求。 这题正好是前几天刷的最小生成树,排序后,维护最小树的最大边就行,代码就不多加注释了。 AC代码:
*/ #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define MX 100000 + 50
using namespace std; int pe[MX];
struct node {
int u,v,cost;
} road[MX]; bool cmp(node a,node b) {
return a.cost<b.cost;
} int find(int x) {
return pe[x]==x?x:(pe[x]=find(pe[x]));
}
int main() {
int T,n,q,num,maxx;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&q);
for(int i=0; i<=n; i++) {
pe[i]=i;
}
num=n-1;
for(int i=0; i<q; i++) {
scanf("%d%d%d",&road[i].u,&road[i].v,&road[i].cost);
}
maxx=0;
sort(road,road+q,cmp);
for(int i=0; i<q; i++) {
int rt1=find(road[i].u);
int rt2=find(road[i].v);
if(rt1!=rt2) {
pe[rt2]=rt1;
maxx=max(maxx,road[i].cost);
num--;
}
if(!num)break;
}
printf("%d\n",maxx);
}
return 0;
}
ACM: 限时训练题解- Travelling Salesman-最小生成树的更多相关文章
- ACM: 限时训练题解-Rock-Paper-Scissors-前缀和
Rock-Paper-Scissors Rock-Paper-Scissors is a two-player game, where each player chooses one of Roc ...
- ACM: 限时训练题解-Runtime Error-二分查找
Runtime Error Bahosain was trying to solve this simple problem, but he got a Runtime Error on one ...
- ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举
Heavy Coins Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...
- ACM: 限时训练题解-Epic Professor-水题
Epic Professor Dr. Bahosain works as a professor of Computer Science at HU (Hadramout Universit ...
- ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】
Street Lamps Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)
题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
随机推荐
- CLR via C#(13)-浅谈事件
提起事件,我们都不陌生,事件使类之间有了交互的能力.它是建立在委托基础上的.有了前面对委托的了解,相信读起事件来也不会太难了.关于事件,现成的好文章数不胜数,本不打算写了.不过问道有先后,各抒己见,也 ...
- EasyUi–7.tab和datagrid和iframe的问题
1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> $(functi ...
- (编辑器)Jquery-EasyUI集合Kindeditor编辑器
1.在html里面添加 list.html list.html (function ($, K) { if (!K) throw "KindEditor未定义!"; functio ...
- WindowManagerPolicy的后缀 解释
转自:http://blog.csdn.net/hunanwy/article/details/8563090 Ti,called from the input thread. Input threa ...
- php建立多层目录的函数
/** *根据路径path建立多级目录 *$dir目标目录 $mode权限,0700表示最高权限 */ function makedir( $dir , $mode = "0700" ...
- VC++6.0使用OpenGL前的配置(必看)
要在VC++6.0中使用opengl,需要配置一下环境设置. 具体需要两步: 1.加入一个头文件,两个lib文件,两个dll文件,放在合适位置. 2.配置一下vc++6.0的Project Setti ...
- Platform SDK、Windows SDK简介
Platform SDK及Windows SDK是由微软公司出品的一个软件开发包,向在微软的Windows操作系统和.NET框架上开发软件和网站的程序员提供头文件.库文件.示例代码.开发文档和开发工具 ...
- nexus私有仓库搭建
步骤: 下载安装JDK(注意可用版本) .查看CentOS自带JDK是否已安装,输入: yum list installed |grep java 一般来说,如果是新装CentOS系统的话,不会有JD ...
- NS2中修改载波侦听范围和传输范围
修改这两个值是在tcl中进行的,加上 Phy/WirelessPhy set CSThresh_ 1.559e-11 ;#550m Phy/WirelessPhy set RXThresh_ 3.65 ...
- BZOJ 1503: [NOI2004]郁闷的出纳员 splay
1503: [NOI2004]郁闷的出纳员 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作 ...