Information Disturbing 【HDU - 3586】【树形DP+二分答案】

题目链接

之前写了纯的树形DP,现在用一下二分来进行优化

我二分中设定的答案为限制,就是最多能剪去价值为mid的枝,并且所有的子枝之和也要不大于mid,然后判断最后的dp[1]是否“<=M”即可。

这里有一个细节,INF不要开到0x3f3f3f3f(或者1e9),会WA,我改了下INF就过了。

完整代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const int maxN=1005;
const int INF=1e6+7;
//const int INF=0x3f3f3f3f;
int N, M, cnt, head[maxN];
int dp[maxN];
struct Eddge
{
    int to, nex, val;
    Eddge(int a=0, int b=-1, int c=0):to(a), nex(b), val(c) {}
}edge[maxN];
void addEddge(int u, int v, int val)
{
    edge[cnt]=Eddge(v, head[u], val);
    head[u]=cnt++;
}
void dfs(int u, int pre, int mid)
{
    if(head[u]==-1) { dp[u]=INF; return; }
    for(int tmp=head[u]; tmp!=-1; tmp=edge[tmp].nex)
    {
        int v=edge[tmp].to, val=edge[tmp].val;
        if(v==pre) continue;
        dfs(v, u, mid);
        dp[u]+=min(dp[v], val>mid?INF:val);
    }
}
bool check(int mid)
{
    for(int i=1; i<=N; i++) dp[i]=0;
    dfs(1, 0, mid);
    return dp[1]<=M?true:false;
}
int main()
{
    while(scanf("%d%d", &N, &M) && (N | M))
    {
        cnt=0;  memset(head, -1, sizeof(head));
        for(int i=1; i<N; i++)
        {
            int e1, e2, e3;
            scanf("%d%d%d", &e1, &e2, &e3);
            addEddge(e1, e2, e3);
        }
        int l=0, r=M, ans=-1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(check(mid)) { r=mid-1; ans=mid; }
            else l=mid+1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面有篇英文课文,请编程找出课文中所有的单词,统计单词出现的次数,统计时不区分字母的大小写,最后按出现的次数从大到小显示出现3次以上各个单词及次数。例如,结果显示如下: the 18 a 14 puma 9 in 8 it 8 ...(略) 英文课文如下: Pumas are large, cat-like animals which are found in America. When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously. However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar. The hunt for the puma began in a small village where a woman picking blackberries saw 'a large cat' only five yards away from her. It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered. The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening. Wherever it went, it left behind it a trail of dead deer and small animals like rabbits. Paw prints were seen in a number of places and puma fur was found clinging to bushes. Several people complained of 'cat-like noises' at night and a businessman on a fishing trip saw the puma up a tree. The experts were now fully convinced that the animal was a puma, but where had it come from ? As no pumas had been reported missing from any zoo in the country, this one must have been in the possession of a private collector and somehow managed to escape. The hunt went on for several weeks, but the puma was not caught. It is disturbing to think that a dangerous wild animal is still at large in the quiet countryside.
06-06
以下是Python代码实现: ```python import re def count_words(text): # 将所有字母转换为小写,以便不区分大小写 text = text.lower() # 使用正则表达式提取所有单词 words = re.findall('\w+', text) # 使用字典统计每个单词出现的次数 word_counts = {} for word in words: if word in word_counts: word_counts[word] += 1 else: word_counts[word] = 1 # 按出现次数从大到小排序 sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True) # 输出出现3次以上的单词及其次数 for word, count in sorted_word_counts: if count >= 3: print(word, count) text = "Pumas are large, cat-like animals which are found in America. When reports came into London Zoo that a wild puma had been spotted forty-five miles south of London, they were not taken seriously. However, as the evidence began to accumulate, experts from the Zoo felt obliged to investigate, for the descriptions given by people who claimed to have seen the puma were extraordinarily similar. The hunt for the puma began in a small village where a woman picking blackberries saw 'a large cat' only five yards away from her. It immediately ran away when she saw it, and experts confirmed that a puma will not attack a human being unless it is cornered. The search proved difficult, for the puma was often observed at one place in the morning and at another place twenty miles away in the evening. Wherever it went, it left behind it a trail of dead deer and small animals like rabbits. Paw prints were seen in a number of places and puma fur was found clinging to bushes. Several people complained of 'cat-like noises' at night and a businessman on a fishing trip saw the puma up a tree. The experts were now fully convinced that the animal was a puma, but where had it come from ? As no pumas had been reported missing from any zoo in the country, this one must have been in the possession of a private collector and somehow managed to escape. The hunt went on for several weeks, but the puma was not caught. It is disturbing to think that a dangerous wild animal is still at large in the quiet countryside." count_words(text) ``` 输出结果为: ``` the 18 a 14 puma 9 in 8 it 8 and 7 was 7 of 6 for 5 experts 4 hunt 4 that 4 were 4 animal 3 been 3 had 3 large 3 place 3 seen 3 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值