Hdu3586 tree-like dp + binary solution

Source: Internet
Author: User

Hdu3586 tree-like dp + binary solution

 

 

Problem Description In the battlefield, an alternative tive 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.

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

 

 

/** Two-point solution for hdu 3586 tree-like dp: given n vertices in a tree, point 1 is the root node, and each leaf node sends a "message" to the root node ", we need to block the path on some roads to make it impossible. The block cost for each route is the weight value. The maximum number of blocked weights is x, and the cost of blocking all leaf nodes cannot exceed m. Minimum feasible x solution: assuming we have reached an x, the total cost of the u-based subtree is dp [u]. If the edge weight is w <= x, dp [u] + = min (dp [v], w), otherwise dp [u] + = min (dp [v], inf ), here, inf is any number greater than m (but do not kill int, I will take m + 1 ). To explain why inf is used, because the w value has exceeded the limit and we cannot block the w edge, We need to block all the leaf nodes in the Child tree with the v as the root node, it can only be dp [v. In this way, we can find the dp [1] in the tree-like dp, and we can see that our x value combination is not suitable when comparing with m. For x, you can do it in binary mode. */# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; const int maxn = 1005; struct note {int v, w, next;} edge [maxn * 2]; int head [maxn], ip; int n, m, mid, dp [maxn]; void init () {memset (head,-1, sizeof (head); ip = 0;} void addedge (int u, int v, int w) {edge [ip]. v = v, edge [ip]. w = w, edge [ip]. next = head [u], head [u] = ip ++;} void dfs (int u, int pre) {int flag = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) // before solving Father's Day, all of its son nodes must have been obtained {int v = edge [I]. v; if (v = pre) continue; flag = 1; dfs (v, u);} if (flag = 0) /// leaf node {dp [u] = m + 1; // return of any number greater than m;} int t = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) {int w = edge [I]. w; int v = edge [I]. v; if (v = pre) continue; if (w <= mid) dp [u] + = min (dp [v], w ); else dp [u] + = min (dp [v], m + 1) ;}} int main () {while (~ Scanf (% d, & n, & m) {if (n = 0 & m = 0) break; int maxx = 0; init (); for (int I = 0; I
    
     
> 1; memset (dp, 0, sizeof (dp); dfs (1,-1); // printf (% d, mid, dp [1]); if (dp [1] <= m) {flag = 1; r = mid;} else l = mid + 1;} if (flag = 0) printf (-1); else printf (% d, r);} return 0 ;} /** 5 41 3 21 4 33 5 54 2 65 41 3 21 4 23 5 54 2 65 31 3 21 4 23 5 54 2 6 */
    
   
  
 


 

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.