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();
    
    }
  • 相关阅读:
    spring+hibernate常见异常集合
    Java报错原因汇总
    java常见异常集锦
    连接池 druid(阿里巴巴的框架)
    企业支付宝账号开发接口实现
    Maven使用常见问题整理
    MySQL的分页
    Struts2中通配符的使用
    Centos下安装mysql 总结
    将linux用在开发环境中
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11720562.html
Copyright © 2011-2022 走看看