zoukankan      html  css  js  c++  java
  • Poj2631--Roads in the North(树的直径)

    Roads in the North
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2428   Accepted: 1190

    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

    Source

     
    图中最长路,  任选一点Bfs得出最长路一个端点--->求最长路;
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #define N 100000+10
    #define M 100000+10
    using namespace std;
    struct Edge
    {
        int from, to, val, next;
    }edge[M];
    int head[N], cnt;
    int dist[N]; bool vis[N];
    int node;
    int ans;
    void init()
    {
        cnt=0;
        memset(head, -1, sizeof(head));
    }
    int dfs(int sx)
    {
        queue<int> Q;
        memset(dist, 0, sizeof(dist));
        memset(vis, false, sizeof(vis));
        vis[sx]=true;
        Q.push(sx);
        node=sx; ans=0;
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            for(int i=head[u]; i!=-1; i=edge[i].next)
            {
                Edge E=edge[i];
                if(!vis[E.to]&&dist[E.to]<dist[u]+E.val)
                {
                    dist[E.to]=dist[u]+E.val;
                    vis[E.to]=true;
                    Q.push(E.to);
                    if(dist[E.to]>ans)
                    {
                        ans=dist[E.to];
                        node=E.to;
                    }
                }
            } 
        }
    }
    void solve()
    {
        dfs(1);
        dfs(node);
        printf("%d
    ", ans);
    }
    void add(int u, int v, int w)
    {
        Edge E={u, v, w, head[u]};
        edge[cnt]=E;
        head[u]=cnt++;
    }
    int main()
    {
        int a, b, c;
        init();
        while(scanf("%d%d%d", &a, &b, &c) != EOF)
        {
            add(a, b, c); 
            add(b, a, c);
        }
        solve();
        return 0;    
    } 
  • 相关阅读:
    如何得到需要下载文件的链接(路径)?
    Python之内存泄漏和内存溢出
    IO多路复用
    python-socket和进程线程协程(代码展示)
    Xshell(远程)连接不上linux服务器(防火墙介绍)
    Shell--变量
    Python读写配置文件模块--Configobj
    python文件处理之fileinput
    python之commands和subprocess入门介绍(可执行shell命令的模块)
    linux下 > /dev/null 2 > &1 的意思和如何在后台启动进程
  • 原文地址:https://www.cnblogs.com/soTired/p/5272195.html
Copyright © 2011-2022 走看看