zoukankan      html  css  js  c++  java
  • poj 1849 Two(最长链)

    Two
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 1394   Accepted: 705

    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

     
    题意
    给出一棵树和根节点
    有两个人从根节点出发遍历整棵树求最小路径
     
    根据观察可以得出答案为 所有路径长度和*2-最长链
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define maxn 100010
    using namespace std;
    int n,root,topt,ans,mx;
    int first[maxn];
    struct edge
    {
        int to;
        int val;
        int next;
    }e[maxn*2];
    void add(int x,int y,int z)
    {
        topt++;
        e[topt].to=y;
        e[topt].val=z;
        e[topt].next=first[x];
        first[x]=topt;
    }
    int dfs(int now,int from)
    {
        int maxx=0,maxt=0;
        for(int i=first[now];i;i=e[i].next)
        {
            int to=e[i].to;
            if(to==from)continue;
            int ha=e[i].val+dfs(to,now);
            if(ha>maxx)maxt=maxx,maxx=ha;
            else if(ha>maxt)maxt=ha;
        }
        mx=max(mx,maxx+maxt);
        return maxx;
    }
    int main()
    {
        scanf("%d%d",&n,&root);
        for(int i=1;i<n;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);add(y,x,z);
            ans+=z*2;
        }
        dfs(root,root);
        printf("%d",ans-mx);
        return 0;
    } 
     
  • 相关阅读:
    CentOS 安装Redis
    python中Url链接编码处理(urlencode,urldecode)
    Flask+mongodb 实现简易个人博客
    Flask中mongodb实现flask_login保持登录
    ubuntu环境变量添加变量
    终端执行python shell的方法
    简单的模拟登录Wap版新浪微博
    爬取淘宝模特信息并自动保存图片
    Python字符串的encode与decode
    python3 安装scrapy Exception: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 1006, in check_if_exists解决方法
  • 原文地址:https://www.cnblogs.com/dingmenghao/p/6001686.html
Copyright © 2011-2022 走看看