poj--2631--Roads in the North(树的直径 裸模板)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2389 | Accepted: 1173 |
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
Source
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 10010
int Node,m,n,cnt,ans;
int head[MAXN],dis[MAXN],vis[MAXN],pre[MAXN];
struct node
{
int u,v,val;
int next;
}edge[MAXN];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
for(int i=0;i<10010;i++)
pre[i]=i;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
void bfs(int sx)
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
ans=0;
queue<int>q;
Node=sx;
q.push(Node);
vis[Node]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(!vis[E.v])
{
vis[E.v]=1;
dis[E.v]=dis[u]+E.val;
q.push(E.v);
}
}
}
for(int i=1;i<=cnt;i++)
{
if(ans<dis[i])
{
ans=dis[i];
Node=i;
}
}
}
void slove()
{
bfs(1);
bfs(Node);
printf("%d\n",ans);
}
int main()
{
int a,b,c;
init();
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
add(a,b,c);
add(b,a,c);
}
slove();
return 0;
}
poj--2631--Roads in the North(树的直径 裸模板)的更多相关文章
- 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: 2359 Accepted: 115 ...
- 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 (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
随机推荐
- 大白话理解this
日常开发中,我们经常用到this.一开始常会用一种感觉去判断this的指向,当遇到复杂的函数调用时,就分不清this的指向. 今天我们来由浅入深来学习下. function family1(){ va ...
- (转载)android开发常见编程错误总结
首页 › 安卓开发 › android开发 android开发常见编程错误总结 泡在网上的日子 / 文 发表于2013-09-07 13:07 第771次阅读 android,异常 0 编辑推荐:稀 ...
- android系统源码下载
ubuntu 安装git curl python 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: mkdir ~/bin PATH=~/bin:$PATH 下载 Repo 工具,并确 ...
- 错误:the apk for your currently selected variant(app-release-unsigned.apk)is not signed.Please specity a signing configuration for this variant(release)
1:导入android studio project 时总会出现运行处一个红色叉号,这里可以点击选择叉号上面显示的Edit Configurations 查看右下角的错误警告信息.: 2:记录错误: ...
- DELPHI调试出现disconnected session的解决办法
我在控制面板中,是禁用了UAC的,如下图 但是,在注册表中启用了UAC(EnableLUA), 工程中请求了管理员权限,如下图: 所以,整个权限请求混乱了. 解决办法,要么把注册表的LUA设置为0,要 ...
- RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
http://blog.csdn.net/zhu_tianwei/article/details/40920985 spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.s ...
- a rel=noopener
看vue-element-admin的源码的时候,看到a 标签使用 rel=noopener: 然后就很奇怪这个是干什么用的:然后百度到一篇文章,涨知识了. 个人的理解是:不加 rel=noopen ...
- day03深浅拷贝、文件操作和函数初识
一.赋值.浅拷贝与深拷贝 直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法, ...
- sqlserver重组索引,优化碎片
dbcc dbreindex('digitlab.dbo.RequestForm','',90) dbcc dbreindex('digitlab.dbo.Requestitem','',90) db ...
- Centos 7 安装图形化界面
Centos 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方便我们使用,这里介绍一下 CentOS7安装图形化桌面系统的方法. 一. ...