poj-1839-Two 树形dp/树的直径


Two
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1260 Accepted: 629

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, 将问题转化成求所有边权和乘2减去直径的思路要理解。
贪心的思想, 要让两人一起走过的路经最长。


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;
const int maxn=1e6+7;
#define ll long long
#define rep(a, b) for(int i=a;i<b;i++)
#define INF 1e9+7
int n, m;
int d[maxn];
int rt;
int ans;
int vis[maxn];
int co;
struct Node{
    int v, cost;
    Node(){}
    Node(int x, int y){v=x;cost=y;}
};
vector<Node> e[maxn];
bool cmp(const int a, const int b){return a>b;}
void init(){
    memset(d, 0, sizeof(d));
    memset(e, 0, sizeof(e));
    memset(vis, 0, sizeof(vis));
    ans=0;
    co=0;
}
void dfs(int cur){
    vis[cur]=1;
    int len=e[cur].size();
    int t=0;
    
    rep(0, len){
        if(vis[e[cur][i].v] == 0) {
            dfs(e[cur][i].v);
            //d[cur]=max(d[cur], d[e[cur][i].v]+e[cur][i].cost);
            if(d[cur] <= d[e[cur][i].v]+e[cur][i].cost){
                t=d[cur];
                d[cur]=d[e[cur][i].v]+e[cur][i].cost;
            }
            if(d[cur] != d[e[cur][i].v]+e[cur][i].cost && t<d[e[cur][i].v]+e[cur][i].cost) t=d[e[cur][i].v]+e[cur][i].cost;
        }
    }
    
    ans=max(ans, t+d[cur]);
    
}
int main(){
    while(~scanf("%d%d", &n, &rt)){
        int a=0, b=0, c=0;
        
        init();
        
        rep(0, n-1){
            scanf("%d%d%d", &a, &b, &c);
            e[a].push_back(Node(b, c));
            e[b].push_back(Node(a, c));
            co += c;
        }
        
        dfs(rt);
        
        printf("%d\n", co*2-ans);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值