程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 3586 二分+樹形dp

hdu 3586 二分+樹形dp

編輯:C++入門知識

Information Disturbing

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


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


題意:切斷一定的邊,使得所有葉子節點與根節點斷開,限制條件是斷開的邊權之和小於某一值,且每一條邊權小於某一值x,求x的最小值。

一看題就知道是二分+樹形dp判斷可行性, INF上界不能太大,否則會溢出,因為這個wa好多次。

代碼:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-6 16:15:10
File 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<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved