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;
    }
  • 相关阅读:
    vijos1198:最佳课题选择
    vijos1071:新年趣事之打牌
    vijos1153:猫狗大战
    bzoj3594: [Scoi2014]方伯伯的玉米田
    bzoj2753: [SCOI2012]滑雪与时间胶囊
    bzoj1923: [Sdoi2010]外星千足虫
    bzoj2783: [JLOI2012]树
    bzoj4590: [Shoi2015]自动刷题机
    bzoj4580: [Usaco2016 Open]248
    bzoj4579: [Usaco2016 Open]Closing the Farm
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11239513.html
Copyright © 2011-2022 走看看