【HDU 3586】Information Disturbing【树型DP】

Information Disturbing

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


 

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

Sample Output

3

 

 

题意不难理解,有n个阵地,其中1表示司令部,所有叶子节点表示前线,要求在切断前线与司令部联系所消耗的总值不大于m的情况下,问切断每条连线的的最小限制。(也就是说完成切断联系的每条边切断的消耗不大于限制,并且总消耗不大于m)不是很难的题,写了好久,全是细节问题……

思路:求最大消耗的最小值,二分+树型DP。每次二分出答案,判断在该答案下,总消耗是否大于m。

注意:

1.叶子节点初始值不能太大或太小,太大的话会爆掉,太小会WA,在这WA了好多次

2.用vector来记录节点间的关系,不能直接在二维数组中遍历,否则会TE

3.这道题会反复大量调用函数,尽量不要在函数内部定义变量,否则会内存超限

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

int n,m,mid,ans=-1,j;
long long dp[1005];
int wei[1007][1007];
vector<int>tree[1005];

int finds(int left,int right);
bool judge(int limit);
int dfs(int pa,int limit);

int main()
{
    int i,l=0,r=0;
    int a,b,w;
    while(scanf("%d %d",&n,&m)&&(n||m))
    {
        for(i=0;i<n;i++) tree[i].clear();
        memset(wei,0,sizeof(wei));
        l=0;r=0;
        for(i=0;i<n-1;i++)
        {
            scanf("%d %d %d",&a,&b,&w);
            wei[a][b]=w;
            tree[a].push_back(b);
            r=max(r,w);
        }
        printf("%d\n",finds(l,r));
    }
    return 0;
}

int finds(int left,int right)
{
    ans=-1;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(judge(mid))
        {
            right=mid-1;
            ans=mid;
        }
        else left=mid+1;
    }
    return ans;
}

bool judge(int x)
{
    dp[1]=0;
    for(j=2;j<=n;j++)
    {
        if(tree[j].size()==0) dp[j]=1000010;
        else dp[j]=0;
    }
    dfs(1,x);
    if(dp[1]>m) return false;
    else return true;
}
int dfs(int pa,int limit)
{
	int i;
    for(i=0;i<(int)tree[pa].size();i++)
    {
        if(wei[pa][tree[pa][i]]<=limit) dp[pa]+=min(dfs(tree[pa][i],limit),wei[pa][tree[pa][i]]);
        else dp[pa]+=dfs(tree[pa][i],limit);
    }
	return dp[pa];
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值