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 2001

struct 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

时间: 2024-11-06 09:34:31

Tju_Oj_3517The longest athletic track的相关文章

TOJ 3517 The longest athletic track

3517.   The longest athletic track Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 880   Accepted Runs: 342 After a long time of algorithm training, we want to hold a running contest in our beautiful campus. Because all of us are curious ab

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

LeetCode 之 Longest Valid Parentheses(栈)

[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another exa

Leetcode 128. Longest Consecutive Sequence (union find)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence i

[Swift]LeetCode409. 最长回文串 | Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the lengt

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关