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. 一些BFC

    我们可能会遇到这样的一些问题,比如:子元素浮动,父元素高度塌陷:父元素跟随子元素一起移动等 这是我们可以通过触发BFC来解决这样的问题. BFC为"块级格式化上下文".它是一个独立 ...

  2. UNIX系统高级编程——第四章-文件和目录-总结

    文件系统: 以UNIX系统V文件系统为例: 磁盘分为区,每个分区都有自己的文件系统: ​ i节点是固定长度的记录项,包含了文件的相关信息.目录项包含文件名和i节点号.stat结构中除文件名和i节点编号 ...

  3. .net 参数修饰符

    参数修饰符的作用 参数修饰符 作用 无 如果一个参数没有用参数修饰符标记,则认为它将按值传递(pass by value),这意味着被调用的方法收到原始数据的一份副本 out 输出参数由被调用的方法赋 ...

  4. Action访问ServletAPI的三种方式

    一.前言 Struts是一种基于MVC设计模式的web应用框架,主要担任C的角色,用于分离页面显示和业务逻辑处理,那其实在我们学习jsp的时候学过一个具有类似功能的东西——servlet.其实Stru ...

  5. android启动第一个界面时即闪屏的核心代码(两种方式)

    闪屏,就是SplashScreen,也能够说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后.自己主动关闭.  第一种方式: android的实现很easy,使用Handler对象的postDe ...

  6. (hdu step 7.1.6)最大三角形(凸包的应用——在n个点中找到3个点,它们所形成的三角形面积最大)

    题目: 最大三角形 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. 从ORA-27300,ORA-27301到ORA-00064

        近期因为session数量添加,须要调整session,也就是要调整process參数. 看是比較简单的一个问题,却遭遇了ORA-27300,ORA-27301.因为这个涉及到了有关内核參数k ...

  8. web前端project师知识汇总

    分类: Web开发应用  一.何为Web前端project师?           前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...

  9. uva 10641 (来当雷锋的这回....)

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...

  10. CentOS7系统安装完MySQL后启动MySQL提示无服务

    重新安装MariaDB数据库即可解决,MySQL所有命令可通用 MariaDB MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,开发这个分支的原因之一是:甲骨文公司收购了My ...