HDU 3586 Information Disturbing

二分+树形DP验证。

答案是通过二分查找得到的,对于每一次二分到的值,进行验证,是否可行。

可以用树形DP来求解所有叶子节点不能向根节点传送消息的最小费用,dp[i] 表示 节点i 的子树的叶子结点都不能向i传送消息的最小费用,这样很容易递推。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;

int n,m;
const int maxn=1000+10;
struct Edge
{
    int to;
    int val;
    Edge (int u,int v){to=u;val=v;}
};
vector<Edge>tree[maxn];
bool vis[maxn];
int dp[maxn];

void init()
{
    for(int i=0;i<=n;i++) tree[i].clear();
    memset(vis,0,sizeof vis);
    memset(dp,-1,sizeof dp);
}

void read()
{
    for(int i=1;i<=n-1;i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        Edge e1(v,w); Edge e2(u,w);
        tree[u].push_back(e1);
        tree[v].push_back(e2);
    }
}

void dfs(int now,int lim)
{
    bool fail=1;
    for(int i=0;i<tree[now].size();i++)
        if(!vis[tree[now][i].to]) fail=0;

    if(fail) return;

    bool x=0;
    int sum=0;

    for(int i=0;i<tree[now].size();i++)
    {
        if(!vis[tree[now][i].to])
        {
            vis[tree[now][i].to]=1;
            dfs(tree[now][i].to,lim);

            if(dp[tree[now][i].to]!=-1)
                sum=sum+min(tree[now][i].val,dp[tree[now][i].to]);
            else
            {
                if(tree[now][i].val<=lim) sum=sum+tree[now][i].val;
                else x=1;
            }
        }
    }
    if(!x) dp[now]=sum;
}

bool work(int lim)
{
    memset(dp,-1,sizeof dp);
    memset(vis,0,sizeof vis);
    vis[1]=1;
    dfs(1,lim);
    if(dp[1]==-1||dp[1]>m) return 0;
    return 1;
}

void BinarySearch()
{
    int left=1,right=1000,mid;
    int ans;

    if(!work(2000)) ans=-1;
    else
    {
        while(left<=right)
        {
            mid=(left+right)/2;
            if(work(mid)) ans=mid,right=mid-1;
            else left=mid+1;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(!n&&!m) break;
        init();
        read();
        BinarySearch();
    }
    return 0;
}
时间: 2024-10-16 20:54:04

HDU 3586 Information Disturbing的相关文章

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 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,二分)

题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一定的power,而我们有一台具有m点power的机器,问在使用此机器切断敌人通信系统的情况下,使得所切断边的最大边权达到最小是多少?(m<=100w,n<=1000) 思路: 其实就是要求所切断的边的最小瓶颈边.由于m比较大,不好枚举或者DP.但是他们具有线性的关系,就是瓶颈边越大,肯定越容易有解

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