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


  • 相关阅读:
    [bzoj3218] a+b problem [最小割+数据结构优化建图]
    [bzoj3456] 城市规划 [递推+多项式求逆]
    [ARC068F] Solitaire [DP]
    [bzoj3601] 一个人的数论 [莫比乌斯反演+高斯消元]
    [中山市选2011][bzoj2440] 完全平方数 [二分+莫比乌斯容斥]
    [bzoj2159] Crash的文明世界 [斯特林数+树形dp]
    [bzoj2839] 集合计数
    通用解题方法—回溯法
    分支限界法—单源最短路径问题
    分支限界法
  • 原文地址:https://www.cnblogs.com/Accepting/p/11243647.html
Copyright © 2011-2022 走看看