Codevs-1521 华丽的吊灯

1521 华丽的吊灯

时间限制: 1 s
空间限制: 256000 KB
题目等级 : 大师 Master

题目描述 Description
著名的Microhardware公司即将迎来其创业50周年庆典,为了使这次庆典能够体面而又隆重,以显出公司在国际硬件的龙头地位,总裁决定举办一次交谊舞会,届时将有社会各界名流前来捧场,希望以此来提高本公司的名望。他将布置场地的任务交给了JYY,而JYY遇到了一个小小的问题——吊灯。
在当前的经济环境下,JYY为了省钱,从一个不知名的小吊灯商那里购来一批吊灯,但是他发现并不能直接把这吊灯挂起来:只有一个吊灯能挂在天花板上,而其他所有的灯只能固定的挂在某一个别的吊灯上(可恶的奸商~…好在没有什么吊灯A只能挂在吊灯B上,而吊灯B却也只能挂在吊灯A上)。众所周知,每个吊灯都有其本身的重量,也有一定的承受能力(如果某一个下面吊的东西太多的话,那么Microhardware公司就得给舞者准备保险金和医疗金了),并且,不是所有的吊灯亮度都一样的。JYY希望能够选出其中的一些吊灯吊起来,每个灯下面所吊的都在其重力承受范围之内,且使所有灯的亮度之和最大,JYY要求你帮他解决这个问题(我不保证他会给你工钱,但是如果你不做的就会被公司解雇)。

输入描述 Input Description
输入共包含n+1行:
第一行一个整数n(n≤400)。 以后的n行每行四个整数t、w、p、l,第i+1行的t(t < i)表示第i盏灯只能吊在第t盏灯下面,w(0≤w≤200)表示第i盏灯的重量,p(p <= 200)表示第i盏灯所能吊起的最大重力,l(l≤10000)表示第i盏灯的亮度。
注意:第1盏灯的t=0。

输出描述 Output Description
输出共包含2行:
第一行两个整数m、maxl,m为所选中的吊灯的数量,maxl为最大的亮度。
第二行共包含m个整数,分别为被选中的吊灯的编号,按升序输出,且每两个之间用空格隔开(末尾无多余空格);如果问题有多解,只需输出其中的一种即可。

样例输入 Sample Input
5
0 100 100 100
1 50 50 50
1 50 50 50
2 30 50 60
2 25 50 50

样例输出 Sample Output
3 210
1 2 4

Codevs已挂,没有链接
明显是一个树上背包问题,详细注释见代码(不是我的)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <cstring>
#include <algorithm>
#define rint register int
#define ll long long
using namespace std;
template <typename xxx> inline void read(xxx &x)
{
	int f = 1;x = 0;
	char c = getchar();
	for(; c < '0' || c > '9' ; c = getchar()) if(c=='-') f = -1;
	for(;'0' <= c && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	x *= f;
}
template <typename xxx> inline void print(xxx x)
{
	if(x < 0) {
		putchar('-');
		x = -x;
	}
	if(x > 9) print(x/10);
	putchar(x % 10 + '0');
}
const int inf = 0x7fffffff;
const int maxn = 402;
const int mod = 1e9+7;
int n,m;
struct edge{
	int to,last;
}e[maxn];
int head[maxn],tot;
inline void add(int from,int to) {
	++tot;
	e[tot].to = to;
	e[tot].last = head[from];
	head[from] = tot;
}
int f[maxn][maxn],sz[maxn];//以i为根的子树负重为j时的最大亮度
int p[maxn],w[maxn],l[maxn];
inline bool gm(int &x,int val) {//神奇的判断+赋值函数
	if(val > x) {
		x = val;
		return 1;
	}
	return 0;
}
vector< pair<int,int> >pre[maxn][222];//根为i载重为j时做出贡献的子树下标与子树重量
inline void dfs_dp(int x) {
	f[x][0] = l[x];//自己的重量不是靠自己解决,所以载重为0时起码有自己的光亮
	sz[x] = w[x];
	for(rint i = head[x];i;i=e[i].last) {
		int to = e[i].to;
		dfs_dp(to);
		sz[x] += sz[to];
		for(rint j = min(p[x],sz[x]); j >= 0; --j) {//由01背包得应倒序枚举限制 
			for(rint k = 0;k <= p[to] && k <= j; ++k) {//枚举下面挂的灯的重量 
				if(k + w[to] <= j) {
					if(gm(f[x][j],f[x][j - k - w[to]] + f[to][k])) {
						pre[x][j] = pre[x][j - k -w[to]];
						pre[x][j].push_back(make_pair(to,k));
					}
				}
			}
		}
	}
	return ;
}
vector<int>sol;
inline void gp(int i,int j) {
	int len = (int)pre[i][j].size();
	for(rint k = 0;k < len; ++k) gp(pre[i][j][k].first,pre[i][j][k].second);
	sol.push_back(i);
	return ;
}
int main()
{
	read(n);
	for(rint i = 1;i <= n; ++i) {
		int t;
		read(t);read(w[i]);read(p[i]);read(l[i]);
		add(t,i);
	}
	memset(f,0xcf,sizeof(f));
	dfs_dp(1);
	int ans = -inf,k = -1;
	for(rint i = 0;i <= p[1]; ++i)
		if(gm(ans,f[1][i])) 
			k = i;
	if(~k) gp(1,k);
	stable_sort(sol.begin(),sol.end());
	int len = (int)sol.size();
	printf("%d %d
",len,ans);
	for(rint i = 0;i < len; ++i) print(sol[i]),putchar(' ');
	return 0;
}
/*
有一个问题就是dp[i][j]如果表示最多重量为j的价值时,可能有实际重量不等于标出重量j
所以我们设dp[i][j]为重量刚好为j时的价值 
*/


原文地址:https://www.cnblogs.com/Thomastine/p/11740581.html