zoukankan      html  css  js  c++  java
  • POJ 2631-Roads in the North-树的直径

    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
    Sample Output
        22
    题目大意:
    给定一棵树,求距离最远的两个点的距离。
    核心思想:
    树的直径模板题。
    任选一个点作为起点s1,第一次bfs得到距离s1最远的点s2。
    以s2为起点,第二次bfs得到树的直径。
    详见代码。
    代码如下:

    #include<cstdio>
    #include<iostream>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const int N=1e4+10;
    //用于vector 
    struct node{
        int x,v;
        node()
        {
        }
        node(int xx,int vv)
        {
            x=xx;
            v=vv;
        }
    };
    vector<node>vec[N];
    int n=1,end,mx;//end记录距离起点最远的点,mx记录距离 
    bool vis[N];
    //广搜,求距离st最远的点以及距离 
    void bfs(int st)
    {
        queue<node>q;
        q.push(node(st,0));
        for(int i=0;i<N;i++)
            vis[i]=0;
        vis[st]=1;
        while(!q.empty())
        {
            int x=q.front().x;
            int v=q.front().v;
            q.pop(); 
            int len=vec[x].size();
            for(int i=0;i<len;i++)
            {
                int tx=vec[x][i].x;
                if(vis[tx])continue;
                vis[tx]=1;
                int tv=v+vec[x][i].v;
                q.push(node(tx,tv));
                if(tv>mx)
                {
                    mx=tv;
                    end=tx;
                }
            }
        }
        return;
    }
    int main()
    {
        int x,y,v;
        //输入 
        while(scanf("%d%d%d",&x,&y,&v)!=EOF)
        {
            n++;
            vec[x].push_back(node(y,v));
            vec[y].push_back(node(x,v));
        }
        //第一次bfs 
        mx=0;
        bfs(1);
        //第二次bfs 
        mx=0;
        bfs(end);
        //输出 
        cout<<mx<<endl;
        return 0;
    }



  • 相关阅读:
    板邓:php获取数组元素个数
    板邓:PHP解决Using $this when not in object context in【转】
    板邓:MYSQL distinct关键字查询重复值只显示一条记录
    板邓: php 过滤文章的html标签方法
    板邓:PHP使用mb_strimwidth截取中文字符串方法大全
    板邓:php str_replace单双引号的替换问题
    板邓:PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)
    板邓:wordpress自定义伪静态 WP_Rewrite
    板邓:wordpress获取当前页面的id、别名
    获取文件夹名
  • 原文地址:https://www.cnblogs.com/NothingbutFight/p/11306795.html
Copyright © 2011-2022 走看看