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;
    }
  • 相关阅读:
    margin问题
    IE6里面子集尺寸大的会把父亲撑大
    第一个元素<flout>写了,想在他的旁边加一个元素.IE6会出现缝隙. 不要用margin撑开,要用flout
    兼容性,float
    HTML5的兼容问题以及调用js文件的方法
    表单
    表格的编写,课程表
    SmartThreadPool
    C# 多线程的等待所有线程结束的一个问题
    DataTable保存与读取 stream
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7769155.html
Copyright © 2011-2022 走看看