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
AC代码:
 #include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=1e4+;
struct node{int to,cap,next;}edge[maxn<<];
int x,y,w,cnt,maxdist,head[maxn];
void add_edge(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].cap=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u,int fa,int &maxdist){
int Dmax=,Dsec=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
int nowd=dfs(v,u,maxdist)+edge[i].cap;
if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
else if(nowd>Dsec)Dsec=nowd;
}
}
maxdist=max(maxdist,Dmax+Dsec);
return Dmax;
}
int main(){
memset(head,-,sizeof(head));maxdist=cnt=;
while(~scanf("%d %d %d",&x,&y,&w)){
add_edge(x,y,w);
add_edge(y,x,w);
}
dfs(,-,maxdist);
printf("%d\n",maxdist);
return ;
}

题解报告:poj 2631 Roads in the North(最长链)的更多相关文章

  1. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  2. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  3. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  4. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  5. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  6. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  7. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

  8. POJ 2631 Roads in the North (树的直径)

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

  9. 题解报告:hdu 4607 Park Visit(最长链)

    Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...

随机推荐

  1. Chrome浏览器 js 关闭窗口失效解决方法

    //获取元素ID var DelHtml = document.getElementById("imgdel"); //alert(DelHtml); //添加点击事件 DelHt ...

  2. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  3. ExtJS常用代码集合

    ExtJS常用代码集合,包括弹出提示框,登陆框,树状结构等等.​1. [代码]弹出提示框     <html>    <head>        <title>Ge ...

  4. CodeForces937B:Vile Grasshoppers(素数性质)

    The weather is fine today and hence it's high time to climb the nearby pine and enjoy the landscape. ...

  5. UIWindow学习

    写在前面 本文内容绝大部分都参考唐巧大神的<iOS开发进阶>,只是结合不是特别长的开发经验加以补充:最后基于UIWindow自定义了一个类似于微信的ActionSheet. UIWindo ...

  6. VMware Workstation安装centos 6.5详细步骤

    转自“http://blog.51cto.com/12496630/2058386” 22.选择分区了,centos新版中使用lvm来分区,我不用过分去计算分区大小,这个模式可以允许用户以后动态调整分 ...

  7. 高效使用ppt素材

    一.素材大致分为: 立体素材:以TG素材为代表的那种高光立体素材 平面素材:以咨询公司麦肯锡.罗兰贝格公司为代表的平面设计的素材 二.使用原则: 原则一:平面左边,立体右边 这个原则告诉你几件事: 如 ...

  8. E20170425-gg

    margin  n(网页设置,CSS) 外铺,(金融,外汇交易)保证金 property n(计算机)类的属性变量 , 资产

  9. 关于TImer使用的注意

    晚点再写   停止Timer let timer = .... timer.invalidate()

  10. hdoj3665【简单DFS】

    题意: 略. 思路: n就10而已,没有环,搜一下就好了.. #include <bits/stdc++.h> using namespace std; typedef long long ...