HDU 1011 Starship Troopers 树形+背包dp
http://acm.hdu.edu.cn/showproblem.php?pid=1011
for(int k=m;k>=bu[x];k--){
for(int j=;j+k<=m;j++){//注意这个地方从1开始
f[x][k+j]=max(f[x][k+j],f[x][k]+f[y][j]);
}
}
j从1开始循环完美避免了不给下面的路分配人却得到brain值的事情发生,然后就可以ac了。。。
但是并没有ac。。为什么呢。。。因为我,没有把写好的清空数据函数放到主程序里。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const double eps=1e-;
const int modn=;
int n,m;
struct nod{
int next,y;
}e[maxn*];
int bu[maxn]={},bra[maxn]={},head[maxn]={},tot=;
bool vis[maxn]={};
int f[maxn][maxn]={};
void init(int x,int y){
e[++tot].y=y;
e[tot].next=head[x];
head[x]=tot;
}
void dfs(int x){
vis[x]=;
int y;
for(int i=bu[x];i<=m;i++){
f[x][i]=bra[x];
}
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
if(!vis[y]){
dfs(y);
for(int k=m;k>=bu[x];k--){
for(int j=;j+k<=m;j++){
f[x][k+j]=max(f[x][k+j],f[x][k]+f[y][j]);
}
}
}
}
}
void yu(){
tot=;memset(f,,sizeof(f));
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
}
int main(){
for(;;){
yu();
scanf("%d%d",&n,&m);
if(n==-||m==-){
break;
}
for(int i=;i<=n;i++){
scanf("%d%d",&bu[i],&bra[i]);
bu[i]=(bu[i]+)/;
}
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
init(x,y);init(y,x);
}
if(m==){
printf("0\n");
continue;
}
dfs();
printf("%d\n",f[][m]);
}
return ;
}
HDU 1011 Starship Troopers 树形+背包dp的更多相关文章
- hdu 1011 Starship Troopers 树形背包dp
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011 Starship Troopers(树形背包)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011 Starship Troopers(树形DP入门)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1011 Starship Troopers(树上背包)
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
- [HDU 1011] Starship Troopers (树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 dp[u][i]为以u为根节点的,花了不超过i元钱能够得到的最大价值 因为题目里说要访问子节点必 ...
- HDU 1011 Starship Troopers 树形DP 有坑点
本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...
- HDU 1011 Starship Troopers【树形DP/有依赖的01背包】
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...
- hdu 1011(Starship Troopers,树形dp)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1011 Starship Troopers 经典的树形DP ****
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 用ajax、PHP、session做购物车
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- laravel判断是否post传输
可以用post传输判断form表单是否有值post传过来: if($request->isMethod('post')){ // 要执行的代码 }通过 Request 对象的 isMethod ...
- laravel前台html代码不显示
后天向前台传输变量,如果能取到变量数据,还有代码,但是不显示图片 可以把{{}}换成{!! !!}试试.
- 在Unity中实现屏幕空间反射Screen Space Reflection(4)
第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...
- 2017ACM暑期多校联合训练 - Team 2 1008 HDU 6052 To my boyfriend (数学 模拟)
题目链接 Problem Description Dear Liao I never forget the moment I met with you. You carefully asked me: ...
- react-native关于ios的启动图标设置
1.首先我们需要使用xcode打开项目,选择项目中的images.xcassets这个文件夹 2.点击AppIcon可以看到右边出现针对不同设备的图标尺寸 3.在左边空白处右击,选择Import... ...
- 固定bottom,页面其它可滑动实现方案
利用flex布局, <html> <body> <div class='container'> <div class='content'></di ...
- PIP安装时报The repository located at pypi.douban.com is not a trusted or secure host and is being ignore
C:\WINDOWS\system32>pip install scrapyCollecting scrapy The repository located at pypi.douban.com ...
- 谁说运维用ELK没用?我就说很有用,只是你之前不会用【转】
1.安装JDK 1)登陆ORACLE官网 (http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html ...
- vue 条件渲染与列表渲染
本文是对官方文档的整理 因为 v-if 是一个指令,所以必须将它添加到一个元素上.但是如果想切换多个元素呢?此时可以把一个 <template> 元素当做不可见的包裹元素,并在上面使用 v ...