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,DFS求出u的最远点m,然后再DFS求出m的最远点n,之后m-n就是最长路径 2.还可以用DP写,d(i)表示以i为根节点的最大路径值,=max(d(j)+1),j为i的子节点,取出最大和次大的d(j) +2;
 #include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define Max 10005
using namespace std;
vector <int> tree[Max],len[Max];
int d[Max],vis[Max];
bool flag=;
int an;
int dfs(int node,int &ans)
{
int i,j;
int maxn=;
int t,index;
vis[node]=;
for(i=;i<tree[node].size();i++)
{
if(vis[tree[node][i]])
continue;
dfs(tree[node][i],t);
if((t+len[node][i])>maxn)
{
// cout<<t<<endl;
maxn=t+len[node][i];
index=i;
}
}
ans=maxn;
return i;
}
void dfs1(int node,int sum)
{
int i;
vis[node]=;
if(flag)
return;
if(sum==)
{
flag=;
an=node;
return;
}
for(i=;i<tree[node].size();i++)
{
if(sum>=len[node][i]&&vis[tree[node][i]]==)
dfs1(tree[node][i],sum-len[node][i]);
}
}
int main()
{
int i,j;
int a,b,val,p=;
freopen("in.txt","r",stdin);
bool flag=;
for(i=;i<Max;i++)
tree[i].clear(),len[i].clear();
while(scanf("%d%d%d",&a,&b,&val)!=EOF)
{
tree[a].push_back(b);
tree[b].push_back(a);
len[a].push_back(val);
len[b].push_back(val);
}
memset(vis,,sizeof(vis));
dfs(,p);
memset(vis,,sizeof(vis));
dfs1(,p);
memset(vis,,sizeof(vis));
dfs(an,p);
cout<<p<<endl;
}
												

Roads in the North(POJ 2631 DFS)的更多相关文章

  1. 题解报告:poj 2631 Roads in the North(最长链)

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

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

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

  3. Roads in the North (树的直径)

    Building and maintaining roads among communities in the far North is an expensive business. With thi ...

  4. Hopscotch(POJ 3050 DFS)

    Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2845   Accepted: 1995 Descrip ...

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

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

  6. 深度搜索DFS-Lake Counting(POJ NO.2386)

    题目链接POJ NO.2386 解题思路: 这个也是一个dfs 的应用,在书上的例子,因为书上的代码并不全,基本都是函数分块来写,通过这个题目也规范了代码,以后能用函数的就都用函数来实现吧.采用深度优 ...

  7. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. [wikioi2144]砝码称重2(另类的dfs)

    题目描述 Description 有n个砝码,现在要称一个质量为m的物体,请问最少需要挑出几个砝码来称? 注意一个砝码最多只能挑一次 输入描述 Input Description 第一行两个整数n和m ...

  9. 【BZOJ】1603: [Usaco2008 Oct]打谷机(水题+dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1603 这种水题... dfs没话说.. #include <cstdio> #inclu ...

随机推荐

  1. npm install 本地安装与全局安装

    npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已: npm install grunt # 本地安装 npm install -g gr ...

  2. 使用StreamReader与StreamWriter进行文本文件读写

    namespace filetest { class FileUtil { public static void WriteFile(string file) { using (FileStream ...

  3. LeetCode_Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. VS2010使用静态编译的qt库(Qt 5)

    Qt 5引入了一种新的编写方式. Qt开发界面很方便,但发布程序就不那么方便了,你的把引用到的dll一起发布才行,要是能静态编译就好了,发布的时候只有一个exe多方便. 虽然以前为了方便,直接安装的q ...

  5. bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  6. 3Sum Closest 解答

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  7. c、c++知识点

    一. (1)在linux下类似uint8_t这样的文件定义在头文件<stdint.h>里面   (2)截取了stdint.h头文件里的一些常用部分 二.c++中c_str()用法 函数返回 ...

  8. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  9. Unity SendMessage方法

    我们今天研究下SendMessage方法, 如果我们需要执行某一个组件的方法时候可以使用SendMessage gameObject.SendMessage("A"); 即可通知当 ...

  10. C++: int和string相互转换

    假设在一个C++的程序中常常会用到int和string之间的互换.个人建议能够写成一个函数,下次用的时候直接调用就可以. #include <iostream> #include < ...