Problem A: [Usaco2005 Mar]Out of Hay 干草危机

Problem A: [Usaco2005 Mar]Out of Hay 干草危机

Problem A: [Usaco2005 Mar]Out of Hay 干草危机

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

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.
奶牛已经用尽了干草,这是一个必须立即补救的可怕事件。 贝茜打算访问其他农场调查他们的干草情况。有N(2 <= N <= 2,000)个农场(编号为1..N); 贝茜从农场1开始。她将穿越部分或全部M(1 <= M <= 10,000)双向道路,其长度不超过1,000,000,000连接农场。有些农场可以与不同长度的道路相连。所有农场都以这样或那样的方式与农场1相连。贝茜正试图决定她需要多大的水域。她知道每道路长度需要一盎司的水。由于她可以在每个农场获得更多的水,她只关心最长的道路的长度。当然,她计划在农场之间的路线,以便最大限度地减少她必须携带的水量。帮助贝茜知道她将要携带的最大水量:她在任何两个农场之间旅行的最长路的长度是多少,假设她选择的路线最小化了这个数字?当然,这意味着她可能会在道路上回溯,以尽量减少她必须穿越的最长道路的长度。

Input

第1行输入两个整数N和M;2 <= N <= 2,000,1 <= M <= 10,000
接下来M行,每行输入三个整数,表示一条道路的起点终点和长度.

Output

* Line 1: A single integer that is the length of the longest road required to be traversed.
输出一个整数,表示在路线上最长道路的最小值.

Sample Input

3 3
1 2 23
2 3 1000
1 3 43

Sample Output

43
由1到达2,需要经过长度23的道路;回到1再到3,通过长度43的道路.最长道路为43

HINT

Code

#include<bits/stdc++.h>
using namespace std;
int n,m,tot,ans;
int x,y,v;
struct node {
    int x,y,v;
} f[10009];
int father[2009];
int cmp(node a,node b) {
    return a.v<b.v;
}
int find(int x) {
    return x==father[x]?x:father[x]=find(father[x]);
}
inline void put(int x,int y,int val) {
 
    f[++tot].x=x;
    f[tot].y=y;
    f[tot].v=v;
}
void pu() {
    for(int i=1; i<=m; i++) {
        x=f[i].x;
        y=f[i].y;
        int u=find(x);
        int v=find(y);
        if(u!=v) {
            father[v]=u;
            ans=max(ans,f[i].v);
        }
    }
    cout<<ans<<endl;
}
int main() {
    cin>>n>>m;
    for(int i=1; i<=m; i++) {
        cin>>x>>y>>v;
        put(x,y,v);
    }
    sort(f+1,f+m+1,cmp);
    for(int i=1; i<=n; i++)
        father[i]=i;
    pu();
    return 0;
}

About


posted @ 2019-03-09 11:52  ZhaoChongyan  阅读(142)  评论(0编辑  收藏  举报