zoukankan      html  css  js  c++  java
  • poj1849(求树的直径)

    题目链接:http://poj.org/problem?id=1849

    题意:有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, 但是最后机器人不要求回到出发点. 问你两个机器人走的路总长之和的最小值是多少?

    分析:如果从某点出发遍历完一棵树再回来,那么所有边都会走两遍,而从某点有两个机器人出发去遍历,因为不用回来,所以最后那两个人距离越远越好,可以从树的直径上某个点背道而驰,那么这段距离(树的直径)只走了一遍,其他的要走两遍,所以ans=sum*2-len(总边长的和*2-树的直径)。

    加强版hdu4003扩展成有k个机器人求最值.

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define N 100010
    #define clr(a) (memset(a,0,sizeof(a)))
    using namespace std;
    struct edge
    {
        int v,w,next;
        edge(){}
        edge(int v,int w,int next):v(v),w(w),next(next){}
    }e[2*N];
    int head[N],tot,n,m,mx,s;
    void addedge(int u,int v,int w)
    {
        e[tot]=edge(v,w,head[u]);
        head[u]=tot++;
    }
    void dfs(int u,int fa,int len)
    {
        if(len>mx)mx=len,s=u;
        for(int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].v,w=e[i].w;
            if(v==fa)continue;
            dfs(v,u,len+w);
        }
    }
    int main()
    {
        int u,v,w,sum;
        while(scanf("%d%d",&n,&m)>0)
        {
            memset(head,-1,sizeof(head));
            tot=0;sum=0;
            for(int i=1;i<n;i++)
            {
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
                sum+=w*2;
            }
            mx=0;
            dfs(m,-1,0);
            dfs(s,-1,0);
            printf("%d
    ",sum-mx);
        }
    }
    View Code
  • 相关阅读:
    python 复杂一点的装饰器
    python 装饰器
    python 歌词解析
    ATX 免越狱调试IOS和Android
    OpenCV库文件介绍
    NetEaseGame/ATX 的MD
    带你玩转Visual Studio——带你高效开发
    python 图像识别
    fatal: Authentication failed for又不弹出用户名和密码 解决办法
    lua luna工具库
  • 原文地址:https://www.cnblogs.com/lienus/p/4204692.html
Copyright © 2011-2022 走看看