深度優先搜索(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.pswp.cn/news/278967.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278967.shtml
英文地址,請注明出處:http://en.pswp.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 早上心態很好的繼續刷…

Linux決心書/李世超

Linux決心書大家好&#xff0c;我叫李世超&#xff0c;來自河北邯鄲。今年24&#xff0c;感覺之前的生活狀態不是自己想要的&#xff0c;每天渾渾噩噩。我覺得人要對自己定一個目標&#xff0c;我的目標就是月薪10K以上&#xff0c;所以我要努力在老男孩教育學習技術。珍惜這五…

linux下設備或資源忙,linux刪除文件目錄 目錄設備或資源忙怎么辦

linux刪除文件目錄 目錄設備或資源忙怎么辦來源&#xff1a;未知作者&#xff1a;老黑時間&#xff1a;09-09-21【打印】[rootrs swms]# rmdir zpggrmdir: ‘zpgg’: 設備或資源忙相關服務都已經停止掉了&#xff0c;有什么辦法強制刪除嗎&#xff1f;你可以在有windows的硬盤上…

Codeforces 1066 C(思維)

傳送門&#xff1a; 題面&#xff1a; C. Books Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have got a shelf and want to put some books on it. You are given qq queries of three type…

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…

uefi模式下win10安裝雙系統ubuntu18.04LTS

自己折騰了半天&#xff0c;血與淚啊&#xff08;難得一個可愛的周末 wwww我一定要寫下來 跟這個博客幾乎一模一樣了 https://blog.csdn.net/xrinosvip/article/details/80428133 我的電腦型號&#xff1a;戴爾G3 默認uefi模式&#xff0c;按f2進入的bios界面是新版跟教程上的不…

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. 如果您曾經在…

WinRAR 5.40 4.20 3.93 的注冊碼 - rarreg.key

把下面的數據復制到“記事本”中,用文件名“rarreg.key”命名該文件&#xff0c;保存到WinRAR安裝文件夾即完成注冊。以下4個Key隨便選一個復制都可以。WinRAR 5.40 版Key&#xff0c;復制箭頭中間內容&#xff0c;上下無空格。&#xff08;5.00版的Key 4.X和之前的3.X版本也能…

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…

django自定義用戶表

django自帶了用戶表。 -- auto-generated definition create table auth_user (id int auto_incrementprimary key,password varchar(128) not null,last_login datetime(6) null,is_superuser tinyint(1) not null,username varchar(150) not null,fir…

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可以找到了): …

STM32的FLASH ID加密

#define FLASH_ID_OFFSET 30000 //任意定義一個數 //把地址直接減去或者加上一個數是不要程序中直接出現這個地址 volatile u32 Flash_ID_addr[3]{ 0x1FFFF7E8 - FLASH_ID_OFFSET, 0x1FFFF7EC FLASH_ID_OFFSET, 0x1FFFF7F0 - FLASH_ID_OFFSET }; /**讀取STM32 FLASH ID*…

linux c視頻如何加水印,如何在Kdenlive的視頻上進行水印 | MOS86

如果你這些東西被稱為水印。他們So&#xff0c;你如何在Linux中創建水印&#xff1f;嗯&#xff0c;你這可能是Linux上最強大的開源視頻編輯器。Installation如果您尚未安裝Kdenlive&#xff0c;您應該可以在包裹管理器中找到它。在Ubuntu中&#xff0c;您還可以使用命令:sudo …