POJ-2395 Out of Hay

来源:互联网 发布:java 计算调用时间 编辑:程序博客网 时间:2024/06/05 00:37
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 3
1 2 23
2 3 1000
1 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.

题意
Bessie准备到农场去取水,现在在农场1.她去m条路中的一些或者所有条双向路(连通农场的),但是路的长度必须不超过 1,000,000,000,每个农场都必须直接或间接与农场1相连。 她可以获得足够充足的水,她现在只担心路程的长短,所以她准备找出当连通这n个农场(总长度最短时)的最长边的距离。
输入n,m,表示有n个农场,m条路,每条路都会有两个农场以及它们之间的距离。输出当连通这n个农场时(总长度最短时)的最长边。
题解
根据题意,本题需用最小生成树的思想来写,其实就是求最小生成树的最长边。
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;int n,m;int f[2005]={0};struct  node{    int u;    int v;    int w;}q[10005];bool cmp(node a,node b){    return a.w<b.w;}void init()                     //初始化{    int i;    for(i=1;i<=n;i++)        f[i]=i;}int getf(int v)                //寻找祖先的递归函数{    if(f[v]==v)         return v;    else    {        f[v]=getf(f[v]);          //路径压缩        return f[v];    }}int merge(int v,int u)             //合并两个集合{    int t1,t2;    t1=getf(v);    t2=getf(u);    if(t1!=t2)                     //判断两个点是否在一个集合里    {        f[t2]=t1;        return 1;    }    return 0;}int main(){    int i,j,maxx,num;    while(~scanf("%d%d",&n,&m))    {        init();        for(i=1;i<=m;i++)            scanf("%d%d%d",&q[i].u,&q[i].v,&q[i].w);        sort(q+1,q+m+1,cmp);           //因为是从1输入的,而sort排序默认从0开始,所以都要加一        maxx=-1;        num=0;        for(i=1;i<=m;i++)              //按距离排好序后枚举每一条边        {            if(merge(q[i].u,q[i].v))   //如果目前这条边的两个顶点在一个集合里,即连通,则选用这个边            {                num++;                if(maxx<q[i].w)                  maxx=q[i].w;         //更新最长边            }            if(num==n-1)                break;        }        printf("%d\n",maxx);    }    return 0;}

原创粉丝点击