/*********
Gym 100712F
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
2
6 7
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
Sample Output
4
2
题目大意就是一个人要周游所有城市但他所驾驶的汽车的油箱容量有限,告诉你走这些城市之间的道路的所需要的油量,求可以使这个人可以到达所有城市的最小容量,也就是从任何一个城市可以到达另一个城市。
由于每条路的C值最大是100000,所以二分只需要不到20次。所以思路就是二分时用并查集判断整个图是否连通。这道题还有另一种思路,就是dijkstra算法,同样十分简单,不再赘述。
Gym 100712F
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
2
6 7
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
Sample Output
4
2
题目大意就是一个人要周游所有城市但他所驾驶的汽车的油箱容量有限,告诉你走这些城市之间的道路的所需要的油量,求可以使这个人可以到达所有城市的最小容量,也就是从任何一个城市可以到达另一个城市。
由于每条路的C值最大是100000,所以二分只需要不到20次。所以思路就是二分时用并查集判断整个图是否连通。这道题还有另一种思路,就是dijkstra算法,同样十分简单,不再赘述。
*********/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+50;
int fa[MAXN],n,k;
struct node
{
int a,b,v;
}road[MAXN];
int find(int n)
{
if(fa[n]==n)
return n;
else
return fa[n]=find(fa[n]);
}
void unite(int a,int b)
{
int x=find(a);
int y=find(b);
fa[x]=y;
}
int judge(int m)
{
for(int i=1;i<=n;i++)
{
fa[i]=i;
}
for(int i=0;i<k;i++)
{
if(road[i].v<=m)
{
unite(road[i].a,road[i].b);
}
}
int cnt=0;
for(int i=1;i<=n;i++)
{
if(fa[i]==i)
cnt++;
}
if(cnt==1)
return 1;
else
return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int l=1,r=1e5+50;
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++)
{
scanf("%d%d%d",&road[i].a,&road[i].b,&road[i].v);
}
int cnt=20;
while(cnt--)
{
int mid=(l+r)/2;
if(judge(mid))//符合情况则向下二分
r=mid;
else
l=mid+1;
}
printf("%d\n",l);
}
return 0;
}