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 ...
随机推荐
- B - Guess a number!
Problem description A TV show called "Guess a number!" is gathering popularity. The whole ...
- JavaScript中的工厂方法、构造函数与class
JavaScript中的工厂方法.构造函数与class 本文转载自:众成翻译 译者:谢于中 链接:http://www.zcfy.cc/article/1129 原文:https://medium.c ...
- 详解sqlserver查询表索引
SELECT 索引名称=a.name ,表名=c.name ,索引字段名=d.name ,索引字段位置=d.colid ? 1 2 3 4 5 6 7 8 FROM sysindexes a ...
- map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); // roots的值为[1, 2, 3], numbers的值仍为[1, 4, ...
- Python基础学习_01字符串的拼接(字符串的格式化输出)
# 字符串的拼接 ---字符串的格式化输出 # 字符串的拼接 ---字符串的格式化输出 name = input("name:") age = input("age:&q ...
- django创建第一个子文件夹
1.首先安装python, 我安装了Python2.7的版本,安装在了D:\Python25\2.接着安装django,我下载了django 1.6的版本.3. 安装完了之后,打开cmd,然后可以检验 ...
- -1 深度学习基础caffe
一.反思 二.反向传播 三.ubuntu安装caffe 四.追踪关键词
- 洛谷P1427 小鱼的数字游戏
题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了).这对小鱼的 ...
- [LUOGU2730] 魔板
搜索水题.因为只有8个数,排列一共有40320种,直接bfs,判重就行了. 但是判重的时候直接用8进制表示的话要88的bool数组.这种操作太low了,于是我们可以用康托展开,降成8!. 康托展开其实 ...
- 初步体验libsvm用法1(官方自带工具)
在机器学习和模式识别领域,svm理论使用得很广泛,其理论基础是统计学习,但是如果我们的研究方向不是svm理论,我们只是利用已有的svm工具来对我们的任务进行分类和回归,那么libsvm是一个不错的选择 ...