zoukankan      html  css  js  c++  java
  • poj 2631 Roads in the North (自由树的直径)

    Roads in the North
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4513   Accepted: 2157

    Description

    Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. 
    Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area. 

    The area has up to 10,000 villages connected by road segments. The villages are numbered from 1. 

    Input

    Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

    Output

    You are to output a single integer: the road distance between the two most remote villages in the area.

    Sample Input

    5 1 6
    1 4 5
    6 3 9
    2 6 8
    6 1 7
    

    Sample Output

    22

    题目大意:

    求树中任意两点最短路长度的最大值。

    求树的直径模板题。

    还是因为室友的数据结构作业,我才知道有这么个问题。

    假设有这么个直径st,s是起点,t是终点。先求任一点到所有其他点的距离,则距离该点最远的点一定在直径上,否则该点就是直径了。再从这个最远的点出发,求该点到所有点的距离,则此次最远的点和上次最远的点必定一个是s,一个是t。

    做两次bfs就好了。(树上的单源最短路可以O(n)时间内解决)

    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<string>
    #include<iostream>
    #define ll long long
    #define maxn 10000
    
    int to[maxn*2+5];
    int w[maxn*2+5];
    int next[maxn*2+5];
    int head[maxn+5];
    
    struct tnode
    {
        int point;
        int dis;
    };
    int vis[maxn+5];
    
    int main()
    {
        int cnt=0;
        memset(head,-1,sizeof(vis));
        int a,b,c;
        while(scanf("%d%d%d",&a,&b,&c)!=EOF)
        {
            to[cnt]=b;w[cnt]=c;next[cnt]=head[a];head[a]=cnt++;
            to[cnt]=a;w[cnt]=c;next[cnt]=head[b];head[b]=cnt++;
        }
    
        std::queue<tnode> q;
        memset(vis,0,sizeof(vis));
        q.push((tnode){1,0});
        vis[1]=1;
        int far=0,farp=1;
        while(!q.empty())
        {
            tnode node=q.front();q.pop();
            if(node.dis>far)
            {
                far=node.dis;
                farp=node.point;
            }
            for(int i=head[node.point];i!=-1;i=next[i])
            {
                if(!vis[to[i]])
                {
                    q.push((tnode){to[i],node.dis+w[i]});
                    vis[to[i]]=1;
                }
            }
        }
    
        memset(vis,0,sizeof(vis));
        q.push((tnode){farp,0});
        vis[farp]=1;
        int ans=0;
        while(!q.empty())
        {
            tnode node=q.front();q.pop();
            ans=std::max(ans,node.dis);
            for(int i=head[node.point];i!=-1;i=next[i])
            {
                if(!vis[to[i]])
                {
                    q.push((tnode){to[i],node.dis+w[i]});
                    vis[to[i]]=1;
                }
            }
        }
    
        printf("%d
    ",ans);
    
        return 0;
    }
    View Code
  • 相关阅读:
    推荐几款Silverlight Tools【转载】
    Emit Vs CodeDom
    Silverlight 中实现Service同步调用
    一个配置文件的Mapping
    Silverlight:获取ContentTemplate中的命名控件
    关于计划
    巧用异步委托解决异步并发问题
    我是如何学习NodeJs的 – 笔记1
    应用HttpHandler, Json, JQuery, ASP.Net UserControl等技术处理 Ajax 的解决方案
    关于程序员的那些事一个五年程序员的总结
  • 原文地址:https://www.cnblogs.com/acboyty/p/9953238.html
Copyright © 2011-2022 走看看