HDU 4679 Terrorist’s destroy

Terrorist’s destroy

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


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
 

Recommend
zhuyuanchen520
 
题意: 有一颗树, 有N个城市, 每条路有一个权值W, 问炸一条路, 使得product 最小。  product等于 = W * (两棵树的最长直径).

思路: 树形DP
我已经做了两题了。  大同小异。  
原来还有手动扩栈这回事, 坑, 还要交C++, 不能交G++。 
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

//树形DP

const int V = 100000 + 50;
const int inf = 0x3fffffff;
typedef struct Edge {
    int a, b, w;
};
Edge edge[V];
int n, T, ans, id;
int pnt[V * 2], nxt[V * 2], head[V * 2], e;//建图
int Fir[V][2], Sec[V][2], Thr[V][2];
bool vis[V]; //标记搜索
inline void addedge(int u, int v) {
    pnt[e] = v;
    nxt[e] = head[u];
    head[u] = e++;
}
void update(int index, int len, int id) {
    if(len > Fir[index][0]) {
        Thr[index][0] = Sec[index][0];
        Thr[index][1] = Sec[index][1];
        Sec[index][0] = Fir[index][0];
        Sec[index][1] = Fir[index][1];
        Fir[index][0] = len;
        Fir[index][1] = id;
    }
    else if(len > Sec[index][0]) {
        Thr[index][0] = Sec[index][0];
        Thr[index][1] = Sec[index][1];
        Sec[index][0] = len;
        Sec[index][1] = id;
    }
    else if(len > Thr[index][0]) {
        Thr[index][0] = len;
        Thr[index][1] = id;
    }
}
int down_dp(int u) {
    vis[u] = true;
    for(int i = head[u]; i != -1; i = nxt[i])
        if(!vis[pnt[i]])
            update(u, down_dp(pnt[i]) + 1, pnt[i]);
    return Fir[u][0];
}
void up_dp(int u, int fa, int len) {
    update(u, len, fa);
    vis[u] = true;
    for(int i = head[u]; i != -1; i = nxt[i]) {
        if(vis[pnt[i]])
            continue;
        if(pnt[i] != Fir[u][1])
            up_dp(pnt[i], u, 1 + Fir[u][0]);
        else
            up_dp(pnt[i], u, 1 + Sec[u][0]);
    }
}
int count_Im(int u, int v) {
    int a = 0, b = 0;
    if(Fir[u][1] != v) {
        a += Fir[u][0];
        b++;
    }
    if(Sec[u][1] != v) {
        a += Sec[u][0];
        b++;
    }
    if(b < 2)
        a += Thr[u][0];
    return a;
}
int main() {
    int i, j, k, d = 1;
    scanf("%d", &T);
    while(T--) {
        id = 0;
        ans = inf;
        memset(Fir, 0, sizeof(Fir));
        memset(Sec, 0, sizeof(Sec));
        memset(Thr, 0, sizeof(Thr));
        scanf("%d", &n);
        //建图
        e = 0;
        memset(head, -1, sizeof(head));
        for(i = 1; i <= n - 1; ++i) {
            scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].w);
            addedge(edge[i].a, edge[i].b);
            addedge(edge[i].b, edge[i].a);
        }
        memset(vis, false, sizeof(vis));
        down_dp(1); //求节点到各儿子路径的最长路
        memset(vis, false, sizeof(vis));
        up_dp(1, 0, 0); //求节点到父亲路径的最长路
        //for(i = 1; i <= n; ++i)
        //    printf("%d %d %d %d %d %d\n", Fir[i][0], Fir[i][1], Sec[i][0], Sec[i][1], Thr[i][0], Thr[i][1]);
        for(i = 1; i <= n - 1; ++i) {
            int a = count_Im(edge[i].a, edge[i].b);
            int b = count_Im(edge[i].b, edge[i].a);
            if(ans > edge[i].w * max(a, b)) {
                ans = edge[i].w * max(a, b);
                id = i;
            }
        }
        printf("Case #%d: %d\n", d++, id);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值