zoukankan      html  css  js  c++  java
  • poj 1849 Two

                                  Two
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 1684   Accepted: 891

    Description

    The city consists of intersections and streets that connect them. 

    Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections. 

    The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection. 

    Write a program that calculates the total amount of fuel that the snow plovers will spend. 

    Input

    The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N. 

    Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000. 

    Output

    Write to the output the minimal amount of fuel needed to clean all streets.

    Sample Input

    5 2
    1 2 1
    2 3 2
    3 4 2
    4 5 1
    

    Sample Output

    6

    Source

    题目大意
    一棵有n个点的带权无向树,在s点有两个机器人,要求机器人把树上所有路径走一遍但不要求会到起始点,
    问机器人所走的距离之和最小是多少
     
    我们考虑从一个点遍历树最后回到出发点,那么所有边都会走两边。若出发后不用回到起点,那么最后会
    有一条 链接出发点与最后一次遍历到的叶节点的边只需要遍历一边。两个机器人走完全不同的,那么每条边最
    多会遍历两边。 那么只遍历一遍的距离就是两机器人最后分别遍历到的叶节点之间的距离。
    求树上两点之间距离最长(求树的直径)
    我们用边的总长*2-树的直径即为ans
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define N 100050
    int n,m;
    int num,head[N];
    struct node{
        int v,w,next;
    }edge[N];
    int sum=0,d=0;int tr;
    void add_edge(int u,int v,int w){
        edge[++num].v=v;edge[num].w=w;edge[num].next=head[u];head[u]=num;
    }
    void dfs(int u,int fa,int far)
    {
        if(far>d)d=far,tr=u;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v!=fa)
            dfs(v,u,far+edge[i].w);
        }
    }
    int main(){
        scanf("%d%d",&n,&m);
        int a,b,c;
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
            sum+=(c<<1);
        }
        dfs(m,-1,0);
        dfs(tr,-1,0);
        printf("%d
    ",sum-d);
    }
  • 相关阅读:
    nginx
    同步和异步
    什么是ACID
    关系型数据库特点小结
    js 简单小知识
    join 和 left join 和 right join的区别?
    mysql中having和where区别?
    include和require区别
    什么是脏读,不可重复,幻读?
    Mysql常见面试题
  • 原文地址:https://www.cnblogs.com/sssy/p/7325092.html
Copyright © 2011-2022 走看看