YunYan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. 
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area. 

The area has up to 10,000 villages connected by road segments. The villages are numbered from 1. 

Input

Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

Output

You are to output a single integer: the road distance between the two most remote villages in the area.

Sample Input

5 1 6
1 4 5
6 3 9
2 6 8
6 1 7

Sample Output

22
题目大意:就是输入点a,b以及a,b之间的距离,输出两点之间最长的距离
AC代码
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,l;
struct stu{
    int a,b;
};
int mark[10010];
vector<stu>ve[10010];
int ans=0;
int xx;


void dfs(int x,int step){
    if(step>ans){
        ans=step;
        xx=x;
    }
    for(int i=0;i<ve[x].size();i++){
        if(mark[ve[x][i].a]==0){
            mark[ve[x][i].a]=1;
            dfs(ve[x][i].a,step+ve[x][i].b);
            mark[ve[x][i].a]=0;
        }
    }
}


int main()
{
    while(scanf("%d %d %d",&n,&m,&l)!=EOF){//输入到文件结束,, 数据输入完成后要按Ctrl+z才可以输出 
        ve[n].push_back({m,l});
        ve[m].push_back({n,l});//存图 
    }
    
    memset(mark,0,sizeof(mark));
    mark[1]=1;
    dfs(1,0);
    memset(mark,0,sizeof(mark));
    mark[xx]=1;
    dfs(xx,0);
    printf("%d",ans);
    return 0;
}

 



posted on 2019-07-25 13:19  Target--fly  阅读(259)  评论(0编辑  收藏  举报