题干:

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

解题报告:

树的直径,裸题。套模板。

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 ;
}

总结:

1.  很迷,,,不知道为什么,maxx初始化成-INF,或者retp不初始化成x,都会re。。。。

【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. POJ2631 Roads in the North

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

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

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

  5. poj 1383 Labyrinth 树的直径

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

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

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

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

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

  8. 【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

    题干: An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a pa ...

  9. 树的直径/重心 学习笔记

    树的直径 POJ2631 Roads in the North 题意:裸的直径 题解:套模板,但是开始的时候,我的代码在第一次dfs循环的时候,没有考虑到,路长全为0的情况,而用来记录最远点的maxp ...

最新文章

  1. 【数理知识】差分 差分方程
  2. QT的QAccessibleEvent类的使用
  3. android 仿ios tabs,React Native兼容iOS Android的TabBar
  4. Couchbase应用示例(初探)
  5. SAP CRM WebClient UI data loss设计原理
  6. 你是外包,麻烦不要偷吃零食,注意素质...
  7. 安装Python-2.5.2,并修改默认python版本
  8. DJ 算法的队列优先优化
  9. AMOS问卷数据建模前传【SPSS 052期】
  10. shell脚本学习指南-学习(2)
  11. wincc连接mysql数据库说明_wincc 数据库的连接方法
  12. 三极管当做开关的导通方式
  13. Python 批量修改PDF文件名
  14. 腾讯云服务器利用镜像部署WordPress个人网站
  15. win7 网络不显示电脑连接到服务器,Win7在网上邻居上看不到别的电脑怎么办?
  16. 逆概率加权法(Inverse Probability Weighting, IPW)的原理及R实现
  17. HTC M7日版HTL22刷机包 毒蛇2.5.0 ART NFC Sense6.0
  18. 模仿天猫实战【SSM版】——后台开发
  19. GeneXus for SAP的最新动态
  20. SpringMVC整合websocket实现消息推送及触发

热门文章

  1. 【数据结构与算法】【算法思想】位图
  2. [Leetcode][第112题][JAVA][路径总和][递归][队列]
  3. [Leetcode][JAVA][第20题][Stack][Map]
  4. c语言程序定义不知数量的一维数组,c语言程序设计10-第6章 利用数组处理批量数据 6.1 怎样定义和引用一维数组.ppt...
  5. windows 修改nginx端口号_分享Nginx搭建图片服务器简单实现
  6. 神舟战神换cpu教程_神舟将十代i5称为“神U出世”?聊聊到底有哪些优势
  7. linux内核打印前有buildroot,buildroot-linux内核
  8. cmake学习(一)静态库与动态库构建
  9. A20成功驱动FT5206触摸屏
  10. 大型项目使用Automake/Autoconf完成编译配置