日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/9 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度优先搜索(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

總結

以上是生活随笔為你收集整理的深度优先搜索(DFS)----------------Tju_Oj_3517The longest athletic track的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。