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 ...
随机推荐
- 51nod 1576 Tree and permutation(树的重心+dfn序)
乍一看我不会. 先不考虑加点. 先考虑没有那个除\(2\). 考虑每一条边的贡献,假设某一条边把这棵树分成大小为x,y的两个部分. 那么这条边最多可以被使用\(min(x,y)*2\)次(因为有进有出 ...
- BZOJ 2631 [国家集训队]Tree II (LCT)
题目大意:给你一棵树,让你维护一个数据结构,支持 边的断,连 树链上所有点点权加上某个值 树链上所有点点权乘上某个值 求树链所有点点权和 (辣鸡bzoj又是土豪题,洛谷P1501传送门) LCT裸题, ...
- NOIP2016 天天爱跑步 (树上差分+dfs)
题目大意:给你一颗树,树上每个点都有一个观察员,他们仅会在 w[i] 时刻出现,观察正在跑步的玩家 一共有m个玩家,他们分别从节点 s[i] 同时出发,以每秒跑一条边的速度,沿着到 t[i] 的唯一路 ...
- Map的四种遍历方法
1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...
- $attr和$listeners is readonly
https://www.jb51.net/article/132371.htm 出现这个问题的原因,主要是因为在使用的时候出现了A组件调用B组件,B组件再调用了C组件.而直接使用了A组件修改C组件的数 ...
- JS深拷贝拷贝的区别?
拷贝拷贝引用,共享内存 深拷贝拷贝实例,不共享内存 1. 浅拷贝:当一个对象拷贝另一个对象的数据时,只要一个对象的数据发生改变时,另一个对象的数据也会发生改变,因为浅拷贝拷贝的是引用的地址 实现方 ...
- cocos2d-x学习笔记(18)--游戏打包(windows平台)
cocos2d-x学习笔记(18)--游戏打包(windows平台) 之前做好的游戏,都是在vs2008下编译执行的.假设说想把游戏公布到网上或者和其它人一起分享游戏,那就得对游戏 ...
- [Gatsby] Install Gatsby and Scaffold a Blog
In this lesson, you’ll install Gatsby and the plugins that give the default starter the ability to t ...
- iOS开发UI调试神器----Reveal
做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做測试.用模拟器来调试UI效果尽管快捷方便,但有时仍然希望有更强大的工具来帮助分析UI,尤其是专注在UI的效果调试时.近期 ...
- Double 与 Float 的值的比較结果
首先看geeksforgeeks上的两个程序: 程序1: #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) print ...