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 ...
随机推荐
- JavaScript之BOM和DOM
前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DO ...
- deepin下jdk和tomcat的安装教程
在deepin上安装java是真的心累啊,照着网上的教程弄,结果一团糟.好不容易折腾成功了,记录下来. 1.下载jdk 首先我们要知道,用sudo 类似命令下载的jdk,是open的jdk,是开源的, ...
- 如何防范自己的IP泄漏
在正式进行各种“黑客行为”之前,黑客会采取各种手段,探测(也可以说“侦察”)对方的主机信息,以便决定使用何种最有效的方法达到自己的目的.来看看黑客是如何获知最基本的网络信息——对方的IP地址:以及用户 ...
- C# 把对象序列化 JSON 字符串 和把JSON字符串还原为对象
/// <summary> /// 把对象序列化 JSON 字符串 /// </summary> /// <typeparam name="T"> ...
- paramiko模块学习笔记
SSHClient 基于用户名密码连接 import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ss ...
- 如何 打包整合linux系统文件夹 用于刷机包等等, 其中包括打包 句号开头 . 开头的文件, 排除系统文件 等
tar --exclude=proc/* --exclude=sys/* -cvjf rootfs.tar.bz2 * .[!.]* -R
- HAOI2006 受欢迎的牛 缩点
不难分析出我们就是要求是否有唯一一个出度为0的强连通分量. Code: #include<cstdio> #include<stack> #include<algorit ...
- 关闭浏览器 清除session
捕获关闭浏览器的事件 关于关闭IE清空session的总结 Session过期会清楚session 还可以手动清除session实现关闭浏览器时清除session的方法
- buddyinfo 内存碎片数据采集
不说了,上工具 #cat buddyinfo.sh #!/bin/sh #*************************************************************** ...
- web内置对象
内置对象,宿主对象,自定义对象的区别? 内置对象: 系统所提供的对象:Object,Array,Math,Date等等. 宿主对象: JS所运行的环境提 ...