3517.   The longest athletic track


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 880   Accepted Runs: 342

After a long time of algorithm training, we want to hold a running contest in our beautiful campus. Because all of us are curious about a coders's fierce athletic contest,so we would like a more longer athletic track so that our contest can last more .

In this problem, you can think our campus consists of some vertexes connected by roads which are undirected and make no circles, all pairs of the vertexes in our campus are connected by roads directly or indirectly, so it seems like a tree, ha ha.

We need you write a program to find out the longest athletic track in our campus. our athletic track may consist of several roads but it can't use one road more than once.

Input

*Line 1: A single integer: T represent the case number T <= 10
For each case
*Line1: N the number of vertexes in our campus 10 <= N <= 2000
*Line2~N three integers a, b, c represent there is a road between vertex a and vertex b with c meters long
1<= a,b <= N,  1<= c <= 1000;

Output

For each case only one integer represent the longest athletic track's length

Sample Input

1
7
1 2 20
2 3 10
2 4 20
4 5 10
5 6 10
4 7 40

Sample Output

80

Source: TJU Team Selection Contest 2010 (3)

解题:求树的直径。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,w,next;
arc(int x = ,int y = ,int z = -){
to = x;
w = y;
next = z;
}
};
arc e[maxn*];
int head[maxn],d[maxn],tu,tot,n;
void add(int u,int v,int w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
e[tot] = arc(u,w,head[v]);
head[v] = tot++;
}
int bfs(int u){
queue<int>q;
memset(d,-,sizeof(d));
q.push(u);
d[u] = ;
int maxlen = ;
while(!q.empty()){
u = q.front();
q.pop();
if(d[u] > maxlen) maxlen = d[tu = u];
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] > -) continue;
d[e[i].to] = d[u] + e[i].w;
q.push(e[i].to);
}
}
return maxlen;
}
int main() {
int T,u,v,w;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i = tot = ; i+ < n; ++i){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
bfs();
printf("%d\n",bfs(tu));
}
return ;
}

TOJ 3517 The longest athletic track的更多相关文章

  1. 深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track

    这个题主要考察对树的操作,主要思想是DFS或者BFS,其次是找树的直径方法(既要运用两次BFS/DFS),最后作为小白,还练习了vector的操作. DFS框架伪码: bool DSF(Node on ...

  2. Finding the Longest Palindromic Substring in Linear Time

    Finding the Longest Palindromic Substring in Linear Time Finding the Longest Palindromic Substring i ...

  3. [Swift]LeetCode409. 最长回文串 | Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. Leetcode 128. Longest Consecutive Sequence (union find)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  5. ACM-ICPC2018徐州网络赛 Features Track(二维map+01滚动)

    Features Track 31.32% 1000ms 262144K   Morgana is learning computer vision, and he likes cats, too. ...

  6. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  7. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. Longest common subsequence(LCS)

    问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

随机推荐

  1. 小程序做一个能够左右滑动切换的多tab页面

    主要原理:使用 <swiper> 和 <scroll-view> 组件 代码片段: https://developers.weixin.qq.com/s/mLx4FWmF757 ...

  2. CF1037E Trips (离线+图上构造)

    题目大意:一共有n个人,每天早上会有两个人成为朋友,朋友关系不具有传递性,晚上,它们会组织旅游,如果一个人去旅游,那么他不少于$k$个朋友也要和他去旅游,求每天的最大旅游人数 一开始并没有想到反向建图 ...

  3. 一、Git起步

    1.关于版本控制 版本控制系统:版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.但实际上,你可以对任何类型的文件进行版本控制. 1.1 本地版本控制系统 许多人习惯用复制 ...

  4. mybatis-plus注解版实现多表联查(sql)

    mybatis注解版实现多表联查 需求: 用户有角色,角色有权限,需要一次取用户信息包含角色信息及其对应权限 实体类: package cn.zytao.taosir.common.model.use ...

  5. 原生javaScript完成Ajax请求

    使用原生javaScript完成Ajax请求,首先应该创建一个对象XMLHttprequest,考虑到兼容低版本IE浏览器,使用ActiveXObject对象,代码入下: var request; i ...

  6. Mysql优化之优化工具profiling

    程序员的成长之路 2016-11-23 22:42 Mysql优化之优化工具profiling 前言 mysql优化技术: mysql优化不是做一个操作就可以的优化,它包含很多的细节,需要一点一点的优 ...

  7. UltraEdit正則表達式介绍及实例

    前几天,有个将Excel中的数据导入到数据库中的需求.原本想到用程序读取Excel中的数据并存储到数据库中,但经一哥们的提醒,说用 EditPlus或UltraEdit这种工具直接将数据拼凑成SQL插 ...

  8. Cocos2d-x使用Luajit将Lua脚本编译为bytecode,实现加密 更新

    项目要求对lua脚本进行加密,查了一下相关的资料 ,得知lua本身能够使用luac将脚本编译为字节码(bytecode)从而实现加密,试了一下,确实可行. 以下是使用原生的lua解释器编译字节码: 1 ...

  9. [CSS3] All abourt responsive image

    Take few examples: Full size image: The problem for that is it overflow when the screen size is smal ...

  10. 5.Git使用详细教程

    转自:https://www.cnblogs.com/seven-ahz/p/7712125.html 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的 ...