zoukankan      html  css  js  c++  java
  • POJ

    http://poj.org/problem?id=1985

    Description

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms. 
    就是求树的直径

    树形dp法
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    
    #define ri register int
    #define u int
    #define NN 500005
    #define MM 500005
    
    namespace all {
    
        u N,M,ans,mxl[NN];
    
        u cnt,h[NN];
        struct node {
            u to,next,va;
        } a[MM<<1];
        inline void add(const u &x,const u &y,const u &z) {
            a[++cnt].next=h[x],a[cnt].to=y,a[cnt].va=z,h[x]=cnt;
        }
    
        void dfs(const u &x,const u &prt) {
            for(ri i(h[x]); i; i=a[i].next) {
                u _y(a[i].to);
                if(_y^prt) {
                    dfs(_y,x);
                    ans=std::max(ans,mxl[x]+mxl[_y]+a[i].va);
                    mxl[x]=std::max(mxl[_y]+a[i].va,mxl[x]);
                }
            }
        }
    
        inline void solve() {
            scanf("%d%d",&N,&M);
            for(ri i(1); i<=M; ++i) {
                u _a,_b,_c;
                char _s;
                scanf("%d%d%d %c",&_a,&_b,&_c,&_s);//毒瘤读入,输入的字母对此题没有卵用 
                add(_a,_b,_c),add(_b,_a,_c);
            }
            dfs(1,0);
            printf("%d",ans);
        }
    
    }
    
    int main() {
    
        //freopen("x.txt","r",stdin);
        all::solve();
    
    }
  • 相关阅读:
    #考研碎碎念#
    #考研笔记#计算机之病毒
    #考研笔记#计算机之多媒体应用
    #考研笔记#计算机之PPT问题
    第六章深入理解类
    第五章方法
    类的基本教程
    类型存储变量
    C#和.net框架
    C#编程概述
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11720562.html
Copyright © 2011-2022 走看看