TOJ 3517 The longest athletic track
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的更多相关文章
- 深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track
这个题主要考察对树的操作,主要思想是DFS或者BFS,其次是找树的直径方法(既要运用两次BFS/DFS),最后作为小白,还练习了vector的操作. DFS框架伪码: bool DSF(Node on ...
- Finding the Longest Palindromic Substring in Linear Time
Finding the Longest Palindromic Substring in Linear Time Finding the Longest Palindromic Substring i ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- ACM-ICPC2018徐州网络赛 Features Track(二维map+01滚动)
Features Track 31.32% 1000ms 262144K Morgana is learning computer vision, and he likes cats, too. ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Longest common subsequence(LCS)
问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- phpstorm中,光标变成一个长方红色块,如何调回来?
今天使用phpstorm,不知道碰到了什么,光标变成长方红色块(如图),搞半天终于调回来了,变回了细细的竖线,记录一下: 其实按一下insert键就可以了
- Vue双向绑定原理(源码解析)---getter setter
Vue双向绑定原理 大部分都知道Vue是采用的是对象的get 和set方法来实现数据的双向绑定的过程,本章将讨论他是怎么利用他实现的. vue双向绑定其实是采用的观察者模式,get和s ...
- 使用Git--将本地项目提交到Github
前置工作 1. 在GitHub官网注册一个GitHub账号: 2. 安装git工具,在Git官网下载对应版本的Git: 方法一: 1. 进入Github首页,点击New repository新建一个项 ...
- HDU 1047 Integer Inquiry( 高精度加法水 )
链接:传送门 思路:高精度水题 /************************************************************************* > File ...
- 从YV12到NV12
Media SDK的decoder,vpp,encoder对输入输出格式有着严格的限制,现在仅仅支持NV12.那么如何从其他格式转化为NV12是日常工作中经常遇到的事情.本篇文章以此为目的,讨论如何将 ...
- ArcGIS探索
一.ArcGIS10概述 1.1 总览 ArcGIS是地理信息系统平台软件,主要用于创建和使用地图,编辑和管理地理数据,分析和共享地理信息,并在一系列应用中使用地图和地理信息. 功能定位: a.地图: ...
- 【codeforces 747E】Comments
[题目链接]:http://codeforces.com/problemset/problem/747/E [题意] 给你一个类似递归的结构; 让你把每一层的字符串按照层,一层层地输出出来; 并输出层 ...
- Apache Tez 0.7、0.83、 0.82 安装、调试笔记
———————————————————— 准备 Tez 编译环境 ———————————————————— 1 需要的支持 tez0.7 需要 Hadoop 2.60 以上 2 需要的 linux 相 ...
- python设计模式 之 简单工厂模式
简单工厂模式属于类的创建型模式,适合用来对大量具有共同接口的类进行实例化,它能够推迟到执行的时候才动态决定要创建哪个类的实例.而不是在编译时就必须知道要实例化哪个类. python: #!/usr/b ...
- Uncaught TypeError: undefined is not a function
index.html <script src="resources/sap-ui-core.js" id="sap-ui-bootstrap" data- ...