p1225 调查干草

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/84893527

题目

描述 Description
奶牛用完了干草,这是一件必须马上补救的可怕事情. Bessie打算看N (2 <= N <=2,000)个农场(标号为1…N)去调查干草的情况.她将走过一些或全部M (1 <= M <=10,000)条连接着农场长度都不大于1,000,000,000的双向通路.一些农场可能被多条不同通路连接.

Bessie尝试决定她需要的水瓶的大小.她知道她在路上每走一个单位长度需要1个单位的水.因为她可以在每个农场得到更多的水,她只关心最长那条路的长度.她打算把她必须带的水减到最少.

帮Bessie计算她必须带的最多的水:即Bessie 走过最长的路最短,输出这个最小的数.
输入格式 Input Format
*第1行:两个整数用空格隔开:N和M
*第2…M+1行:每一行包括3个数Ai,Bi,Li,表示从Ai农场到Bi农场有条路径长度为Li.
输出格式 Output Format
*第一行:一个整数表示必须经过的最长的路.
样例输入 Sample Input

3 3
1 2 23
2 3 1000
1 3 43

样例输出 Sample Output

43
时间限制 Time Limitation
1s
来源 Source
usaco 月赛 outofhay

代码

#include<bits/stdc++.h>
using namespace std;
struct rec
{
	int x, y, z;
}edge[500010];
int n, m, ans, fa[100010];
inline int read()
{
	int f=1,num=0;
	char ch=getchar();
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while (ch>='0'&&ch<='9') { num=(num<<1)+(num<<3)+ch-'0'; ch=getchar(); }
	return num*f;
}
bool operator<(rec a, rec b)
{
	return a.z < b.z;
}
int get(int x)
{
	if (x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}
int main()
{
	n=read(),m=read();
	for (int i = 1; i <= m; i++)
		edge[i].x = read(), edge[i].y = read(), edge[i].z = read();
	sort (edge+1,edge+m+1);
	for (int i = 1; i <= n; i++)
		fa[i] = i;
	for (int i = 1; i <= m; i++)
	{
		int x = get(edge[i].x);
		int y = get(edge[i].y);
		if (x == y) continue;
		fa[x] = y;
		if (ans < edge[i].z) ans = edge[i].z;
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/84893527