HDU 3586 Imformation Disturbing (树形DP)

任重而道远

Problem Description
In the battlefield , an effective way to defeat enemies is to break their communication system.
The information department told you that there are n enemy soldiers and their network which have n-1 communication routes can cover all of their soldiers. Information can exchange between any two soldiers by the communication routes. The number 1 soldier is the total commander and other soldiers who have only one neighbour is the frontline soldier.
Your boss zzn ordered you to cut off some routes to make any frontline soldiers in the network cannot reflect the information they collect from the battlefield to the total commander( number 1 soldier).
There is a kind of device who can choose some routes to cut off . But the cost (w) of any route you choose to cut off can’t be more than the device’s upper limit power. And the sum of the cost can’t be more than the device’s life m.
Now please minimize the upper limit power of your device to finish your task.
 

Input
The input consists of several test cases. 
The first line of each test case contains 2 integers: n(n<=1000)m(m<=1000000).
Each of the following N-1 lines is of the form:
ai bi wi
It means there’s one route from ai to bi(undirected) and it takes wi cost to cut off the route with the device.
(1<=ai,bi<=n,1<=wi<=1000)
The input ends with n=m=0.
 

Output
Each case should output one integer, the minimal possible upper limit power of your device to finish your task. 
If there is no way to finish the task, output -1.
 

Sample Input
 
  
5 51 3 21 4 33 5 54 2 60 0
 

Sample Output
 
  
3


题目大意:

给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=100000,1<=m<=100万


AC代码:

#include<cstdio>
#include<cstring>

using namespace std;

const int N = 1000 + 10;
const int INF = 1000000 + 10;
struct edge {
    int tov, val, nxt;
}e[N << 1];
int num, ans, maxn, n, m;
int dp[N], head[N << 1];

void add_edge ( int u, int v, int w ) {
    num++;
    e[num].tov = v;
    e[num].nxt = head[u];
    e[num].val = w;
    head[u] = num;
}

int min ( int a, int b ) {
    return a < b ? a : b;
}

void dfs ( int u, int pre, int lim ) {
    bool jud = false;
    dp[u] = 0;
    for (int i = head[u]; i != -1; i = e[i].nxt) {
        int v = e[i].tov;
        if (v == pre) continue;
        jud = true;
        dfs (v, u, lim);
        if (e[i].val <= lim) {
            dp[u] += min (dp[v], e[i].val);
        } else {
            dp[u] += dp[v];
        }
    }
    if (!jud) dp[u] = INF;
}

void bsearch () {
    int lf = 1, rg = maxn;
    ans = -1;
    while (lf <= rg) {
        int mid = (lf + rg) >> 1;
        dfs (1, -1, mid);
        if (dp[1] <= m) {
            ans = mid;
            rg = mid - 1;
        } else {
            lf = mid + 1;
        }
    }
}

int main () {
    while (scanf ("%d%d", &n, &m)) {
        if (n == 0 && m == 0)
          break;
        num = 0, maxn = 0;
        memset (head, -1, sizeof (head));
        int u, v, w;
        for (int i = 1; i < n; i++) {
            scanf ("%d%d%d", &u, &v, &w);
            add_edge (u, v, w);
            add_edge (v, u, w);
            if (w > maxn) maxn = w;
        }
        bsearch ();
        printf ("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/INTEGRATOR_37/article/details/79506328