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

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

告诉我不是我一个人看到输入数据时不知道是什么意思
树的直径裸题
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 40000
using namespace std;
int head[MAX];
int vis[MAX],dis[MAX];
int ans,sum,beg;
struct node
{
int u,v,w;
int next;
}edge[MAX];
void add(int u,int v,int w)
{
edge[ans].u=u;
edge[ans].v=v;
edge[ans].w=w;
edge[ans].next=head[u];
head[u]=ans++;
}
void bfs(int sx)
{
int i,j;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
queue<int>q;
sum=0;
beg=sx;
q.push(sx);
while(!q.empty())
{
int top=q.front();
q.pop();
for(i=head[top];i!=-1;i=edge[i].next)
{
int x=edge[i].v;
if(!vis[x])
{ vis[x]=1;
dis[x]=dis[top]+edge[i].w;
q.push(x);
if(sum<dis[x])
{
sum=dis[x];
beg=x;
}
}
}
}
}
int main()
{
ans=0;
memset(head,-1,sizeof(head));
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
add(a,b,c);
add(b,a,c);
}
bfs(1);
bfs(beg);
printf("%d\n",sum);
return 0;
}

  

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. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

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

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

  4. poj 2631 Roads in the North

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

  5. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

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

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

  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. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

随机推荐

  1. [Twisted] 部署Twisted

    Twisted提供了基础设施,来实现可重用.可配置的方式来部署. 1.Service Twisted使用Service来实现了许多协议,如TCP,FTP,HTTP,SSH等. 实现的IService接 ...

  2. C# 静态类和非静态类的区别

    静态类和非静态类的区别 静态类: static       关键字 调用 类名.方法 在静态方法中只能访问静态成员  在静态类中只能有静态成员 在非静态类中 即可有非静态成员,也可以有静态成员 在静态 ...

  3. Net的struct的内存对齐问题

    很少有人谈起struct的内存对齐问题, 就是在很多C#书中, 也很少提及. 但在实际应用中, 如果不注意内存对齐, struct比较大的话, 则会浪费一定的内存.    先从一个实例看起. publ ...

  4. Fibonacci 数列递归 重复计算

    public class Fibonacci{ public static long F(long n){ System.out.println("call F" + n); ) ...

  5. zoj1276矩阵连乘dp

    很经典的入门dp /*******************************************************************************/ /* OS : 3 ...

  6. php 之 json格式

    /*JSON语法数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组 JSON 数据的书写格式是:名称/值对名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值;如"myw ...

  7. php开源项目学习二次开发的计划

      开源项目: cms 国内 dedecms cmstop 国外 joomla, drupal 电商 国内 ecshop 国外 Magento 论坛 discuz 博客 wordpress   学习时 ...

  8. css 多出一行或多行后显示...的方法

    一行超出显示... .mui-ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 两行超出的显示. ...

  9. C语言学习总结(四) 剩余内容

    第六章.剩余内容 (预处理指令,宏定义,条件编译,文件操作) 预处理指令 简单的来说就是在程序编译之前需要做的事情 1.宏定义 概念: 是一个替换代码的预处理指令,可以在编译之前进行代码替换(宏展开, ...

  10. 【转】改善C#程序的建议2:C#中dynamic的正确用法 空间

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...