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("\n\n%d %d",sum,mx);
    return 0;
}

 

 

 

扩展:2-->k 

HDU4003(HDU最近挂了)

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

以后做做吧

 

转载于:https://www.cnblogs.com/candy99/p/5810982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值