• 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;
    }



  • 相关阅读:
    JavaScript作用域
    JavaScript数据类型
    CSS页面布局(二)
    CSS页面布局(一)
    CCS界面组件
    JavaScript面向对象设计
    python-序列化与反序列化
    python-常用模块
    python-re模块
    python-匿名函数
  • 原文地址:https://www.cnblogs.com/NothingbutFight/p/11306795.html
Copyright © 2020-2023  润新知