【POJ 1155】TELE
【题目链接】
【算法】
树形DP
f[i][j]表示以i为根的子树中,选了j个叶子节点,所能带来的最大收益
不难发现这就是一个经典的背包问题,不过是在树上做背包罢了
最后,判断f[1][i]是否大于等于0,输出最大的i
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 3010
const int INF = 2e9; int i,j,a,c,k,x,n,m;
vector< pair<int,int> > e[MAXN];
int size[MAXN],f[MAXN][MAXN]; inline void dfs(int x)
{
int i,j,k,y,cost;
for (i = ; i < e[x].size(); i++)
{
y = e[x][i].first;
cost = e[x][i].second;
dfs(y);
size[x] += size[y];
for (j = size[x]; j >= ; j--)
{
for (k = ; k <= size[y]; k++)
{
if (j >= k && f[x][j-k] != -INF && f[y][k] != -INF)
f[x][j] = max(f[x][j],f[x][j-k]+f[y][k]-cost);
}
}
}
} int main()
{ scanf("%d%d",&n,&m);
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
f[i][j] = -INF;
}
}
for (i = ; i <= n - m; i++)
{
scanf("%d",&k);
for (j = ; j <= k; j++)
{
scanf("%d%d",&a,&c);
e[i].push_back(make_pair(a,c));
}
} for (i = n - m + ; i <= n; i++)
{
scanf("%d",&x);
size[i] = ;
f[i][] = x;
} dfs(); for (i = m; i >= ; i--)
{
if (f[][i] >= )
{
printf("%d\n",i);
break;
}
} return ; }
【POJ 1155】TELE的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- 使用cURL和用户名和密码?
问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...
- 2016阿里校招python研发面试
一面: 面:说说你们学校的主修课程. 学校开的全是尼玛java课,这个我是想了有一会的. 面:看你简历写了会jquery,来问你个简单的jquery问题 :jQuery支不支持css引入. 呵呵 面: ...
- apache cgi 模块安装
apache安装,请参照:http://httpd.apache.org/docs/2.4/ 安装系统: Fedora release 21 (Twenty One) (x64) 版本:Server ...
- 跨多种环境部署 Gearman -改善应用程序性能和降低服务器负载
您可能想要将工作扩散到一个大型机器群体中,或者想要在不同语言和环境之间共享功能,那么开放源码的 Gearman 服务可以让您轻松地将工作分布到网络中的其他机器.本文将介绍 Gearman 的一些典型应 ...
- Django学习之 - 基础模板语言
模板语言if/else/endif {% if today_is_weekend %} <p>Welcome to the weekend!</p> {% else %} &l ...
- The Bottom of a Graph
poj——The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- 2018.11.3 PION模拟赛
期望:100 实际:100 #include<cstdio> #include<cstring> #include<iostream> #include< ...
- ASCII流程图
http://asciiflow.com/ http://www.torchsoft.com/en/aas_information.html
- Linux上设置RAID 10
RAID 10(又叫RAID 1+0或镜像条带)阵列结合了RAID 0和RAID 1两者的功能特性,从而提供了高性能.容错的磁盘输入/输出操作.在RAID 0中,读取/写入操作跨多个驱动器并路执行:在 ...
- iOS设计模式 - (1)概述
近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...