TOJ 3517 The longest athletic track
来源:互联网 发布:知乎 守夜人原画 编辑:程序博客网 时间:2024/05/08 19:12
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
171 2 202 3 102 4 204 5 105 6 104 7 40
Sample Output
80
分析:求树的直径(最长路)的模板题。
Code:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))using namespace std;const int maxn=2005;const int inf=0x3f3f3f3f;int Map[maxn][maxn],sum[maxn];bool vis[maxn];int ans,n;int bfs(int st) { memset(vis,false,sizeof(vis)); vis[st]=true; sum[st]=0; queue<int>Q; Q.push(st); ans=0; int key; while(!Q.empty()) { int cur=Q.front(); Q.pop(); for(int i=1;i<=n;i++) { if(!vis[i]&&Map[cur][i]<inf) { vis[i]=true; sum[i]=sum[cur]+Map[cur][i]; Q.push(i); if(sum[i]>ans) { ans=sum[i]; key=i; } } } } return key;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) { Map[i][i]=inf; for(int j=i+1;j<=n;j++) Map[i][j]=Map[j][i]=inf; } int u,v,w; for(int i=1;i<n;i++) { scanf("%d %d %d",&u,&v,&w); Map[u][v]=Map[v][u]=w; } int p=bfs(1); bfs(p); printf("%d\n",ans); } return 0;}
- TOJ 3517 The longest athletic track
- TOJ 3517 The longest athletic track(树直径)
- 【BFS】The longest athletic track
- BFS 3517. The longest athletic track
- THE TRACK
- toj 1765. Longest Ordered Subsequence
- TOJ 1765. Longest Ordered Subsequence
- TOJ 1782.The jackpot
- TOJ 2917.The Watchman
- OpenCL on the Fast Track
- Keep track of the median?
- Track the bootstrap by codes
- track
- TOJ 2429 Find the Clones
- Toj 3016 Decode the Strings
- TOJ 2034. Just the Facts
- TOJ 2076. The Drunk Jailer
- TOJ 2968. Find the Diagonal
- n个人有c个魔法帽几天去掉所有的帽子
- 术团队新官上任之基层篇
- 如何给word 文章的每段段尾添加 脚注
- c#读取json,并序列化
- Ajax解析之一——XMLHTTPRequest对象的方法和属性
- TOJ 3517 The longest athletic track
- Missing artifact com.sun:tools:jar:1.5.0:system 解决方法
- 设计模式:如何使用观测者模式实现监控和推送
- JSP Tag实现定义 java文件
- POJ 2104 & HDU 2665 K-th Number (划分树)
- 习题4.1
- Servlet:实现多个文件上传,上传中文文件乱码解决办法
- 串口SerialPort编程(C#)
- QT中QSettings 的用法 保存窗口的位置 大小