网上题解都是用spfa求1-n路径的,但其实dfs一次就可以了。。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip> using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 1000005
#define MOD 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define INF 100000000 int n , t;
struct edge
{
int u , v , w;
int next;
}E[*];
int head[];
int dp[][];
int val[];
int cost[] , per[];
int id,sum ,flag ; void add(int u , int v , int w)
{
E[id].u = u;
E[id].v = v;
E[id].w = w;
E[id].next = head[u];
head[u] = id++;
}
/*
void spfa()
{
mem(per , -1);
for(int i = 2 ; i <= n ; i ++) cost[i] = INF;
cost[1] = 0;
queue<int>q;
q.push(1);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u] ; i >= 0 ; i = E[i].next)
{
if(cost[E[i].v] > cost[u] + E[i].w)
{
cost[E[i].v] = cost[u] + E[i].w;
per[E[i].v] = i;
q.push(E[i].v);
}
}
}
for(int i = 1 ; i <= n ; i ++) cout << per[i] << endl;
for(int i = per[n] ; i >= 0 ; i = per[i])
{
cout << E[i].u << " " << E[i].v << " " << E[i].w << endl;
E[i].w = 0;
}
}*/
/*
bool judge(int u,int pre)///找出1~n的路径
{
if(u == n)return true;
for(int i = head[u]; i != -1 ; i = E[i].next)
{
int v = E[i].v;
if(v == pre)continue;
if(judge(v,u))
{
sum += E[i].w;
E[i].w=0;
return true;
}
}
return false;///这句话必须有,因为这一句我没写WA到死.....
}
*/ int judge(int u , int per)
{
int flag=;if(u==n)return ;
for(int i = head[u] ; i >= ; i = E[i].next )
{
if(E[i].v == per) continue;
if(judge(E[i].v,u)) {sum += E[i].w ; E[i].w = ; flag = ;}
else E[i].w*=;
}
return flag;
} void solve(int u , int per)
{
for(int i = head[u] ; i >= ; i = E[i].next)
{
if(E[i].v == per) continue;
solve(E[i].v , u);
for(int j = t ; j >= E[i].w ; j --)
{
int up = j - E[i].w;
for(int k = ; k <= up ; k ++)
{
// if(dp[u][up-k] != -1 && dp[E[i].v][k] != -1)
dp[u][j] = max(dp[u][j] , dp[u][up-k] + dp[E[i].v][k]);
}
}
}
} int main()
{
while(scanf("%d %d" , &n , &t) != EOF)
{
mem(dp , );id = ;
mem(head , -);
int u , v , w;
for(int i = ; i < n ; i ++)
{
scanf("%d %d %d" , &u , &v , &w);
//if(u > v) swap(u , v);
add(u , v , w);
add(v , u , w);
}
for(int i = ; i <= n ; i ++)
{
scanf("%d" , &val[i]);
for(int j = ; j <= t ; j ++) dp[i][j] = val[i];
}
flag = sum = ;
judge( , -);
//spfa();
if(sum > t)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
//cout << sum << endl;
t -= sum;
solve( , -);
printf("%d\n" , dp[][t]);
}
}

hdu4276 依赖背包的更多相关文章

  1. 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

    The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...

  2. hdu 1561 The more, The Better (依赖背包 树形dp)

    题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...

  3. hdoj1010Starship Troopers (树dp,依赖背包)

    称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...

  4. 依赖背包——cf855C好题

    比较裸的依赖背包,但是想状态还是想了好久 转移时由于边界问题,虽然可以倒序转移,但当容量为0|1的时候,由于有初始值的存在 很难再原dp数组上进行修改,所以额外用tmp数组来保存修改后的值 #incl ...

  5. cf581F 依赖背包+临时数组 好题

    这题得加个临时数组才能做.. /* 给定一棵树,树节点可以染黑白,要求叶子节点黑白平分 称连接黑白点的边为杂边,求使得杂边最少的染色方 那么设dp[i][j][0|1]表示i子树中有j个叶子节点,i染 ...

  6. poj1155 依赖背包

    /* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...

  7. BZOJ.4182.Shopping(点分治/dsu on tree 树形依赖背包 多重背包 单调队列)

    BZOJ 题目的限制即:给定一棵树,只能任选一个连通块然后做背包,且每个点上的物品至少取一个.求花费为\(m\)时最大价值. 令\(f[i][j]\)表示在点\(i\),已用体积为\(j\)的最大价值 ...

  8. BZOJ.4910.[SDOI2017]苹果树(树形依赖背包 DP 单调队列)

    BZOJ 洛谷 \(shadowice\)已经把他的思路说的很清楚了,可以先看一下会更好理解? 这篇主要是对\(Claris\)题解的简单说明.与\(shadowice\)的做法还是有差异的(比如并没 ...

  9. hdu1561 树形dp,依赖背包

    多重背包是某个物品可以选择多次,要把对物品数的枚举放在对w枚举外面 分组背包是某组的物品只能选一个,要把对每组物品的枚举放在对w枚举内侧 依赖背包是多层的分组背包,利用树形结构建立依赖关系,每个结点都 ...

随机推荐

  1. 前端开发者不得不知的es6十大特性(转)

    转载自AlloyTeam:http://www.alloyteam.com/2016/03/es6-front-end-developers-will-have-to-know-the-top-ten ...

  2. 对象转换为json格式,类似中间层API

    <一头扎进SpringMvc视频教程\<一头扎进SpringMvc>第四讲 源码\> 对象自动转换为json格式要在 spring-mvc.xml添加一个东西 ,和对应的命名空 ...

  3. 微信小程序学习过程

    1.诞生背景: 更好的体验: 规范与管理: 2.什么是小程序: 小程序是一种不需要下载即可使用的应用,它实现了应用“触手可及”的梦想: 用户扫一扫或者搜一下即可打开应用,也体现了“用完即走”的理念: ...

  4. 【NLP CS224N笔记】汇总

    [NLP CS224N笔记]Lecture 1 - Introduction of NLP [NLP CS224N笔记]Lecture 2 - Word Vector Representations: ...

  5. 虚拟机安装CentOS配置静态IP

    在VMware中安装Linux虚拟机后(比如CentOS6.*),不能访问网络,需要配置静态IP.虚拟机中推荐使用NET模式进行网络连接,在虚拟机的工具栏点击编辑>虚拟网络编辑器>NET模 ...

  6. Boost.Asio的使用技巧

    基本概念 Asio proactor I/O服务 work类 run() vs poll() stop() post() vs dispatch() buffer类 缓冲区管理 I/O对象 socke ...

  7. win32编程中消息循环和WndProc()窗口过程函数

    原文地址:https://blog.csdn.net/zxxSsdsd/article/details/45504383 在win32程序的消息循环函数中  while (GetMessage (&a ...

  8. hibernate框架学习之数据抓取(加载)策略

    Hibernate获取数据方式 lHibernate提供了多种方式获取数据 •load方法获取数据 •get方法获取数据 •Query/ Criteria对象获取数据 lHibernate获取的数据分 ...

  9. struts2框架之输入校验(参考第二天学习笔记)

    输入校验: 1. 分类 客户端校验:javascript,它是用户体验而已,可以绕开. 服务器端校验 * 代码校验 1). 要求Action必须继承ActionSupport 2). 重写Action ...

  10. bean shell之间传递参数

    BeanShell PostProcessor 向 BeanShell断言 传递参数 断言成功: