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



  • 相关阅读:
    日常排雷:redis报错 could not get a resource from the pool
    阿里云centos服务器tomcat启动后,浏览器请求无响应
    并发生产顺序单据号测试
    json 数据 格式,请求接口,部分字段无法注入
    baomidou 动态数据源@DS 使用问题
    SpringMVC框架深入(八)--SpringMVC原理
    Spring框架深入(七)--json数据交互
    框架理论深入(六)--拦截器
    Spring框架深入(五)--文件上传和异常处理
    int和Integer的区别
  • 原文地址:https://www.cnblogs.com/NothingbutFight/p/11306795.html
Copyright © 2011-2022 走看看