Information Disturbing 【HDU - 3586】【纯树形DP】

题目链接

  题意挺奇怪的,最初的时候还以为是道网络流的题目,后来想了想,就是求剪去所有叶子节点到根节点的关系的所需最贵的一条边最小化的题目,挺拗口的不是吗?

  这道题,想了良多,听说有用树形DP+二分来写的(之后再补上这样的做法吧),但我不那么做,我想了想,树形DP+链式前向星也可以写,剪去非根节点以外的边,我们所需的价值不就是要么剪去与父亲的边要么就是剪去所有与儿子的边的权值的总和,所以dp[i][j]存放的是对于i这个节点,我们处理掉它与它的子节点或者父节点关系的最小花费,j的意思是你用j的钱是否可以达成目的,以及达成目的后的最优解,就是j~上限的所需花费可能会是同一个值,然后我定义了个cost[]数组,从叶子节点开始向上返回,返回所要消去这号链上的节点所需要的最小花费,其中,从叶子节点往上,我们的花费得从最小下限开始,如果低于了最小下限,说明钱不够,所以得有这样的判断。

  初始化问题:这里的dp[][]得初始化为极大值,INF一开始写成(long long)1<<63,会WA,改一下,写成INF=0x3f3f3f3f就过了,我也不知道是什么原因,下次规范着写吧,就像前段时间刚学的链式前向星一样(略懂略懂==)。

完整代码:

#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 ll INF=0x3f3f3f3f;
int N, M, cnt, head[maxN<<1], mx;
ll dp[maxN][maxN];
struct Eddge
{
    int to, next, val;
    Eddge(int a=0, int b=-1, int c=0):to(a), next(b), val(c) {}
}edge[maxN<<1];
void addEddge(int u, int v, int val)
{
    edge[cnt]=Eddge(v, head[u], val);
    head[u]=cnt++;
}
void dfs(int u, int r)
{
    if(u!=1) for(int i=r; i<=mx; i++) dp[u][i]=r;
    ll cost[maxN]={0};
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to, val=edge[i].val;
        dfs(v, val);
        for(int j=1; j<=mx; j++)
        {
            cost[j]+=dp[v][j];
        }
    }
    for(int i=1; i<=mx; i++)
    {
        if(cost[i]==0) continue;
        dp[u][i]=min(dp[u][i], cost[i]);
    }
}
int main()
{
    while(scanf("%d%d", &N, &M) && (N | M))
    {
        memset(head, -1, sizeof(head)); cnt=mx=0;
        for(int i=1; i<N; i++)
        {
            int e1, e2, e3;
            scanf("%d%d%d", &e1, &e2, &e3);
            addEddge(e1, e2, e3);
            mx=max(mx, e3);
        }
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=mx; j++)
            {
                dp[i][j]=INF;
            }
        }
        dfs(1, 0);
        int ans=-1;
        for(int i=1; i<=mx; i++)
        {
            if(dp[1][i]<=M) { ans=i; break; }
        }
        printf("%d\n", ans);
    }
    return 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、付费专栏及课程。

余额充值