http://acm.hdu.edu.cn/showproblem.php?pid=4276

一般题目是求回到原地,而这道题规定从1进n出。所以1-n这条路是必走,其他走不走无所谓。

这样很自然通过dfs先处理1-n这条路,统计这条路的花费时间cost,同时将这条上的边权设为0。

接下来就是求m-cost时间遍历能产生的最大价值,也就是最普通的问题。在这里因为1-n这条路边权已经为0,所以一定会走,只要m-cost时间能回到1点就行。

所以我觉得将1-n边权置为0是这道题的点睛之笔。

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; struct node{int y,val,next;}tree[MAXN<<]; int head[MAXN],vis[MAXN],ptr=,dp[MAXN][MAXN],a[MAXN],vis2[MAXN]; void init()
{
mem(head,-);
mem(vis,);
mem(vis2,);
mem(dp,);
ptr=;
}
void add(int x,int y,int val)
{
tree[ptr].y = y;
tree[ptr].val = val;
tree[ptr].next = head[x];
head[x] = ptr++;
} int cost; void dfs(int rt)
{
vis[rt]=;
for(int i = ;i<=m;i++) dp[rt][i] = a[rt];
for(int i = head[rt];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
dfs(y);
for(int j=m;j>=;j--)
{
for(int k=;k<=j;k++)
{
if(j-k-*tree[i].val>=)
dp[rt][j] = max(dp[rt][j],dp[rt][j-k-*tree[i].val]+dp[y][k]);
}
}
}
} bool dfs2(int rt)
{
vis2[rt] = ;
for(int i = head[rt];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis2[y]) continue;
if(y==n)
{
cost+=tree[i].val;
return true;
}
if(dfs2(y))
{
cost+=tree[i].val;
tree[i].val = ;
return true;
}
}
} int main()
{
int i,j,k,t;
while(~sf("%d%d",&n,&m))
{
init();
cost = ;
for(i=;i<n;i++)
{
int x,y,z;
sf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(i=;i<=n;i++) sf("%d",&a[i]);
dfs2();
if(cost>m)
{
pf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
continue;
}
m-=cost;
dfs();
pf("%d\n",dp[][m]);
}
}

hdu 4276 树形m时间1进n出的更多相关文章

  1. hdu 4276 树形dp

    题意:给你n个点,n-1条边构成树,每条边有边权(表示走每条边的时间),每个点有点权,问在时间T从点1走到点n,能够得到最多的点权有多少. 题目链接:点我 由于是树,最优的结果一定经过最短路,其他边要 ...

  2. 【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 ...

  3. HDU 4276 The Ghost Blows Light

    K - The Ghost Blows Light Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  4. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  5. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  6. win10锁屏或睡眠一段时间后弹不出登录框

    win10锁屏或睡眠一段时间后弹不出登录框 文:铁乐与猫 通常发生在win10更新到10周年版后发生,也就是会卡在登录状态,但不见输入登录框. 我出现这种情况的时候不是很严重,一般等久些也能出现,但问 ...

  7. android 开发 实现一个app的引导页面,使用ViewPager组件(此引导的最后一页的Button会直接写在最后一页布局里,跟随布局滑进滑出)

    基本ViewPager组件使用方式与我之前写的https://blog.csdn.net/qq_37217804/article/details/80332634 这篇博客一致. 下面我们将重点详细解 ...

  8. ref与out区别(ref有进有出,而out只出不进)

    ref与out区别(ref有进有出,而out只出不进)   C#基础:ref和out的区别 ref和out的区别在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值 ...

  9. HDU 4276 The Ghost Blows Light(树形)

    题意:给出一棵n个节点的树,起点1,终点n,相连的两个节点之间有距离,每个节点有个价值,给出一个时间T.问从1到达n在给定时间T内取得的最大价值? 思路:先从1走到n,如果总的时间不够走完,直接退出, ...

随机推荐

  1. c++语言的学习笔记代码与笔记注释《函数部分》

    具体的笔记以注释的形式写在代码内,每个知识点用函数的形式表现. #include <iostream>; #include<cmath> const double PI=3.1 ...

  2. c++编码规范(摘录)

    在同一项目组应明确规定对接口函数参数的合法性检查应由函数的调用者负责还是由接口函数本身负责,缺省是由函数调用者负责. 函数的规模尽量限制在200行以内.说明:不包括注释和空格行. 一个函数仅完成一件功 ...

  3. CF580C Kefa and Park dfs

    Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual pa ...

  4. react PropTypes 与 DefaultProps

    PropTypes 与 DefaultProps import React ,{ Component } from 'react'; import PropTypes from 'prop-types ...

  5. python统计字符串中字符个数

    str = "xxx" result = {} for i in set(str):#set将字符串转为集合对象,用于去重,减少计算量 result[i] = str.count( ...

  6. Linux安全加固--系统相关

    一.系统相关 1.系统关键文件设置 1.1.设置文件初始权限 设置默认的umask值,增强安全性. [root@localhost ~]# umask 0022 /etc/profile最下面添加一行 ...

  7. Jury Jeopardy (这是一道单纯的模拟题)

    Description What would a programming contest be without a problem featuring an ASCII-maze? Do not de ...

  8. 浅谈CSS3中display属性的Flex布局

    浅谈CSS3中display属性的Flex布局   最近在学习微信小程序,在设计首页布局的时候,新认识了一种布局方式display:flex 1 .container { 2 display: fle ...

  9. yum安装zabbix故障报错

    装zabbix时报错 [root@zabbix ~]# rpm -ivh zabbix-server-mysql-2.2.3-1.el6.x86_64.rpm zabbix-web-mysql-2.2 ...

  10. Maven常见问题总结

    Failed to read artifact descriptor for cn.lds.tsp:common:jar 1. 先查看本地repository是否下载成功,如果没有,考虑更改下载rep ...