zoukankan      html  css  js  c++  java
  • 【POJ 2631】 Roads in the North

    Roads in the North
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5802   Accepted: 2731

    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

    Source

     
    题解:树形DP,经典题了,树的直径,两遍dfs即可、poj不可以用万能头文件,生气
    害我运行时错误了几次。枯了我。
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    const int N=20005;
    const int oo=0x3f3f3f3f;
    int x,y,z;
    struct node{
        int v,l,next;
    }e[N];
    int vis[N],d[N],head[N];
    int ans,k,cnt,biu;
    void add_bian(int u,int v,int l){
        cnt++; e[cnt].v=v; e[cnt].l=l;
        e[cnt].next=head[u]; head[u]=cnt;
        
        cnt++; e[cnt].v=u; e[cnt].l=l;
        e[cnt].next=head[v]; head[v]=cnt;
    }
    
    void dfs(int u,int t){
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].v;
            if(vis[v]==0){
                vis[v]=1;
                d[v]=t+e[i].l;
                if(d[v]>ans) 
                   { ans=d[v]; biu=v; }
                dfs(v,d[v]);
            }
        }
    }
    
    int main(){
        freopen("2631.in","r",stdin);
        freopen("2631.out","w",stdout);
        while(scanf("%d %d %d",&x,&y,&z)!=EOF)
            add_bian(x,y,z);
            
        memset(vis,0,sizeof(vis));
        vis[1]=1; ans=0; dfs(1,0);
        
        memset(vis,0,sizeof(vis));
        vis[biu]=1; ans=0; dfs(biu,0);
        
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    php正则表达式验证(邮件地址、Url地址、电话号码、邮政编码)
    laravel 事件广播
    windows apache 配置多个服务 站点 Apache Service Monitor
    apache配置多站点
    querySelector和querySelectorAll方法介绍
    document.domain与js跨域的问题
    用JS判断用户使用的是手机端还是pc端访问
    下载的firebug-lite压缩包的调用方法
    JS实现常用的分享到按钮
    转:不会定义jQuery插件,不要说会jQuery
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11239513.html
Copyright © 2011-2022 走看看