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\n",l);
    else if(check(r)) printf("%d\n",r);
    else printf("-1\n");
}

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;
}
时间: 2024-10-05 05:54:36

Information Disturbing hdu-3586的相关文章

HDU 3586.Information Disturbing 树形dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3205    Accepted Submission(s): 1137 Problem Description In the battlefield , an effective way to defeat enemies is to bre

HDU 3586 Information Disturbing 树形DP+二分

Information Disturbing 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 communicati

hdu 3586 Information Disturbing (树形dp)

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1722    Accepted Submission(s): 641 Problem Description In the battlefield , an effective way to defeat enemies is to bre

Hdu Information Disturbing

Hdu Information Disturbing 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

HDU 3586 树状dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1668    Accepted Submission(s): 618 Problem Description In the battlefield , an effective way to defeat enemies is to brea

BNUOJ 7697 Information Disturbing

Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 358664-bit integer IO format: %I64d      Java class name: Main In the battlefield , an effective way to defeat enemies is to break their

hdu 3586 树形dp+二分

题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所 有前线与司令部联系所花费的总费用少于m时的最小limit.1<=n<=1000,1<=m<=100万 题目要问的是最小的最大限制,必然二分答案 然后对于每一个值,树形DP判定是否可行 dp[i]表示要切断以i为根的其它所有子树的最小代价. 其中设定叶子结点的代价为无穷大

HDU 3586 Information Disturbing (二分+树形dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit.1<=n<=1000,1<=m<=10^6 dp[i]表示i节点为root的这个子树所破坏的最少费用,if(cost[i]

HDU 3586 Information Disturbing(二分+树形dp)

http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit. 思路: 对于上限limit我们可以二分查找.然后就是树形dp,看代码就可以理解的. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring>

HDU 3586 Information Disturbing

二分+树形DP验证. 答案是通过二分查找得到的,对于每一次二分到的值,进行验证,是否可行. 可以用树形DP来求解所有叶子节点不能向根节点传送消息的最小费用,dp[i] 表示 节点i 的子树的叶子结点都不能向i传送消息的最小费用,这样很容易递推. #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<map> #include<vector>