(File IO): input: road.in output: road.out
time limit: 1000 ms space constraints: 131072 KB specific restrictions
Goto ProblemSet
Title Description
After the demonstration featured sheep village inspection, the inspection team felt the need to rebuild the road sheep village, dilapidated roads, will affect the lamb to school safety.
Organize village construction team began measuring distance, planning the construction program, it has been a viable option to build roads between several buildings, a total of
buildings, and
pieces Optional road. These roads will be guaranteed
is connected to a building.
The final program, the sheep village wants to build the world's most luxurious all-marble road, the road can be two-way traffic, and one piece, in a seamless way. To achieve this the design requirements, it is necessary to self-built marble factory!
Difficulty marble factory-built that their needs must be designed in accordance with the maximum length of the production of marble. Marble factory can produce any length does not exceed its design limits. For example, the design of length
plants, can be produced
marble length and the like, but can not produce a length of
marble.
Sheep village budget is limited, I hope you can help plan a road scheme, making the design size of the plant as small as possible, and you can guarantee that it can produce marble building can be connected to all of the village sheep. The value of minimum design size plant.
Enter
the first line two integers
and
represents the number of buildings in the village sheep, M is the number of road can be built.
Next
lines, each line three integers
sum
, expressed from building
, the building
, can build a length
road.
Note that numbers from building
to
, may be a number of roads between the two buildings.
Output
minimum design size marble factory output.
Sample input
. 3. 3
. 1 2 100
2 101. 3
. 1. 3 99
Sample output
100
Data range limit
% of the data
。
% of the data
,
。
Tip
As long as the construction of
to
, and
to
road, it can make
th building communicate with each other, and only maximum
, only the scale of construction design
marble plant, can be produced in length
and
marble.
Solving ideas
minimum spanning tree Kruskal hair, the accumulation path and selecting the maximum value becomes the right edge. (I used a disjoint-set)
Code
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,m,k,fa[2010],xx,yy,ans;
struct c{
int x,y,z;
}a[10010];
bool cmp(const c&l,const c&r)
{
return l.z<r.z;
}
int father(int h)
{
while(h!=fa[h])
h=fa[h];
return h;
}
int main(){
freopen("road.in","r",stdin);
freopen("road.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
sort(a+1,a+m+1,cmp);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
xx=father(a[i].x);
yy=father(a[i].y);
if(xx!=yy)
{
ans=max(ans,a[i].z);
fa[xx]=yy;
}
}
printf("%d",ans);
}