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 调用存储过程
    Mybatis mapper配置
    流量红包算法
    带搜索的下拉框Chosen
    生成二维码
    Linux netlink机制
    OpenFlow Switch学习笔记(七)——Matching Fields
    Hierarchical Token Bucket
    OpenvSwitch架构
    Examining Open vSwitch Traffic Patterns
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11720562.html
Copyright © 2011-2022 走看看