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

题干:

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。。。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441981.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

齐博php百度编辑器上传图片_php版百度编辑器ueditor怎样给上传图片自动添加水印?...

百度ueditor是广泛使用的所见即所得图文排版编辑插件&#xff0c;功能比较完善&#xff0c;美中不足就是不支持自动加水印。万维景盛工程师搜集到php版ueditor自动加水印的教程&#xff0c;希望对大家有帮助。1.打开ueditor目录下的php目录下的config.json 文件在上传配置项添加…

【HDU - 1285】确定比赛名次 (拓扑排序)

题干&#xff1a; 有N个比赛队&#xff08;1<N<500&#xff09;&#xff0c;编号依次为1&#xff0c;2&#xff0c;3&#xff0c;。。。。&#xff0c;N进行比赛&#xff0c;比赛结束后&#xff0c;裁判委员会要将所有参赛队伍从前往后依次排名&#xff0c;但现在裁判委…

mysql dql_Mysql中的DQL查询语句

欢迎进入Linux社区论坛&#xff0c;与200万技术人员互动交流 >>进入 Mysql中的DQL查询语句 1、查询所有列 --查询 学生 表所有记录(行) select *from 学生 --带条件的查询 select *from 学生 where 年龄19 2、查询指定的列 --查询 所有人的姓名和性别 select 姓名,性欢迎…

【POJ - 1028】 Web Navigation( 栈 or 模拟队列 )

题干&#xff1a; Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. …

python循环post请求_循环post请求太多

我正在做一个scrapy spider&#xff0c;我必须发送一个post请求循环才能转到下一个页面&#xff0c;问题是它只发送一个post请求。querystring更改每个页面的元素“currentPage”&#xff0c;因此我必须为每个页面更改此键的值并发送post。但是&#xff0c;正如我之前所说&…

【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)

题干&#xff1a; Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Jo…

递归Java_递归的Java实现

递归是一种应用非常广泛的算法(或者编程技巧)。递归求解问题的分解过程&#xff0c;去的过程叫“递”&#xff0c;回来的过程叫“归”。递归需要满足的三个条件&#xff1a;1. 一个问题的解可以分解为几个子问题的解&#xff1b;2. 这个问题与分解之后的子问题&#xff0c;除了…

【HDU - 2112】 HDU Today(dijkstra单源最短路 + map转换)

题干&#xff1a; HDU Today Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 11 Accepted Submission(s) : 5 Problem Description 经过锦囊相助&#xff0c;海东集团终于度过了危机&#xff0c;从此&#…

JAVA线程并发数量控制_线程同步工具(二)控制并发访问多个资源

声明&#xff1a;本文是《 Java 7 Concurrency Cookbook》的第三章&#xff0c; 作者&#xff1a; Javier Fernndez Gonzlez 译者&#xff1a;郑玉婷控制并发访问多个资源在并发访问资源的控制中&#xff0c;你学习了信号量(semaphores)的基本知识。在上个指南&#xff0c;你实…

*【51nod - 1459】迷宫游戏(记录双向权值的Dijkstra单源最短路)

题干&#xff1a; 你来到一个迷宫前。该迷宫由若干个房间组成&#xff0c;每个房间都有一个得分&#xff0c;第一次进入这个房间&#xff0c;你就可以得到这个分数。还有若干双向道路连结这些房间&#xff0c;你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定…

算法--背包九讲(详细讲解+代码)

背包九讲 目录 第一讲 01背包问题 第二讲 完全背包问题 第三讲 多重背包问题 第四讲 混合三种背包问题 第五讲 二维费用的背包问题 第六讲 分组的背包问题 第七讲 有依赖的背包问题 第八讲 泛化物品 第九讲 背包问题问法的变化 附&#xff1a;USACO中的背包问题 前…

java中白盒测试用例_基于JAVA开发的中国象棋游戏的开发与研究白盒测试用例.doc...

中国象棋白盒测试用例文件状态当前版本V1.0草稿作 者梁世聪完成日期2012/6/17文档模板SSP-VER-T13-V1.0密 级变更历史版本完成日期变更记录作者批准签字V1.02012/6/17无梁世聪梁世聪目 录目录1 目的12 范围13 被测模块列表14 模块逻辑结构14.1 模块逻辑结构图14.2 模块功能定义…

【POJ - 1502】MPI Maelstrom(Dijkstra单源最短路--求一点到其余个点的最小值的最大值)

题干&#xff1a; BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKees research advisor, Jack Swigert, has asked her to ben…

java 强制清除缓存_IDEA强制清除Maven缓存的方法示例

重新导入依赖的常见方式下面图中的刷新按钮&#xff0c;在我的机器上&#xff0c;并不能每次都正确导入pom.xml中写的依赖项&#xff0c;而是导入之前pom.xml的依赖(读了缓存中的pom.xml)。当然除了这些&#xff0c;还可以下面这样&#xff1a;存在的问题上面虽然是重新导入Mav…

ACM算法--spfa算法--最短路算法

求单源最短路的SPFA算法的全称是&#xff1a;Shortest Path Faster Algorithm。 SPFA算法是西南交通大学段凡丁于1994年发表的。 从名字我们就可以看出&#xff0c;这种算法在效率上一定有过人之处。 很多时候&#xff0c;给定的图存在负权边&#xff0c;这时类似…

knn算法python理解与预测_理解KNN算法

KNN主要包括训练过程和分类过程。在训练过程上&#xff0c;需要将训练集存储起来。在分类过程中&#xff0c;将测试集和训练集中的每一张图片去比较&#xff0c;选取差别最小的那张图片。如果数据集多&#xff0c;就把训练集分成两部分&#xff0c;一小部分作为验证集(假的测试…

【POJ-3259】 Wormholes(判负环,spfa算法)

题干&#xff1a; While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac…

【HihoCoder - 1550】顺序三元组(思维)

题干&#xff1a; 给定一个长度为N的数组A[A1, A2, ... AN]&#xff0c;已知其中每个元素Ai的值都只可能是1, 2或者3。 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ N且Ai < Aj < Ak。 Input 第一行包含一个整数N 第二行包含N个整数A1, A2, ... …

joptionpane java_Java JOptionPane

Java JOptionPane1 Java JOptionPane的介绍JOptionPane类用于提供标准对话框&#xff0c;例如消息对话框&#xff0c;确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。JOptionPane类继承了JComponent类。2 Java JOptionPane的声明public class JOptionPa…

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

题干&#xff1a; One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roa…