(KEI) 2183. Goat road (Road) [Kruskal minimum spanning tree & disjoint-set] +

(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 N N buildings, and M M pieces Optional road. These roads will be guaranteed N N 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 100 100 plants, can be produced 100 90 100、90 marble length and the like, but can not produce a length of 101 101 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 N N and M N M,N represents the number of buildings in the village sheep, M is the number of road can be built.
Next M M lines, each line three integers A i B i Ai, Bi sum C i there , expressed from building A i Ai , the building B i With a , can build a length C i there road.
Note that numbers from building 1 1 to N N , 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
30 30 % of the data N < = 10 N 1 < = M < = 100 N<=10,N-1<=M<=100
100 100 % of the data 1 < = N < = 2000 N 1 < = M < = 10000 1 < = A i B i < = N 1<=N<=2000,N-1<=M<=10000,1<=Ai,Bi<=N 1 < = C i < = 1000000000 1<=Ci<=1000000000


Tip
As long as the construction of 1 1 to 2 2 , and 1 1 to 3 3 road, it can make 3 3 th building communicate with each other, and only maximum 100 100 , only the scale of construction design 100 100 marble plant, can be produced in length 100 100 and 99 99 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);
}

Published 119 original articles · won praise 8 · views 4899

Guess you like

Origin blog.csdn.net/kejin2019/article/details/105162722