深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track

这个题主要考察对树的操作,主要思想是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 <LinkNode> 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

1
7
1 2 20
2 3 10
2 4 20
4 5 10
5 6 10
4 7 40

Sample Output

80


/** 3517_The longest athletic track.cpp*    给定一个边长带有权值的树,求树的直径。*  Created on: 2018年11月8日*      Author: Jeason*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
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 <LinkNode> Tree[MAX];bool DSF(vector <LinkNode> 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] <<endl;return true;}//    cout << "finsh at:"<< nowPoint << endl;return true;
}int find(int findMax[MAX])
{int m = 0;for( int i = 0; i <= MAX; i++ )if( findMax[i] > 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] <<endl;int tempPoint = findMaxPoint[numofMax];memset(findMaxLength,0,sizeof(findMaxLength));memset(findMaxPoint,0,sizeof(findMaxPoint));memset(visited,0,sizeof(visited));DSF(Tree[tempPoint], 0, tempPoint );numofMax = find(findMaxLength);
//        cout << "第2次结束,离" << findMaxPoint[numofMax] << "最远的点:"<< findMaxPoint[numofMax] << ";其长度:"<< findMaxLength[numofMax] <<endl;cout << findMaxLength[numofMax] << endl;}}

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278967.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

word中图片超出页边距_如何在Word中更改页边距

word中图片超出页边距Word documents open with one-inch margins by default. You can adjust the page margins by choosing one of Word’s predefined options, or you can specify the exact height and width of the margins yourself. Here’s how. 默认情况下&#xff…

Android 中文 API (16) —— AnalogClock

一、结构 public class AnalogClock extends View java.lang.Object android.view.View android.widget.AnalogClock 二、类概述 这是一个带有时针和分针的模拟时钟控件。 三、受保护的方法 protected void onAttachedToWindow () 视图&#xff08;AnalogClock&#xff09;附在…

linux服务器探针软件,服务器安装ServerStatus监控探针教程

前言本文将介绍在服务器上安装ServerStatus来监控多台服务器的运行状态的教程.ServerStatus-Toyo版是一个酷炫高逼格的云探针、云监控、服务器云监控、多服务器探针~&#xff0c;该云监控(云探针)ServerStatus-Toyo项目链接本文为Stille原创文章.经实践,测试,整理发布.如需转载…

iphone播客怎么上传_如何在iPhone,iPad或Android上收听播客

iphone播客怎么上传Khamosh PathakKhamosh PathakDid someone recently recommend you listen to a podcast? If your response was, “What’s a podcast?” we’ve got the answer, and more! Here’s a crash course on podcasts and how you can listen to them on your …

NOIP2018 退役记

NOIP挂完&#xff0c;OI再见 AFO Day 0 早上的高铁&#xff0c;1点多到广州&#xff0c;2点多到酒店&#xff0c;下午就是颓颓颓&#xff0c;然后晚上随便刷了一下板子&#xff0c;反正PJ也没啥板子可以刷 就这样浪费了一天&#xff0c;我到底在干嘛 Day 1 早上心态很好的继续刷…

outlook默认签名设置_如何将默认签名添加到Outlook会议请求

outlook默认签名设置An odd quirk in Outlook is the inability to add a default signature to meeting requests. Here’s a quick and simple way to set up a one-click solution that avoids cutting and pasting every time you create a meeting. Outlook中的一个奇怪问…

技嘉 linux设置u盘启动项,技嘉主板bios设置u盘启动教程

对于想要重装系统的朋友来说&#xff0c;进bios一直是最大的难关&#xff0c;对于技嘉主板来说尤为复杂&#xff0c;下面小编就详细给大家介绍一下技嘉主板bios设置u盘启动的方法。方法一&#xff1a;使用u盘启动快捷键直接进入u盘装系统1、技嘉主板u盘启动快捷键是F12&#xf…

outlook日历不显示_如何在Outlook Online中突出显示不同的日历

outlook日历不显示If you’ve ever displayed multiple calendars in one view in Outlook Online, you’ll know how useful it is but also how confusing it can get. Use colors and charms to know at a glance which appointment belongs to which calendar. 如果您曾经在…

linux 下eclipse调试程序,文章2 Linux安装Eclipse阅读及调试程序

由于安装Eclipse需要Java环境&#xff0c;还需要配置环境&#xff0c;非常复杂&#xff0c;建议安装系统时&#xff0c;选择上Eclipse开发工具但是安装的Eclipse中没有CDT。首先给Eclipse安装一个CDT。1.安装CDTEclipse菜单栏help----Install New Software.从Available Softwar…

Redis学习笔记~分布式的Pub/Sub模式

redis的客户端有很多&#xff0c;这次用它的pub/sub发布与订阅我选择了StackExchange.Redis&#xff0c;发布与订阅大家应该很清楚了&#xff0c;首先一个订阅者&#xff0c;订阅一个服务&#xff0c;服务执行一些处理程序&#xff08;可能是写个日志&#xff0c;插入个数据&am…

easyui关机图标_如何在Windows 10中创建关机图标

easyui关机图标It’s true that shutting down your Windows 10 PC the old-fashioned way only takes three clicks. But why spend the extra energy when you can do it in two? All you have to do is create a shutdown icon, and you’ll save yourself some time. 的确…

Struts2+JFreeChart

下面以边帖图片和代码的方式来讲解Struts2与JFreeChart的整合。搭建环境&#xff1a;首先帖一张工程的目录结构以及所需的jar包。注意:如果你不打算自己写ChartResult的话只需要引入struts2-jfreechart-plugin-2.0.6.jar(这个在struts-2.0.6-all.zip可以找到了): …

linux c视频如何加水印,如何在Kdenlive的视频上进行水印 | MOS86

如果你这些东西被称为水印。他们So&#xff0c;你如何在Linux中创建水印&#xff1f;嗯&#xff0c;你这可能是Linux上最强大的开源视频编辑器。Installation如果您尚未安装Kdenlive&#xff0c;您应该可以在包裹管理器中找到它。在Ubuntu中&#xff0c;您还可以使用命令:sudo …

mac触控板手势无法使用_如何在iPad上使用触控板手势

mac触控板手势无法使用Apple苹果Apple’s new floating Magic Keyboard case for the iPad Pro looks fantastic, but you don’t need to spend $299 to use a trackpad. Simply connect a Magic Trackpad or a third-party multi-touch trackpad to get access to all of iPa…

安装SQLserver2008

双击点击setup&#xff0c;以管理员身份运行&#xff1b; 点击安装-》全新SQLServer独立安装或向现有安装添加功能 选择下一步&#xff0c;然后点击具有高级服务的express版本&#xff0c;点击下一步&#xff1b; 点击选择我接受许可条款&#xff0c;然后继续点击下一步&#x…

如何在没有Word的情况下打开Microsoft Word文档

Microsoft Word is part of Microsoft Office and requires an up-front purchase or a Microsoft 365 subscription. If you’re using a computer without Word installed, there are other ways to view that DOCX or DOC file. Microsoft Word是Microsoft Office的一部分&a…

数组去重的4种方法(Which one is the fastest???嘻嘻嘻....)

<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <title>Document</title> </head> <body> <input type"button" value"数组去重1" οnclick"show()"&g…

windows复制文件路径_如何在Windows 10上复制文件的完整路径

windows复制文件路径Sometimes, it’s handy to copy the full path of a file or folder in Windows 10 to the clipboard. That way, you can paste the path into an open or upload dialog quickly without having to browse for it the file. Luckily, there’s an easy w…

用c语言复制字符串的元音字母,急求:编写程序,将一个字符串中的元音字母复制到另一个字符串,然后输出。...

#include#includevoid str(char a[100],char b[100]){int i0, j0;while(a[i]!\0)//\0代表ASCLL码0的字符&#xff0c;即是一个空操作符也就是是结束符;{if(a[i]a||a[i]e||a[i]i||a[i]o||a[i]u){b[j]a[i];j;}i;}}int main(){char a[100];char b[100];gets(a);str(a,b);puts(b);r…

05 替换空格

题目描述&#xff1a; 请实现一个函数&#xff0c;将一个字符串中的每个空格替换成“%20”。例如&#xff0c;当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 解题思路有&#xff1a; #判断字符串是否为空&#xff0c;判断length是否大于0。 #记录空格的数…