POJ1849Two[DP|樹的直徑](擴充套件HDU4003待辦)

Candy?發表於2016-08-26
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]的含義是一個下去又上來,其他的下去不上來

以後做做吧

 

相關文章