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
6 1 7

Sample Output

22

  有一个树结构, 给你树的所有边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径)

首先从树上任意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b点最远的点c. 那么bc间的距离就是树的直径.

       程序实现用的是邻接表来表示树结构.

       Head[i]==j 表示与i结点连接的边组成了一条链表, 其中第j条边是这条链的头一个元素, 接着通过j可以找到剩余的(与i连接的)边.

       Edge是用来表示每条边的结构.

   BFS返回从s结点能走到的最远的点的编号

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
const int maxn=10000+5;
const int maxm=1000000+5;
//边结构

struct Edge
{
    Edge() {}
    Edge(int to,int cost,int next):to(to),cost(cost),next(next) {}
    int to;
    int cost;
    int next;
} edges[maxm]; //所有边
int cnt;       //边总数
int head[maxn];//头结点

//添加两条有向边
void AddEdge(int u,int v,int cost)
{
    edges[cnt]=Edge(v,cost,head[u]);
    head[u]=cnt++;
    edges[cnt]=Edge(u,cost,head[v]);
    head[v]=cnt++;
}

//dist[i]表当前点到i的距离
int dist[maxn];

//BFS返回从s结点能走到的最远的点的编号
int BFS(int s)
{
    int max_dist=0;//记录最远距离
    int id=s;      //记录最远点
    queue<int> Q;
    memset(dist,-1,sizeof(dist));
    dist[s]=0;
    Q.push(s);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(dist[u]>max_dist)
        {
            max_dist=dist[u];
            id=u;
        }
        for(int i=head[u]; i!=-1; i=edges[i].next)
        {
            Edge &e=edges[i];
            if(dist[e.to]==-1)//未访问过e.to点
            {
                dist[e.to]=dist[u]+e.cost;
                Q.push(e.to);
            }
        }
    }
    return id;
}

int main()
{
    int u,v,cost;
    memset(head,-1,sizeof(head));
    cnt=0;
    while(scanf("%d%d%d",&u,&v,&cost)==3)
        AddEdge(u,v,cost);
    printf("%d\n",dist[BFS(BFS(u))]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值