POJ2395Out of Hay(最小生成树Kruskal模板)

来源:互联网 发布:mac战网如何更改地区 编辑:程序博客网 时间:2024/05/29 17:47
Out of Hay
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16495 Accepted: 6460

Description

The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1. 

Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry. 

Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.

Input

* Line 1: Two space-separated integers, N and M. 

* Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.

Output

* Line 1: A single integer that is the length of the longest road required to be traversed.

Sample Input

3 31 2 232 3 10001 3 43

Sample Output

43

Hint

OUTPUT DETAILS: 

In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.

Source

USACO 2005 March Silver

题目大意:要求找最短的路径,使得能够通过所有的点,输出最短路径当中的最长的长度

解题思路:这是一道最小生成树的问题,一般最小生成树都是要求找到一个路径能够遍历所有的点,一般解题步骤由排序和并查集两部分组成

一般你需要将所有的路径根据权值进行一个排列,然后取最小的值路径长度,同时判断取出来的最小路径是否连通,一般判连通是根据并查集来寻找,判断是否存在于一个集合,以此判断是否这条道路是否能够当作最小生成树的一部分

并查集包括两部分,一部分判断是否在一个集合之中,一个是合并到一个集合中

特别要注意的是,再不断地取出来最小路径的时候,判一下是否符合条件的路的个数达到n-1,因为一棵树就是n-1个边

#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;struct lu{int x, y;long long int l;}l[10005];int n, m, check[2005], sum;long long int ans;void init()//初始化并查集{int i;for (i = 1; i <= n; i++){check[i] = i;}}bool cmp(lu x, lu y){return x.l < y.l;}int getf(int x){if (check[x] == x)return x;else{check[x] = getf(check[x]);return check[x];}}bool merge(int x, int y){int t1, t2;t1 = getf(x);t2 = getf(y);if (t1 != t2){check[t2] = t1;return true;}elsereturn false;}int main(){int i, x, y;cin >> n >> m;for (i = 1; i <= m; i++){cin >>x >>y >> l[i].l;l[i].x = min(x, y);l[i].y = max(x, y);}init();sort(l + 1, l + 1 + m, cmp);ans = -1e9 - 1;sum = 0;for (i = 1; i <= m; i++){if (merge(l[i].x, l[i].y)==true){sum++;ans = max(ans, l[i].l);}if (sum == n - 1)break;}if (sum < n - 1)ans = 0;cout << ans << endl;}



0 0
原创粉丝点击