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();
    
    }
  • 相关阅读:
    文件限额
    Shell命令
    HDFS基本概念
    hadoop学习
    CentOS 5.6怎么安装MongoDB数据库?
    RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x
    centos6.x yum 安装 mysql5.6 mysql5.7
    Centos6.4环境下DNS服务器的搭建
    CentOS系统中使用iptables设置端口转发
    通过WEB网管登录
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11720562.html
Copyright © 2011-2022 走看看