VJ 补提
F. 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 Nis the number of cities, and Mis the number of roads.
Each of the following Mlines contains three integers: X Y C (1 ≤ X, Y ≤ N)(X ≠ Y)(1 ≤ C ≤ 100,000), where
Cis the length in kilometers between city Xand 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
67
123
233
315
344
454
463
655
33
121
232
313
————————————————
4
2
解析:这个题是最小生成树的变式,题意是求能够通达各个城市的最小邮箱容量,城市可以加油,路上不能加油,就是求最小生成树最大边权值
代码如下:(Kruskal算法)
#include
#include <bits/stdc++.h>
using namespace std;
int fa[100100];
struct node
{
int u;
int v;
int w;
}e[100100];
int cmp(struct node a, struct node b)
{
return a.w < b.w;
}
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
if(x==fa[x])
return x;
return fa[x] = Find(fa[x]);
}
void unite(int x, int y)
{
x = Find(x), y = Find(y);
if(x!=y)
fa[x] = y;
}
int main()
{
int t;
int n, m, ans;
cin>>t;
while(t–)
{
ans = 0;
cin>>n>>m;
init(n);
for(int i=1;i<=m;i++)
{
cin>>e[i].u;
cin>>e[i].v;
cin>>e[i].w;
}
sort(e+1,e+1+m,cmp);
for(int i=1;i<=m;i++)
{
int u=e[i].u, v = e[i].v, w = e[i].w;
if(Find(u)!=Find(v))
{
ans = w;
unite(u, v);
}
}
cout<<ans<<endl;
}
return 0;
}