zoukankan      html  css  js  c++  java
  • Roads in the North(POJ 2631 DFS)

    Description

    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
    给定一颗棵树,求最长路径
    思路:随机找一个节点u,DFS求出u的最远点m,然后再DFS求出m的最远点n,之后m-n就是最长路径

    2.还可以用DP写,d(i)表示以i为根节点的最大路径值,=max(d(j)+1),j为i的子节点,取出最大和次大的d(j) +2;
     1 #include <cstring>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <vector>
     6 #define Max 10005
     7 using namespace std;
     8 vector <int> tree[Max],len[Max];
     9 int d[Max],vis[Max];
    10 bool flag=0;
    11 int an;
    12 int dfs(int node,int &ans)
    13 {
    14     int i,j;
    15     int maxn=0;
    16     int t,index;
    17     vis[node]=1;
    18     for(i=0;i<tree[node].size();i++)
    19     {
    20         if(vis[tree[node][i]])
    21             continue;
    22         dfs(tree[node][i],t);
    23         if((t+len[node][i])>maxn)
    24         {
    25         //    cout<<t<<endl;
    26             maxn=t+len[node][i];
    27             index=i;
    28         }
    29     }
    30     ans=maxn;
    31     return i;
    32 }
    33 void dfs1(int node,int sum)
    34 {
    35     int i;
    36     vis[node]=1;
    37     if(flag)
    38         return;
    39     if(sum==0)
    40     {
    41         flag=1;
    42         an=node;
    43         return;
    44     }
    45     for(i=0;i<tree[node].size();i++)
    46     {
    47         if(sum>=len[node][i]&&vis[tree[node][i]]==0)
    48             dfs1(tree[node][i],sum-len[node][i]);
    49     }
    50 }
    51 int main()
    52 {
    53     int i,j;
    54     int a,b,val,p=0;
    55     freopen("in.txt","r",stdin);
    56     bool flag=0;
    57     for(i=0;i<Max;i++)
    58         tree[i].clear(),len[i].clear();
    59     while(scanf("%d%d%d",&a,&b,&val)!=EOF)
    60     {
    61         tree[a].push_back(b);
    62         tree[b].push_back(a);
    63         len[a].push_back(val);
    64         len[b].push_back(val);
    65     }
    66     memset(vis,0,sizeof(vis));
    67     dfs(1,p);
    68     memset(vis,0,sizeof(vis));
    69     dfs1(1,p);
    70     memset(vis,0,sizeof(vis));
    71     dfs(an,p);
    72     cout<<p<<endl;
    73 }


  • 相关阅读:
    GitHub教程学习笔记1本地Git管理
    GitHub教程学习笔记2远程仓库和本地仓库
    图像分割方法综述
    println输出乱码
    ngshow,ngif区别
    isFile() exists() isDirectory()的区别
    File类中的list和listFiles方法
    mkdir与mkdirs的区别
    新建play项目eclipsify后导入eclipse后无法debug调试
    大龄屌丝自学笔记Java零基础到菜鸟021
  • 原文地址:https://www.cnblogs.com/a1225234/p/5240063.html
Copyright © 2011-2022 走看看