Hdu 3586 binary + tree dp

Source: Internet
Author: User

Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission (s): 1335 Accepted Submission (s): 489


Problem DescriptionIn 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 neighbor 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.

InputThe 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.

OutputEach case shocould 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


Cut off a certain edge so that all the leaf nodes are disconnected from the root node. The condition is that the sum of the broken edge weights is smaller than a certain value, and each edge weight is smaller than a certain value x, returns the minimum value of x.

At first glance, we can see that it is a binary + tree-like dp to determine the feasibility. The INF upper bound cannot be too large, otherwise it will overflow, because this wa has been used for many times.

Code:

/* ***********************************************Author :xianxingwuguanCreated Time :2014-2-6 16:15:10File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include 
 
  #include 
  
   #include #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include 
             using namespace std;#define INF 1000000#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=4009;int head[maxn],tol;struct node{ int next,to,val; node(){}; node(int _next,int _to,int _val):next(_next),to(_to),val(_val){}}edge[5*maxn];void add(int u,int v,int val){ edge[tol]=node(head[u],v,val); head[u]=tol++;}int dfs(int u,int fa,int num){ int sum=0,flag=0; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(v==fa)continue; int tt=dfs(v,u,num); if(tt>edge[i].val&&edge[i].val<=num) tt=edge[i].val; sum+=tt; flag=1; } if(!flag)return INF; return sum;}int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int i,j,k,m,n; while(~scanf("%d%d",&n,&m)){ if(n==0||m==0)break; memset(head,-1,sizeof(head));tol=0; int l=1,r=1010; for(i=1;i
              
               >1; if(dfs(1,1,mid)<=m) ans=mid,r=mid-1; else l=mid+1; } cout<
               

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.