2020 winter vacation [gmoj2183] [road sheep grazing] [minimum spanning tree: Prim & Kruskal + check set]

Title description

After the inspection of the characteristic demonstration Yangcun, the inspection team felt that the roads in Yangcun needed to be rebuilt, and the broken roads would affect the safety of the young sheep in school.
The village chief organized a construction team, started to measure the distance, and planned the construction plan, and has obtained a number of feasible plans for building roads between buildings. There are N buildings and M optional roads. These roads ensure that N buildings can be connected.
In the final plan, Yangcun intends to build the world's most luxurious all-marble road. The road can pass in both directions and is integrated into one, with no gaps in the road. In order to meet this design requirement, you must build your own marble factory!
The difficulty of building a marble factory is that it must be designed to produce the largest length of marble according to its needs. The factory can produce marble of any length that does not exceed its design limits. For example, a factory with a design length of 100 can produce marbles with a length of 100, 90, etc., but cannot produce marbles with a length of 101.
Yangcun ’s budget is limited, I hope you can help plan a road repair plan, so that the design scale of the factory is as small as possible, and it can ensure that the marble it can produce can connect all the buildings in Yangcun. Find the minimum design size of the factory.

Input

The first two integers N and M in the first row, N represents the number of buildings in the village, M represents the number of roads that can be built.
In the next M lines, each line has three integers Ai, Bi, and Ci, indicating that from building Ai to building Bi, a road of length Ci can be constructed.
Note that building numbers range from 1 to N, and there may be multiple roads between the two buildings.

Output

The minimum design scale of the output marble factory.

Sample input

3 3
1 2 100
2 3 101
1 3 99

Sample output

100

Data range limitation

30% numerical setting N <= 10, N-1 <= M <= 100.
100% numerical value 1 <= N <= 2000, N-1 <= M <= 10000, 1 <= Ai, Bi <= N, 1 <= Ci <= 1000000000.

prompt

As long as the roads of 1 to 2 and 1 to 3 are built, the three buildings can be connected to each other, and the maximum value is only 100. Only a marble factory with a design scale of 100 can be built to produce marbles with lengths of 100 and 99. .

analysis

At the beginning I also understood the problem of seeking connectivity in order to delete edges.
In fact, it is the smallest spanning tree. You can use prim or kruskal + and check both methods. I have done both.
A bit of variation: change the cumulative path sum into the maximum value of the edge weight .

Code on

prim
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[2001][2001],dis[2001],f[2001],ans;
void prim()
{
	memset(dis,0x7f,sizeof(dis));
	dis[1]=0;
	for(int i=1;i<=n;i++)
	{
		int v=a[0][0],k=0;
		for(int j=1;j<=n;j++)
		{
			if(dis[j]<v&&!f[j])
			{
				v=dis[j];
				k=j;
			}
		}
		if(k==0) return;
		f[k]=1;
		ans=max(ans,v);
		for(int j=1;j<=n;j++)
		{
			if(a[k][j]<dis[j]&&!f[j])
			{
				dis[j]=a[k][j];
			}
		}
	}
}
int main()
{
	freopen("road.in","r",stdin);
	freopen("road.out","w",stdout);
	cin>>n>>m;
	memset(a,0x7f,sizeof(a));
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		a[x][y]=a[y][x]=min(a[x][y],z);
	}
	prim();
	cout<<ans;
	fclose(stdin);
	fclose(stdout);
	return 0;
 } 
kruskal
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,k,fa[2010],xx,yy,ans;
struct node
{
    int x,y,z;
}a[10010];
int cmp(node l,node 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);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>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;
        }
    }
	cout<<ans;
	fclose(stdin);
	fclose(stdout);
	return 0; 
}

———————————— End of the play ————————————

Published 110 original articles · 100 praises · 8006 views

Guess you like

Origin blog.csdn.net/dglyr/article/details/105185494