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

The UofA Local 1999.10.16

图中最长路,  任选一点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\n", 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;
} 
时间: 2024-10-21 06:43:11

Poj2631--Roads in the North(树的直径)的相关文章

UVa 10308 Roads in the North 树的直径

题目来源:UVa 10308 Roads in the North 题意:求距离最远的2点之间的距离 思路:裸的树的直径 或者树形DP #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn = 100010; struct node { int to, w; node(){} node(int to, int w): to(to), w(w) {

POJ 2631 Roads in the North 树的直径

题目大意:裸的树的直径. 思路:随便用一个点跑BFS,求出这个点到所有点的距离,取距离最长的那个点,再用那个点跑BFS,最远的距离就是这棵树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 20010 using namespace std; int x,y,z;

poj2631 ?Roads in the North(求树的直径)

Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2941   Accepted: 1447 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such

POJ2631 Roads in the North

题解: 树的最长路径,根据树的最长路径的性质,从任一一点bfs得到最远的点的位置,然后再从该点重新bfs一次就可以得到树的直径了 数学证明http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html 代码: #include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #in

UVA10308 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

poj2631 求树的直径裸题

题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: 假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s.t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 证明: 1.设u为s-t路径上的一点,结论显然成立,否则设搜到的最远点为T则   dis(u

poj2631 树的直径 + bfs

1 //Accepted 492 KB 0 ms 2 //树的直径 bfs 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 using namespace std; 8 const int imax_n = 10005; 9 struct node 10 { 11 int u,v,c; 12 node() 13 { 14 15 } 16 n

poj 2631 Roads in the North

题目连接 http://poj.org/problem?id=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出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c之间的距离就是树的直径. 模板: const int N=1e6+5; int head[N]; int dis[N]; bool vis[N]; int cnt=0,b,mxn=0; struct edge { int to,w,next; }edge[N]; void add_edge(int u,