树的直径问题poj2631
来源:互联网 发布:淘宝优惠网站有哪些 编辑:程序博客网 时间:2024/05/21 09:46
树的直径是指在一棵树中(不能是图),任意两点之间最远的距离。
找出树的直径的方法:任选一个点p出发,运用bfs找到距离点p最远的点q,然后再从q点出发,再次使用bfs找到距离q点最远的点m,那么q和m之间的距离就是树的直径(也就是通过两次bfs来找出树的直径)
在做关于树或图的题目时,树或图的存储应该使用邻接表。
poj2631
Description
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
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.
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
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 61 4 56 3 92 6 86 1 7
Sample Output
22
我的题解:
//这道题中邻接表的建立是用链表,但最好的做法是用数组建立邻接表#include<stdio.h>#include<string.h>#include<stdlib.h>bool flag[10005];int queue[10005];int length[10005];//用来记录当前点到起点的距离typedef struct Point//邻接表的结点{ int num; int l; struct Point* iNext;}ArcP;struct VNode//邻接表的表头{ArcP* first;}AdjList[10005];void init()//初始化邻接表表头{ for(int i=1;i<10005;i++) { AdjList[i].first=(ArcP*)malloc(sizeof(ArcP)); AdjList[i].first->num=i; AdjList[i].first->l=0; AdjList[i].first->iNext=NULL; }}int bfs(){ int Max=1;int top=0;queue[top++]=1;flag[1]=false;length[1]=0;for(int i=0;i<top;i++) { ArcP* p=AdjList[queue[i]].first; while(p->iNext!=NULL) { if(flag[p->iNext->num]) { flag[p->iNext->num]=false; queue[top++]=p->iNext->num; length[p->iNext->num]=length[queue[i]]+p->iNext->l;//用bfs统计各点到起点距离 p=p->iNext; continue; } p=p->iNext; } } for(int i=2;i<=top;i++)//找出第一个最远点 if(length[Max]<length[i]) Max=i; memset(flag,true,sizeof(flag)); top=0; queue[top++]=Max;//以第一个最远点为起点 flag[Max]=false; length[Max]=0; for(int i=0;i<top;i++)//第二次bfs找最远点 { ArcP* p=AdjList[queue[i]].first; while(p->iNext!=NULL) { if(flag[p->iNext->num]) { flag[p->iNext->num]=false; queue[top++]=p->iNext->num; length[p->iNext->num]=length[queue[i]]+p->iNext->l; p=p->iNext; continue; } p=p->iNext; } } Max=1; for(int i=2;i<=top;i++) if(length[Max]<length[i]) Max=i; return length[Max];//树的直径}int main(){int a,b,l,is_max=1,i=0;ArcP* p=(ArcP*)malloc(sizeof(ArcP));init();memset(flag,true,sizeof(flag));while(scanf("%d%d%d",&a,&b,&l)!=EOF)//把数据写入邻接表{ p=AdjList[a].first; while(p->iNext!=NULL) { p=p->iNext; } p->iNext=(ArcP*)malloc(sizeof(ArcP)); p->iNext->l=l; p->iNext->num=b; p->iNext->iNext=NULL; p=AdjList[b].first; while(p->iNext!=NULL) { p=p->iNext; } p->iNext=(ArcP*)malloc(sizeof(ArcP)); p->iNext->l=l; p->iNext->num=a; p->iNext->iNext=NULL;}printf("%d\n",bfs());return 0;}
0 0
- 树的直径问题poj2631
- poj2631树的直径
- POJ1985+POJ2631 求一棵树的直径
- poj2631 Roads in the North(求树的直径裸题)
- 树的直径问题
- 求树的直径问题
- POJ2631
- POJ2631-Roads in the North(树的最长路径)
- 数据结构中树的直径问题
- 树的直径(最长路)问题
- bzoj3124 [Sdoi2013]直径 树的直径
- 【树的直径】
- 树的直径
- 求树的直径
- 树的最长直径
- HDU4607 树的直径
- URAL1056(树的直径)
- 树的直径hdu3721
- Windows系统批处理修改IP,DNS和路由的方法
- android JNI开发之 ----配置环境
- ArcGIS教程:基于路网的城市间距离计算方法
- arcgis for flex 的编辑功能解读
- terminate called after throwing an instance of 'std::out_of_range'
- 树的直径问题poj2631
- 测试标题1
- python发邮件实例
- 使用scanf函数遇到的问题
- 个人为人处事总结
- Javascript闭包深入解析及实现方法
- CodeForces 377B---Preparing for the Contest(二分+贪心)
- 安装ubuntu远程桌面xrdp可视化设置界面
- MyEclipse中启动Tomcat,报OutOfMemoryError: PermGen space的解决方法