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


  • 相关阅读:
    编译器合成的拷贝构造函数
    WIN phone 8.1 SDK 坑遇到 Hyper-V
    JDBC编程步骤
    关闭safari浏览器button默认样式
    Codeforces Round #273 (Div. 2)
    android Activity之间数据传递 Parcelable和Serializable接口的使用
    如何删除JAVA集合中的元素
    Android自定义长按事件
    关于android多点触控
    Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程
  • 原文地址:https://www.cnblogs.com/Accepting/p/11243647.html
Copyright © 2011-2022 走看看