组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对。

#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#define maxn 150
using namespace std; struct Edge
{
int v, w;
Edge(int vi, int wi) :v(vi), w(wi){}
Edge(){}
}; vector<Edge> G[maxn];
int n, q;
int dp[maxn][maxn];
int tmp[maxn]; void dfs(int u, int fa)
{
for (int i = 0; i < G[u].size(); i++){
int v = G[u][i].v, w = G[u][i].w;
if (v == fa) continue;
dfs(v, u);
memcpy(tmp, dp[u], sizeof(dp[u]));
for (int k = q; k >= 0; k--){
for (int j = k - 1; j >= 0; j--){
dp[u][k] = max(dp[u][k], dp[v][j] + tmp[k-1- j] + w);
}
}
}
} int main()
{
while (cin >> n >> q)
{
for (int i = 0; i <= n; i++) G[i].clear();
int ui, vi, wi;
for (int i = 0; i < n - 1; i++){
scanf("%d%d%d", &ui, &vi, &wi);
G[ui].push_back(Edge(vi, wi));
G[vi].push_back(Edge(ui, wi));
}
memset(dp, 0, sizeof(dp));
dfs(1, -1);
cout << dp[1][q] << endl;
}
return 0;
}

URAL1018 Binary Apple Tree(树dp)的更多相关文章

  1. Ural-1018 Binary Apple Tree(树形dp+分组背包)

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  2. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  3. URAL-1018 Binary Apple Tree---树形DP

    题目链接: https://cn.vjudge.net/problem/URAL-1018 题目大意: 给你一棵树,每条边有一个边权,求以1为根节点,q条边的子数(q+1个点),边权和至最大. 解题思 ...

  4. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  5. URAL1018. Binary Apple Tree

    链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...

  6. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

  7. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  8. BNUOJ 13358 Binary Apple Tree

    Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...

  9. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

随机推荐

  1. System V共享内存区

    要点 shell查看命令:ipcs -m 主要函数 #include <sys/shm.h> //oflag=IPC_CREAT|IPC_EXCL|0644组合 //创建一个内存共享区 i ...

  2. .NET开源工作流RoadFlow-流程设计-流程步骤设置-基本设置

    流程属性设置完成后点击确定之后,即可进行流程步骤设置了. 点击工具栏上的步骤按钮,即可添加一个新步骤. 在新步骤图形上双击即可弹出该步骤相应属性设置框. 步骤ID:系统自动为该步骤生成的唯一ID. 步 ...

  3. oracle 11g 分区表

    查看所有用户分区表及分区策略(1.2级分区表均包括): SELECT p.table_name AS 表名, decode(p.partitioning_key_count, 1, '主分区') AS ...

  4. hdu 4217 Data Structure?/treap

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 可用线段树写,效率要高点. 这道题以前用c语言写的treap水过了.. 现在接触了c++重写一遍 ...

  5. sql server 查询数据库所有的表名+字段

    SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT    (case when a.colorder= ...

  6. javascript常用方法函数收集

    收集了一些比较常用的javascript函数. 1.字符串长度截取 function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00- ...

  7. 55.ERROR:Place:1136 - This design contains a global buffer instance…… non-clock load pins off chip

    ISE在布局布线时,出现下图所示错误. 对于"clock_dedicated_route”错误原因有两种情况: 1.  就是有一个时钟你没有放到全局时钟或者局部时钟的引脚,布局的时候不能把它 ...

  8. 解决SQLite数据库中文乱码问题

      关于SQLite中出现中文乱码的分析以及解决方案 我们在使用SQLite数据库时候,可能会发现,向数据库插入数据时候显示的是汉字,但通过SQLite读出来时却显示的乱码,这是因为SQLite数据库 ...

  9. Sprint计划会议1

    会议时间:4.15.晚9点 会议地点:学一食堂2楼 会议进程 • 首先我们讨论了实验第一个Sprint1要实现的功能(用户登录及信息录入).•  之后对任务进行了认领.•  最后每个人对自己的任务进行 ...

  10. fstab文件

    格式: 文件系统 挂载点 格式 挂载文件系统选项 备份选项 自检选项 1.指定挂载的设备或者远程文件系统,比如普通的挂载设备/dev/sda1, 对于nfs格式:<host>:<di ...