POJ 2631 Roads in the North (求树的直径)
Description
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
Output
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22
#include<cstdio>
#include<queue>
#include<string.h>
#define M 100000
using namespace std;
int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
struct stu
{
int from,to,val,next;
}st[M];
void init()
{
num=;
memset(head,-,sizeof(head));
}
void add_edge(int u,int v,int w)
{
st[num].from=u;
st[num].to=v;
st[num].val=w;
st[num].next=head[u];
head[u]=num++;
}
void bfs(int fir)
{
ans=;
int u;
memset(sum,,sizeof(sum));
memset(flag,,sizeof(flag));
queue<int>que;
que.push(fir);
flag[fir]=;
while(!que.empty())
{ u=que.front();
que.pop();
for(i = head[u] ; i != - ; i=st[i].next)
{
if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
{
sum[st[i].to]=sum[u]+st[i].val;
if(ans < sum[st[i].to])
{
ans=sum[st[i].to];
node=st[i].to;
}
flag[st[i].to]=;
que.push(st[i].to);
}
}
}
}
int main()
{
init();
while(scanf("%d %d %d",&a,&b,&c)!=EOF)
{
add_edge(a,b,c);
add_edge(b,a,c);
} bfs();
bfs(node);
printf("%d\n",ans);
}
//输入后Ctrl+Z输出结果
POJ 2631 Roads in the North (求树的直径)的更多相关文章
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- C - Roads in the North DFS+树的直径
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
随机推荐
- 注册jdbc驱动的三种方式
java.sql.DriverManger类简介 java的驱动管理类.管理一组 JDBC 驱动程序. javax.sql.DataSource 接口是 JDBC 2.0 API 中的新增内容,它 ...
- python关于文件的一些记录
1.文件打开: file("data.txt")或open("data.txt")注意不要漏了文件的后缀.(不加参数时,file为你默认为'r',reading ...
- idea使用Git提交代码时忽略指定文件或文件夹
简述 使用idea编写代码并使用git作为版本控制器的时候,常常不需要提交配置文件以及一些其他不需要提交的文件,可以使用.ignore插件来在上传的时候忽略一些文件或文件夹. 安装 注意:安装完成之后 ...
- oracle 触发器,序列,索引
oracle 触发器,序列,索引 --1,触发器 ----trigger /*触发器是一种特殊的存储过程,它与数据表紧密联系,用于保护表中的数据, 当一个定义了特定类型触发器的基表执行插入.修改或删除 ...
- 数据库连接池proxool的两种使用方式
数据库连接池可以通过两种方式来应用,针对web应用和非web应用而来. 非web应用的数据库连接池配置 第一种方式:工厂类 非web应用可以使用工厂模式将数据库连接创建封装在一个工具类中,工具类中又使 ...
- 分类IP地址(A、B、C类)的指派范围、一般不使用的特殊IP地址
分类IP地址(A.B.C类)的指派范围.一般不使用的特殊IP地址 A类地址:0开头,8位网络号 B类地址:10开头,16位网络号 C类地址:110开头,24位网络号 D类地址:1110开头,多播地址 ...
- 10g集群启动顺序
1. 首先, /etc/inittab(不同平台文件名可能不同),文件中的下面3行被调用. h1:35:respawn:/etc/init.d/init.evmd run >/dev/null ...
- sysUpload.vue上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock')
上传组件 的时候 看进度的时候 不要mock 注释掉 // if (process.env.NODE_ENV !== 'production') require('@/mock') <!-- * ...
- JS怎么创建一个类?
15. JS怎么创建一个类? 方式1 : var obj = new Object(); 方式2 : var obj = {}; 16.JS的typeof返回哪些数据类型? string.number ...
- 批量下载ts视频文件
第一步 使用chrome 按F12进入开发模式,拖动视频进度条到视频结束: 然后找到.m3u8以结尾的文件并保存为文本文件. 第二步 点开查看里面是否存在如下以ts结尾的文件内容 ...... /20 ...