BFS 3517. The longest athletic track
来源:互联网 发布:淘宝客qq群推广赚钱吗 编辑:程序博客网 时间:2024/05/20 03:41
解题思路:
先随便确定一个顶点,则从该顶点广搜到的最远的点一定是该图的一个起始点,则再从该点出发,广搜。
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
#include<iostream>#include<cstring>using namespace std;const int num=2000;int n,visit[num],map[num][num],x,y,maxx;struct{int a,sum;}q[num];int bfs(int begin){int max=0,top=1,k;memset(visit,0,sizeof(visit));visit[begin]=1;q[0].a=begin;q[0].sum=0;maxx=0;for(int i=0;i<top;i++){int temp=q[i].a;//i为当前点在栈中所处位置for(int j=1;j<=n;j++)//j为从当前点出发要入栈的点的num{if(map[temp][j]!=-1&&!visit[j]){visit[j]=1;q[top].a=j;//top++;q[top].sum=q[i].sum+map[temp][j];if(q[top].sum>maxx){maxx=q[top].sum;k=j;}top++;}}}return k;}int main(){int t,a,b,c;cin>>t;while(t--){cin>>n;memset(map,-1,sizeof(map));for(int i=0;i<n-1;i++){cin>>a>>b>>c;map[a][b]=map[b][a]=c;}int v=bfs(1);v=bfs(v);cout<<maxx<<endl;}return 0;}
- BFS 3517. The longest athletic track
- 【BFS】The longest athletic track
- TOJ 3517 The longest athletic track
- TOJ 3517 The longest athletic track(树直径)
- THE TRACK
- OpenCL on the Fast Track
- Keep track of the median?
- Track the bootstrap by codes
- track
- Overview of the TREC-2011 Microblog Track
- The Longest Prefix
- The Longest increasing subsequence
- The Longest Straight
- CheckIO The Longest Palindromic
- FZU2216 The Longest Straight
- The Longest Straight FZU
- The xor-longest Path
- UVA 11646 - Athletics Track || UVA 11817 - Tunnelling the Earth 几何
- EEPlat订单管理系统开发(二)之__初识面板配置
- zigbee路由杂谈
- iOS多线程中performSelector: 和dispatch_time的不同
- 使用svn diff的-r参数的来比较任意两个版本的差异
- one way or two way location tdoa rtof
- BFS 3517. The longest athletic track
- 线程安全的单例模式的几种实现方法分享
- java基础(集合)
- java函数参数类型后添加三点的用法
- 二叉树各种递归非递归遍历
- 定义函数要return返回时需要注意
- MFC各种坐标转换
- 直接启动一个程序
- Android 滑动效果入门篇(一)—— ViewFlipper