hdu 4679 Terrorist’s destroy ( 树形dp )

Terrorist’s destroy

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1399    Accepted Submission(s): 440

Problem Description
There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But now he is alone, he can only destroy one road, then the city will be divided into two cities. Impression of the city is a number defined as the distance between the farthest two houses (As it relates to the fare).When the terrorist destroyed a road, he needs to spend some energy, assuming that the number is a.At the same time,he will get a number b which is maximum of the Impression of two cities. The terrorist wants to know which road to destroy so that the product of a and b will be minimized.You should find the road's id.
Note that the length of each road is one.
 

Input
The first line contains integer T(1<=T<=20), denote the number of the test cases.
For each test cases,the first line contains a integer n(1 < n <= 100000);denote the number of the houses;
Each of the following (n-1) lines contains third integers u,v,w, indicating there is a road between house u and houses v,and will cost terrorist w energy to destroy it.The id of these road is number from 1 to n-1.(1<=u<=n , 1<=v<=n , 1<=w<=10000)
 

Output
For each test case, output the case number first,and then output the id of the road which the terrorist should destroy.If the answer is not unique,output the smallest id.
 

Sample Input
2 5 4 5 1 1 5 1 2 1 1 3 5 1 5 1 4 1 1 3 1 5 1 1 2 5 1
 

Sample Output
Case #1: 2 Case #2: 3
 

Source

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 

题意:一颗树,去掉一条边花费的费用为:去掉这条边后形成的两棵树的最大直径*这条边的权值。求去掉哪条边,花费的费用最小。 

思路: 

1.去掉非直径上的边,那么花费为 d*w;
2.去掉直径上的边,那么需要求生成的两颗子树中的直径的最大值,树中有个结论就是去掉一条边后生成的两颗子树最大直径肯定是原来的树中一个端点出发的路径,那么就可以考虑用对原来树的直径的两个端点两次dfs用树形dp来解决问题了。
去掉一条边u-v后的最大直径,先算u->v,则去掉u->v后含v的子树最大直径有两种可能:
(1).从v出发的最大边+从v出发的次大边。
(2).v的子树的最大直径。
去掉v->u同理。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 100005;
int len,gen,tot;
int head[N];
int dep[N],pre[N],vis[N],res[N];
int ma[N],sma[N],mm[N];//ma-最长边 sma-次长边 mm-最大直径 res-答案
struct Edge
{
    int to,next;
    int id,w;
}edge[N*2];
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(res,0,sizeof(res));
}
void addedge(int u,int v,int w,int id)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].id = id;
    edge[tot].next = head[u];
    head[u] = tot++;
    edge[tot].to = u;
    edge[tot].w = w;
    edge[tot].id = id;
    edge[tot].next = head[v];
    head[v] = tot++;
}
void dfs1(int u,int fa)
{
    dep[u] = dep[fa] + 1;
    pre[u] = fa;
    if(len < dep[u])
    {
        len = dep[u];
        gen = u;
    }
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(v != fa) dfs1(v,u);
    }
}
void dfs(int u,int fa)
{
    ma[u] = sma[u] = mm[u] = 0;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == fa)continue;
        dfs(v,u);
        if(sma[u] < ma[v] + 1)
        {
            sma[u] = ma[v] + 1;
            if(sma[u] > ma[u])
                swap(sma[u],ma[u]);
            mm[u] = max(mm[u],sma[u]+ma[u]);
        }
        if(mm[u] < mm[v])
            mm[u] = mm[v];
    }
}
void solve(int u,int fa)
{
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        int id = edge[i].id;
        int w = edge[i].w;
        if(v == fa)continue;
        if(vis[v])
            res[id] = max(res[id],mm[v]*w);
        else
            res[id] = max(res[id],len*w);
        solve(v,u);
    }
}
int main()
{
    int t,n,u,v,w,st,ed,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d",&n);
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w,i);
        }
        dep[0] = len = 0;
        dfs1(1,0);
        st = gen;
        len = 0;
        dfs1(st,0);
        ed = gen;
        len--;//根的长度
        int tmp = ed;
        while(tmp)
        {
            vis[tmp] = 1;
            tmp = pre[tmp];
        }
        dfs(st,0);
        solve(st,0);
        dfs(ed,0);
        solve(ed,0);
        int ans = INF,hao;
        for(int i=1; i<n; i++)
        {
            if(ans > res[i])
            {
                ans = res[i];
                hao = i;
            }
        }
        printf("Case #%d: %d\n",++ca,hao);
    }
    return 0;
}
View Code
posted @ 2015-07-18 12:49  Doli  阅读(158)  评论(0)    收藏  举报