zoukankan      html  css  js  c++  java
  • Information Disturbing hdu-3586

    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
    题解:在满足总的花费不大于m的情况下,limit要尽量小,所以二分limit,然后检查啦!(学过的东西不能融汇贯通,难怪我这么菜,orz)
    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn=1005;struct node{
        int to,next,va;
    }e[2*maxn];
    
    int n,m,tot;
    int dp[maxn],head[maxn];
    
    void Inite(){
        tot=0;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int w){
        e[tot].to=v;
        e[tot].va=w;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    
    void DFS(int pa,int u,int limit){
        dp[u]=0;
        int flag=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(pa==v) continue;
            flag=1;
            DFS(u,v,limit);
            
            if(e[i].va>limit) dp[u]+=dp[v];
            else dp[u]+=min(dp[v],e[i].va);
        }
        if(flag==0) dp[u]=100001;
    }
    
    bool check(int limit){
        DFS(0,1,limit);
        if(dp[1]>m) return false;        
        else return true;
    } 
    
    void Solve(){
        int l=0,r=m+1;
        for(int i=1;i<=100;i++){
            int mid=(l+r)>>1;
            if(check(mid)) r=mid;
            else l=mid;
        }
        if(check(l)) printf("%d
    ",l);
        else if(check(r)) printf("%d
    ",r);
        else printf("-1
    ");
    }
    
    int main()
    {    
        while(~scanf("%d%d",&n,&m)){
            if(n==0&&m==0) break;
            
            Inite();
            for(int i=2;i<=n;i++){
                int u,v,va;
                scanf("%d%d%d",&u,&v,&va);
                addedge(u,v,va);
                addedge(v,u,va);
            }
            Solve();    
        }
        return 0;
    }
  • 相关阅读:
    织梦分页条添加省略号(支持动态静态)
    织梦点击数或者其他数值过【千】过【万】过【亿】的写法
    织梦文章页每个TAG标签单独输出相关文章
    织梦验证码不显示解决方法总结
    织梦搜索结果根据搜索不同栏目显示不同搜索结果模板
    织梦正则提取中英混合字符串中第一个中文汉字
    织梦让内容摘要多行文本支持换行
    织梦dede:tag标签输入添加自增autoindex
    织梦去除底部版权power by dedecms
    织梦自定义表单添加访客提交时间和访客IP+限制每天每个IP提交表单次数
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7769155.html
Copyright © 2011-2022 走看看