Out of Hay (poj2395) (query set), haypoj2395

Source: Internet
Author: User

Out of Hay (poj2395) (query set), haypoj2395
Out of Hay

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:11580   Accepted:4515

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
/* Calculate the maximum weight in the Minimum Spanning Tree */# include <stdio. h ># include <algorithm> using namespace std; int pre [2020]; int ans; struct st {int a, B, l;} data [10010]; int find (int N) {return pre [N] = N? N: pre [N] = find (pre [N]);} int cmp (st a, st B) {return. l <B. l ;}int main () {int I, n, m, ans, x, y; scanf ("% d", & n, & m ); for (I = 1; I <= n; I ++) pre [I] = I; for (I = 1; I <= m; I ++) scanf ("% d", & data [I]. a, & data [I]. b, & data [I]. l); sort (data + 1, data + m + 1, cmp); for (I = 1, ans = 0; I <= m; I ++) {x = find (data [I]. a); // It is often written as x = pre [data [I]. a]! Understanding is the most important !!! Y = find (data [I]. B); if (x! = Y) {if (x> y) pre [x] = y; elsepre [y] = x; ans = max (ans, data [I]. l) ;}} printf ("% d \ n", ans); return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.