URAL 1018 (金典树形DP)
连接:1018. Binary Apple Tree
Memory limit: 64 MB
N, where
N is the total number of all enumerated points. For instance in the picture below
N is equal to 5. Here is an example of an enumerated tree with four branches:
2 5 |
Input
Output
Sample
input | output |
---|---|
5 2 |
21 |
有一棵苹果树,苹果树的是一棵二叉树,共N个节点,树节点编号为1~N,编号为1的节点为树根,边可理解为树的分枝,每个分支都长着若干个苹果,现在要要求减去若干个分支,保留M个分支,要求这M个分支的苹果数量最多。
#include<stdio.h>
#include<string.h>
const int N=110;
int dp[N][N],vis[N],head[N],num,m;
struct edge
{
int st,ed,w,next;
}e[N*4];
void addedge(int x,int y,int w)
{
e[num].st=x;e[num].ed=y;e[num].w=w;e[num].next=head[x];head[x]=num++;
e[num].st=y;e[num].ed=x;e[num].w=w;e[num].next=head[y];head[y]=num++;
}
void dfs(int u)
{
vis[u]=1;
int i,v,w,j,k,son=0;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].ed;w=e[i].w;
if(vis[v]==1)continue;
dfs(v);
for(k=m;k>=1;k--)//0-1背包
{
for(j=1;j<=k;j++)//在v节点的子树中选择j条边
if(dp[u][k]<dp[u][k-j]+dp[v][j-1]+w)//u与v有一条边,所以加上dp[v][j-1],
dp[u][k]=dp[u][k-j]+dp[v][j-1]+w;
}
}
}
int main()
{
int i,x,y,w,n;
while(scanf("%d%d",&n,&m)!=-1)
{
memset(head,-1,sizeof(head));
num=0;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
addedge(x,y,w);
}
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
dfs(1);
printf("%d\n",dp[1][m]);
}
return 0;
}
URAL 1018 (金典树形DP)的更多相关文章
- Ural 1018 (树形DP+背包+优化)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题目大意:树枝上间连接着一坨坨苹果(不要在意'坨'),给 ...
- ural 1018(树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 思路:典型的树形dp,处理的时候类似于分组背包,dp[i] ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- 树形DP URAL 1039 Anniversary Party
题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...
- 刷题总结——二叉苹果树(ssoj树形dp+记忆化搜索)
题目: 题目背景 URAL:http://acm.timus.ru/problem.aspx?space=1&num=1018 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- poj 2342 Anniversary party 简单树形dp
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3862 Accepted: 2171 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- hdu1520 第一道树形DP,激动哇咔咔!
A - 树形dp Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- hdu4597 Play Game
Play Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...
- Ext JS学习第五天 我们所熟悉的javascript(四)
此文用来记录学习笔记: •javascript之对象.面向对象 •可能对于高级语言你可能了解甚至精通OOP面向对象,那么对于javascript你又熟悉多少呢?我们一起来学习javascript面向对 ...
- c++,多继承造成的二义性及解决办法
#include <iostream> using namespace std; //------------------------------- class A1{ public: i ...
- vim下设置tab
前言:大多数情况下tab键的宽度设置为4个空格,这个可以根据自己 的代码风格进行替换,然而当你提交不同的语言的代码的时候python 和c的时候就有区别了.c的话一般tab键做缩进,而python提交 ...
- 仿爱乐透android客户端界面实现(附工程源码)
最近研究了爱乐透android客户端的界面,感觉它的界面布局在一般开发中具有代表性.难点在于复杂的布局实现. 界面实现主要采用了以下方式: 注意:版本支持:android2.2以上,低版本要改动源码哦 ...
- POJ 3047 Fibonacci
DEBUG很辛苦,且行, 且珍惜 原代码: ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000; a ...
- CSS 规避脱标之两种用法
大家好,我是小强老师,今天讲解一小点知识哈 对比了才知道什么好 看不出,很漂亮吧! 有木有倾国倾城的美色. 呵呵,好多东西也是这样的,好的东西只有对比了才觉得好. 我们知道我们网页布局 有三模式. ...
- 【细说Java】揭开Java的main方法神秘的面纱(转)
大家都知道,main方法是Java应用程序的入口,其定义格式为: public static void main(String[] args) 可是为什么要这么定义呢?不这样定义可以么?main方法可 ...
- Arachnid包含一个简单的HTML剖析器能够分析包含HTML内容的输入流
Arachnid是一个基于Java的web spider框架.它包含一个简单的HTML剖析器能够分析包含HTML内容的输入流.通过实现Arachnid的子类就能够开发一个简单的Web spiders并 ...
- android大牛高焕堂最新力作-android架构师之路
android大牛高焕堂 个人介绍: Android专家顾问,台湾Android论坛主席,现任亚太地区Android技术大会主席,台湾Android领域框架开发联盟总架构师.发表100多篇Androi ...