zoukankan      html  css  js  c++  java
  • C

    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;
    }


  • 相关阅读:
    Hadoop
    java获取系统指定时间年月日
    JS获取系统的指定定年月日
    nodetree中 前面复选框禁用插件
    JS生成指定长度的随机数
    Post的请求案例
    Ajax的简单请求案例
    from 表单提交
    Oracle中添加视图
    java double保留小数点的零的问题,java保留小数点问题
  • 原文地址:https://www.cnblogs.com/Accepting/p/11243647.html
Copyright © 2011-2022 走看看