Roads in the North (树的直径)

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

代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define Inf 0x3f3f3f3f

const int maxn=1e4+5;
typedef long long ll;
using namespace std;
struct edge
{
    int to,cost;
};

vector<edge> e[maxn];
int farthest,ans;
void dfs(int x,int pre,int dis)
{
    for(int i=0;i<e[x].size();i++)
    {
        int xx = e[x][i].to;
        if(xx == pre)
            continue;
        dfs(xx,x,dis+e[x][i].cost);
    }
    if(dis > ans)
    {
        ans = dis;
        farthest = x;
    }
}
int main()
{
        int i,j;

        int x,y;
        edge t;
           while(scanf("%d%d%d",&x,&y,&t.cost)!=EOF)
           {
            t.to = y;
            e[x].push_back(t);
            t.to = x;
            e[y].push_back(t);
            }
        ans = 0;
        dfs(1,-1,0);

        dfs(farthest,-1,0);
        printf("%d\n",ans);

    return 0;
}

原文地址:https://www.cnblogs.com/Staceyacm/p/11258299.html

时间: 2025-01-20 01:04:48

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: 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

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 ?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

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,

Cow Marathon(树的直径)

传送门 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5362   Accepted: 2634 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has

(树的直径)第九届湘潭市大学生程序设计比赛 H-Highway

Highway Accepted : 25   Submit : 104 Time Limit : 4000 MS   Memory Limit : 65536 KB  Highway In ICPCCamp there were n towns conveniently numbered with 1,2,-,n connected with (n?1) roads. The i-th road connecting towns ai and bi has length ci. It is g

hdu 4123 Bob’s Race 树的直径+rmq+尺取

Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads