题干:

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.

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 6
1 4 5
6 3 9
2 6 8
6 1 7

Sample Output

22

解题报告:

这题有好多地方是容易错的,bfs中的maxx和retp不能不赋值,不然就会wa!想想也是,极限情况,只有一个点,或者是空图,总之可以构造出一个让他根本进不去那些循环的,或者进不去那个while的,所以需要初始化!这也是为什么w=maxx那句不能放到注释的那里。

AC代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX = 2e5 + 5 ;
const int INF = 0x3f3f3f3f;
struct Node {int to;int w;int ne;
} e[MAX];
struct point {int pos,c;point(){}//没有此构造函数不能写  node t  这样point(int pos,int c):pos(pos),c(c){}//可以写node(pos,cost)这样};
int head[MAX];
int cnt = 0 ;
bool vis[MAX];
void init() {cnt = 0;memset(head,-1,sizeof(head));
}
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt;cnt++;
} int bfs(int x,int &w) {queue <point> q;int maxx = 0;int retp = x ;//返回的点坐标 memset(vis,0,sizeof(vis) );q.push(point(x,0));vis[x] = 1;point now;while(q.size() ) {point cur = q.front();q.pop();for(int i = head[cur.pos]; i!=-1; i=e[i].ne) {if(vis[e[i].to]) continue;vis[e[i].to] = 1;now.pos = e[i].to;now.c = cur.c + e[i].w;if(now.c>maxx) {maxx = now.c;retp = now.pos;}q.push(now);}//w = maxx;}w = maxx;return retp;
}
int main()
{init();int u,v,w;while(~scanf("%d%d%d",&u,&v,&w) ) {add(u,v,w);add(v,u,w);}int ans1 = 0,ans2 = 0;u = bfs(1,ans1);v = bfs(u,ans2);printf("%d\n",ans2);return 0 ;
}

【POJ - 2631】Roads in the North (树的直径,模板)相关推荐

  1. C++剑指offer:[POJ]2631 Roads in the North - 用树形DP的方式求出一棵树的直径

    前言 此题是道很简单的题(做法不单一,不仅只有树形DP的方法) 做完了这道题才发现此题原来是一道求树的直径的题,也就是求树上两个节点的最大距离. 题目 问题 N(2692): [POJ2631]北极地 ...

  2. 0x63.图论 - 树的直径与最近公共祖先

    目录 一.树的直径(Diameter) 1.树形DP求树的直径 2.两次BFS/DFS求树的直径 1.POJ 1985.Cow Marathon(DFS求树的直径模板题) 2.AcWing 350. ...

  3. 牛客 - 点对最大值(树的直径)

    题目链接:点击查看 题目大意:给出 n 个点组成的一棵树,每个点和每条边都有权值,现在需要求出一对点 ( x , y ) ,使得点 x 到点 y 的唯一路径权值和最大,权值和包括点 x 和点 y 的权 ...

  4. 小A与欧拉路 (树加边求最小权值欧拉路+树的直径)

    链接:https://ac.nowcoder.com/acm/contest/369/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...

  5. 【POJ - 2631 】Roads in the North(树的直径)

    题干: Building and maintaining roads among communities in the far North is an expensive business. With ...

  6. POJ2631 Roads in the North

    题面:http://poj.org/problem?id=2631 本题是求树的直径裸题.Code: #include<iostream> #include<cstdio> # ...

  7. poj 1985 Cow Marathon 【树的直径】

    求树的直径 /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostre ...

  8. poj 1383 Labyrinth 树的直径

    题意: 给一个迷宫,其中任意两个可以走的点之间只有一条路,求在这个迷宫里最长能走多远. 分析: 求树的直径,两次dfs即可. 代码: //poj 1383 //sep9 #include <io ...

  9. POJ 1849 Two(树的直径+思维)

    题目链接:http://poj.org/problem?id=1849        题意是有n个点,n-1条边(树形图),在s点放两个机器人,问这两个机器人遍历完所有的点的最少花费(不用回到原点). ...

  10. HDU 3966 POJ 3237 HYSBZ 2243 HRBUST 2064 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

最新文章

  1. EMC Isilon(OneFS)删除重要数据后恢复案例
  2. 德国Renusol进军南非市场 瞄准屋顶光伏
  3. 计算机绘画作品 星空,关于近期绘画作品《星空系列》的自述:
  4. python canny检测_【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)...
  5. word 转 html cms,Java 将Word文件转换为HTML格式文件
  6. Python读取word文档(python-docx包)
  7. Apache Tomcat 信息泄露漏洞(CVE-2016-8747)
  8. 算法 Tricks(六)—— 判断一个数是否为完全平方数
  9. otg模拟器_全能模拟器 RetroArch 安卓版上手教程
  10. Vector去除重复元素
  11. RecyclerView条目item点击事件---放大
  12. c语言时钟程序整点报时,C语言 · 报时助手(示例代码)
  13. 网络协议 5 - ICMP 与 ping:投石问路的侦察兵
  14. 键盘上使用计算机重启,我终于找到了如何使用键盘和鼠标重新启动计算机
  15. 电商用户行为可视化分析
  16. IRIS的镜像配置(1)
  17. Python uiautomator2安卓自动化测试
  18. 难解的AIoT焦虑,华为是否在准备一剂特效药存在?
  19. 康佳芯云存储芯片生产突破100K,迎来半导体业务发展里程碑
  20. 自定义 epoll 的实现

热门文章

  1. 第一章 概率论的基本概念
  2. 【数据结构与算法】二分查找
  3. 二分归并排序算法_第五篇排序算法|归并排序
  4. java程序设计实验报告册_20145215《Java程序设计》实验一实验报告
  5. 用批处理实现文本文件中指定字符串的替换 zz
  6. php面试编程题_PHP程序员面试题(经典汇总,mysql为主)
  7. pythonvim编辑教程_Pycharm学习教程(6) Pycharm作为Vim编辑器使用
  8. eclipse 中文_谁说API必须用英文?中文API的Java库可以有!
  9. java版本streamgobbler_java调用本地命令 Runtime class's exec() method
  10. pythonopencv算法_python opencv之SURF算法示例