zoukankan      html  css  js  c++  java
  • POJ1849Two[DP|树的直径](扩展HDU4003待办)

    Two
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 1390   Accepted: 701

    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

    DP也可以,f[i]和g[i]分别处理两个人子树i进去回来和只进不回,f[i]=sum{w of i's son}*2,g[i]两种情况,两人进入i的同一个或不同一个孩子,好麻烦啊

    其实答案就是sum{w}*2-w直径,无论从哪里开始都可以

    证明:

    虽然s是起点(很多人的起点就忽略了这个),

    s在直径上好说,

    假设s不再直径上,我们选择直径某点为root,从s到root到话费也是w*2,和从root开始一个到s又回来是一样的,然后就和上面一样了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=1e5+5;
    int n,s,u,v,w,sum=0,mx=0;
    struct edge{
        int v,ne,w;
    }e[N*2];
    int h[N],cnt=0;
    inline void ins(int u,int v,int w){
        cnt++;
        e[cnt].ne=h[u];e[cnt].v=v;e[cnt].w=w;h[u]=cnt;
        cnt++;
        e[cnt].ne=h[v];e[cnt].v=u;e[cnt].w=w;h[v]=cnt;
    }
    int f[N][2];
    int dp(int u,int fa){
        int &t0=f[u][0],&t1=f[u][1];
        if(t0!=-1) return t0;
        t0=0;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v;
            if(v==fa) continue;
            int d=dp(v,u)+e[i].w;
            if(d>t0) t1=t0,t0=d;
            else if(d>t1) t1=d;
        }
        return t0;
    }
    int main(int argc, const char * argv[]) {
        scanf("%d%d",&n,&s);
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&u,&v,&w);
            ins(u,v,w);sum+=w*2;
        }
        memset(f,-1,sizeof(f));
        dp(1,-1);
        for(int i=1;i<=n;i++)
            mx=max(mx,f[i][0]+f[i][1]);
        cout<<sum-mx;
        //printf("
    
    %d %d",sum,mx);
        return 0;
    }

    扩展:2-->k 

    HDU4003(HDU最近挂了)

    和选课很像了,这里特殊之处是d[i][0]的含义是一个下去又上来,其他的下去不上来

    以后做做吧

  • 相关阅读:
    [易语言] 六边形扫雷游戏实战开发
    [web开发] 利用微信小程序开发上海大学失物招领平台
    [web开发] Vue + spring boot + echart 微博爬虫展示平台
    [web开发] Vue+Spring Boot 上海大学预约系统开发记录
    [神经网络]一步一步使用Mobile-Net完成视觉识别(一)
    Python中操作ini配置文件
    python操作mySQL数据库
    使用python和selenium写一个百度搜索的case
    功能测试的过程中有关数据安全性的检查点
    python主流测试框架的简介
  • 原文地址:https://www.cnblogs.com/candy99/p/5810982.html
Copyright © 2011-2022 走看看