zoukankan      html  css  js  c++  java
  • HDU 3586 : Information Disturbing

    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 5
    1 3
    2 1
    4 3
    3 5
    5 4
    2 6
    0 0
     
    Sample Output
    3

    题目大意:

    大意就是给你一棵树(n<=1000),树的根恒定为1,然后你需要删掉一些边,使得任何叶子都没有路径到达结点1(根).

    题目的要求是在删掉的边的总花费不超过m的情况下,最大的花费最小。

    解题报告:

    首先本题卡了stl的vector,所以需要手写前向星链表.

    因为本题中的 m 太大,而单条边的花费最多只有1000,所以我们不妨有 dp( i , j )表示将i为根的子树没有任何叶子可以到达点i,且其中的最大花费是m的最小总边权花费。

    那么我们考虑转移,考虑到现在已经处理完了点i的前 k - 1 个子树,现在正在处理点i的第 k 个子树,我们将前面的状态进行转移。

    不妨有这条边的花费为 w ,

    我们考虑 j 从 0 -> w 的转移,他们的转移只有两种选择,第一种还是维护最大花费为 j , 此时肯定不能切掉这条w的边,只能在子树中切掉他,还有一种就是切掉w这条边,状态转移到dp(u,w_

    之后我们考虑 j 从 w + 1 -> 1000 的转移,他们的转移就是取min好了,第一种是在子树中切掉,还有一种是切掉这条w的边,这样我们转移就完成了,初始化dp都是0(因为还没有考虑子树)

    同时叶子的话dp初始化成inf,注意转移的时候inf+inf.....可能会爆int,需要谨慎的进行判断.

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int maxn = 1e3 + 15;
    struct Edge
    {
        int v , w , nxt;
    };
    int n , m ,dp[maxn][maxn],temp[maxn] , head[maxn],tot,maxv,sz[maxn];
    Edge e[maxn*2];
    
    void add(int u ,int v ,int w)
    {
        e[tot].v = v , e[tot].nxt = head[u] , e[tot].w = w;sz[u]++;
        head[u] = tot ++ ;
    }
    
    void initiation()
    {
        memset(head,-1,sizeof(head));tot=0;maxv=0;memset(sz,0,sizeof(sz));
        for(int i = 1 ; i < n ; ++ i){int u , v , w;scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);maxv=max(maxv,w);}memset(dp,0x00,sizeof(dp));
    }
    
    inline void updata(int & x,int v) {x = min(x,v);}
    
    void dfs(int u ,int fa)
    {
        if(sz[u] == 1 && u != 1) memset(dp[u],0x3f,sizeof(dp[u]));
        for(int i = head[u] ; ~i ; i = e[i].nxt)
        {
            int v = e[i].v , w = e[i].w;
            if(v == fa) continue;dfs(v,u);int newe=dp[v][w]+dp[u][w];
            for(int j = 0 ; j <= w ; ++ j)
            {
                updata(newe,dp[u][j]+w);
                if(dp[u][j] >= 1000005 || dp[v][j] >= 1000005) dp[u][j] = 1000005;
                else dp[u][j] += dp[v][j];
            }dp[u][w]=newe;
            for(int j = w + 1 ; j <= maxv ; ++ j) dp[u][j] += min(dp[v][j] , w);
        }
    }
    
    int solve()
    {
        dfs(1,0);int ans = 1<<29;
        for(int i = 0 ; i <= maxv ; ++ i) if(dp[1][i] <= m) {ans = i;break;}
        if(ans == (1<<29)) ans = -1;
        return ans;
    }
    
    
    int main(int argc , char * argv[])
    {
        while(scanf("%d%d",&n,&m) && n)
        {
            initiation();
            printf("%d
    ",solve());
        }
        return 0;
    }
  • 相关阅读:
    微信开发:消息回复总结
    *** wechat-php-sdk 微信公众平台php开发包
    **微信接入探秘(一)——从零认识微信接口(主动接口和被动接口)
    《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
    Linux进程调度原理【转】
    Linux进程核心调度器之主调度器schedule--Linux进程的管理与调度(十九)【转】
    Tslib触摸屏官网【转】
    Tslib的移植【转】
    Linux Kernel代码艺术——数组初始化【转】
    Linux 内核进程管理之进程ID【转】
  • 原文地址:https://www.cnblogs.com/Xiper/p/4833781.html
Copyright © 2011-2022 走看看