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 ...
随机推荐
- BZOJ 3509 分块FFT
思路: 跟今年WC的题几乎一样 (但是这道题有重 不能用bitset水过去) 正解:分块FFT http://blog.csdn.net/geotcbrl/article/details/506364 ...
- awk杂集-20170911
awk 格式 1.awk -F '分割符' 'BEGIN{} /执行条件/{} END{}' filepath; 默认使用空格分割 2.awk -v word=$command '{print wor ...
- .net中的TreeView的数据绑定与EasyUi_tree的数据绑定
昨天看到了.net中的TreeView,学习了一波TreeView的数据绑定,联想到EasyUi中的Tree的数据,觉得里面的逻辑差不多,就总结了一下两者的数据绑定. 前端页面和必要的JS如下 < ...
- 改造PAXOS算法消灭活锁
分布式一致性协议的目的是确定一个不可变变量分布式存储的取值:通过对国内外一致性算法的研究成果和PAXOS协议活锁的分析,发现引入一个角色作为竞争时的代理提交者就可以解决活锁问题,从而在本文引入“代理提 ...
- 3D集合图元:最小边界框/包围盒(boundingbox)
对于2D边界框的应用时比较广泛地,它为一个简单匹配建立了很小的计算规则,3D模型的boundingbox则比较困难,计算代价较大.对于PCL库的使用则降低了计算难度,三维数值化降低了建模过程,可以使用 ...
- 数据的图表统计highcharts
数据统计常用的图表一般是饼状图.柱状图.线状图,HighCharts可以很好的实现. HighCharts highcharts是基于jquery的一个功能强大的插件,使用时先导入jquery.js ...
- Ad_hoc_polymorphism 备份
https://en.wikipedia.org/wiki/Polymorphism_(computer_science) https://en.wikipedia.org/wiki/Ad_hoc_p ...
- C语言break/continue/exit/return的功能区别
break是跳出整个循环而执行循环体之外的下一条语句: continue只是跳出本次循环继续判断下一次循环条件是否满足. exit() 结束当前进程/当前程式/,在整个程式中,只要调用 exit ,就 ...
- Noip2011 Mayan游戏 搜索 + 模拟 + 剪枝
写了一下午,终于AC了. 由于n<=5, 所以不需要太多的剪枝和技巧也能过.可以将操作后的消方块和下落和剪枝函数写到一个结构体中,这样会减少调试难度,更加简洁. 可以采用如下剪枝: 1. 如果当 ...
- python之openpyxl模块
一 . Python操作EXCEL库的简介 1.1 Python官方库操作excel Python官方库一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库 ...