博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track
阅读量:5316 次
发布时间:2019-06-14

本文共 3859 字,大约阅读时间需要 12 分钟。

这个题主要考察对树的操作,主要思想是DFS或者BFS,其次是找树的直径方法(既要运用两次BFS/DFS),最后作为小白,还练习了vector的操作。

DFS框架伪码:

bool DSF(Node oneTreePoint ){   //传入的结点和其他有效信息    visited[nowPoint] = 1; //当前点已遍历标记    相关操作(随题更改)for( AllLinkNode){
//遍历与此点相连的所有节点; if ( !visited[oneLinkNode] ){ //如果没有被访问过才能继续搜索; visited[onelinkedNode] = 1; //标记已访问;    相关操作(随题更改) DSF( Node oneTreePoint); } } if(触底判断条件){
     执行操作; return true; } return true;}

vector的操作:

 

//建立一个vector数组,每个Vector中存放结构体数据类型;struct LinkNode{    int linkedPoint;    int length;};vector 
Tree[MAX];//对vector数组清空 for(int i = 0;i < MAX;i++) Tree[i].clear();//插入Tree[i].push_back( LinkNode n );

大意是给一个树,每个边的权重已知,求树的直径。

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
/* * 3517_The longest athletic track.cpp *    给定一个边长带有权值的树,求树的直径。 *  Created on: 2018年11月8日 *      Author: Jeason */#include 
#include
#include
#include
#include
using namespace std;#define MAX 2001struct LinkNode{ int linkedPoint; int length;};int findMaxLength[MAX];int findMaxPoint[MAX];int numofMax;int visited[MAX];int start = 0;vector
Tree[MAX];bool DSF(vector
oneTreePoint ,int totalLength ,int nowPoint ){ visited[nowPoint] = 1; int tmp = totalLength; for(int i = 0; i < oneTreePoint.size(); i++){ //遍历与此点相连的所有节点; if ( !visited[oneTreePoint[i].linkedPoint] ){ //如果没有被访问过才能继续搜索; visited[oneTreePoint[i].linkedPoint] = 1; //标记已访问; totalLength = tmp + oneTreePoint[i].length; //如果为符合条件点,更新当前总长;// cout << "当前点" << nowPoint << " 和 " << oneTreePoint[i].linkedPoint << "长度为" << totalLength << endl; DSF( Tree[ oneTreePoint[i].linkedPoint ], totalLength, oneTreePoint[i].linkedPoint); } } if(oneTreePoint.size() == 1){ //说明找到了边缘的子叶,执行操作; findMaxLength[start] = totalLength; //记录当前总长度; findMaxPoint[start] = nowPoint; //总长度对应的点; start++;// cout << "get bottom:"<< findMaxLength[start-1] <
findMax[m]) m = i; return m;}int main(){ int T,point_num; int a,b,ab_length; cin >> T; while(T--){ //初始化操作 start = 0; numofMax = 0; memset(findMaxLength,0,sizeof(findMaxLength)); memset(findMaxPoint,0,sizeof(findMaxPoint)); memset(visited,0,sizeof(visited)); for(int i = 0;i < MAX;i++) Tree[i].clear(); cin >> point_num; point_num--; while(point_num--){ //将数据存储在树结构中 cin >> a >> b >> ab_length; LinkNode point1; point1.linkedPoint = b; point1.length = ab_length; Tree[a].push_back( point1 ); LinkNode point2; point2.linkedPoint = a; point2.length = ab_length; Tree[b].push_back( point2 ); } DSF(Tree[2], 0 , 2 ); //从编号为1的结点开始DSF; numofMax = find(findMaxLength);// cout << "第1次结束" << ",,离2最远的点:"<< findMaxPoint[numofMax] << ";其长度:"<< findMaxLength[numofMax] <

 

转载于:https://www.cnblogs.com/JeasonIsCoding/p/9937618.html

你可能感兴趣的文章
jQuary的相关动画效果
查看>>
高级iOS面试题
查看>>
语法上的小trick
查看>>
windows下共享文件夹在Linux下打开
查看>>
【SDOI2008】仪仗队
查看>>
TrimPath - Js模板引擎
查看>>
[Swift]LeetCode1146. 快照数组 | Snapshot Array
查看>>
检查点(Checkpoint)过程如何处理未提交的事务
查看>>
IT行业 侃侃富士康
查看>>
Windows按名称排序问题
查看>>
python 生成排列、组合以及选择
查看>>
myeclipse和maven的clean和build
查看>>
SpringMVC接受JSON参数详解及常见错误总结我改
查看>>
JS里的onclick事件
查看>>
Echart..js插件渲染报错 data.length<1?
查看>>
js闭包
查看>>
06 小数据池 is 和 = = 再谈编码
查看>>
NSValue包装自定义结构体
查看>>
mac下npm/node的安装和卸载、升级;node、npm升级后最后删掉node_modules重新安装
查看>>
wget整站抓取、网站抓取功能;下载整个网站;下载网站到本地
查看>>