【题目描述】
DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars。DotR里面的英雄只有一个属性——力量。他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄的力量值等于它购买的所有装备的力量值之和。装备分为基本装备和高级装备两种。基本装备可以直接从商店里面用金币购买,而高级装备需要用基本装备或者较低级的高级装备来合成,合成不需要附加的金币。装备的合成路线可以用一棵树来表示。比如,Sange and Yasha的合成需要Sange, Yasha和Sange and Yasha Recipe Scroll三样物品。其中Sange又要用Ogre Axe, Belt of Giant Strength 和 Sange Recipe Scroll合成。每件基本装备都有数量限制,这限制了你不能无限制地合成某些性价比很高的装备。现在,英雄Spectre有M个金币,他想用这些钱购买装备使自己的力量值尽量高。你能帮帮他吗?他会教你魔法Haunt(幽灵附体)作为回报的。
【输入格式】
输入文件第一行包含两个整数,N (1 <= n <= 51) 和 m (0 <= m <= 2,000)。分别表示装备的种类数和金币数。装备用1到N的整数编号。接下来的N行,按照装备1到装备n的顺序,每行描述一种装备。每一行的第一个正整数表示这个装备贡献的力量值。接下来的非空字符表示这种装备是基本装备还是高级装备,A表示高级装备,B表示基本装备。如果是基本装备,紧接着的两个正整数分别表示它的单价(单位为金币)和数量限制(不超过100)。如果是高级装备,后面紧跟着一个正整数C,表示这个高级装备需要C种低级装备。后面的2C个数,依次描述某个低级装备的种类和需要的个数。
【输出格式】
第一行包含一个整数S,表示最多可以提升多少点力量值。
【样例输入】
10 59 5 A 3 6 1 9 2 10 1 1 B 5 3 1 B 4 3 1 B 2 3 8 A 3 2 1 3 1 7 1 1 B 5 3 5 B 3 3 15 A 3 1 1 5 1 4 1 1 B 3 5 1 B 4 3
【样例输出】
33
首先我要说我觉得这道题并不好,因为它目前并没有合理的多项式时间算法,我感觉应该以后也不会有吧。。
这道题吧,非常的奇怪,所有的题解的时间复杂度都是O(100*n*m*m)≈10^10的,但是它们都A了。。这数据实在太弱了。。
先来讲讲泛化背包的优化,其实就是说如果有一连串一样的,那么显然我们只需要保存一个。我一开始写的非常蛋疼,写成了类似链表的样子,还要各种加1减1,后来参观了vfk的,原来把一连串相同的都置成0,只留最前面一个就好了。
vfk的题解还提到了一种方法就是倒着做——逆向思维!这是我没有想到的,补集转换有很多好处。其中最妙的地方,就是我们只需要求出一棵树的树根能拆分出的最大价值,然后反复加回去就可以了。这样的话,对于每一棵树,实际上我们都只需要求出它们选至多根之后的残量和单位泛化背包而已,并不需要太多的中间过程,使得可以很轻易地过掉普通数据。
但是这并不能降低时间复杂度,所以如果遇到残量异常充足的数据,就会被轻易卡飞了,所以下面是一个很强的数据(是我能出的最强的了。。),在我能找到的所有题解中,vfk是跑的最快的,在我的机子上它跑了2.5s:
51 2000
19 B 1 100
46 B 4 100
36 B 5 100
42 B 4 100
13 B 4 100
21 B 2 100
33 B 5 100
26 B 2 100
45 B 4 100
46 B 2 100
9 B 2 100
8 B 4 100
26 B 4 100
27 B 4 100
36 B 3 100
16 B 1 100
42 B 3 100
14 B 2 100
38 B 5 100
22 B 5 100
90 A 20 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1
8 B 4 100
23 B 2 100
9 B 3 100
36 B 1 100
48 B 5 100
6 B 4 100
1 B 2 100
10 B 4 100
6 B 3 100
22 B 5 100
32 B 3 100
18 B 2 100
21 B 4 100
15 B 2 100
21 B 5 100
28 B 5 100
11 B 2 100
44 B 5 100
8 B 1 100
22 B 4 100
75 A 20 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1
15 B 2 100
45 B 4 100
47 B 1 100
24 B 2 100
46 B 1 100
46 B 3 100
21 B 5 100
60 A 7 43 1 44 1 45 1 46 1 47 1 48 1 49 1
14 A 3 21 100 42 100 50 100
我的代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define inf 0x7fffffff
int f[55][105][2005];
#include<vector>
int sn[55][55],sm[55][55],num[55];
int sum[55],sc[55],sw[55];
int c[55],w[55],p[105];
int csum[55];
int m;
inline int max(int a,int b){
return a>b?a:b;
}
inline int min(int a,int b){
return a<b?a:b;
}
int pred[55][105][2005];
inline void dfs(int node){
if(p[node]){
for(int i=1,j,tmp=0;i<=p[node];++i){
tmp+=w[node];
for(j=c[node]*i;j<=m;++j)f[node][i][j]=tmp;
}
for(int i=p[node],j;i--;)
for(j=m;j;--j)
f[node][i][j]=max(f[node][i][j],f[node][i+1][j]);
for(int k=p[node],j;k>=0;--k)
for(j=1;j<=m;++j)
pred[node][k][j]=f[node][k][j-1]==f[node][k][j]?pred[node][k][j-1]:j-1;
csum[node]=p[node]*c[node];
}
else{
p[node]=inf;
int i,j,k,o,nowc=0,noww=0;
for(i=0;i<num[node];++i)dfs(sn[node][i]);
for(i=0;i<num[node];++i){
p[node]=min(p[node],p[sn[node][i]]/sm[node][i]);
//cout<<p[sn[node][i]]<<" "<<sm[node][i]<<endl;
c[node]+=sm[node][i]*c[sn[node][i]];
csum[node]=min(m+1,csum[node]+csum[sn[node][i]]);
}
csum[node]=min(csum[node],m+1);
memset(sum,0,num[node]*sizeof(int));
memset(sc,0,num[node]*sizeof(int));
memset(sw,0,num[node]*sizeof(int));
for(k=0;k<=p[node];++k){
//cout<<k<<" "<<p[node]<<endl;
for(j=nowc;j<=m;++j)f[node][k][j]=noww;
for(i=0;i<num[node];++i)
for(j=csum[node];j>nowc;--j){
for(o=max(pred[sn[node][i]][sum[i]][j-nowc+sc[i]]+1-sc[i],1);o>0;){
f[node][k][j]=max(f[node][k][j],f[node][k][j-o]+f[sn[node][i]][sum[i]][o+sc[i]]-sw[i]);
o=o-1>0?o=max(pred[sn[node][i]][sum[i]][o+sc[i]-1]-sc[i]+1,1):o-1;
}
}
for(i=0;i<num[node];++i)sum[i]+=sm[node][i],sc[i]+=c[sn[node][i]]*sm[node][i],sw[i]+=w[sn[node][i]]*sm[node][i];
nowc+=c[node],noww+=w[node];
}
for(j=csum[node];++j<=m;)f[node][p[node]][j]=f[node][p[node]][j-1];
for(k=p[node];k--;){
for(j=csum[node];j;--j)f[node][k][j]=max(f[node][k][j],f[node][k+1][j]);
for(j=csum[node];++j<=m;)f[node][k][j]=f[node][k][j-1];
}
for(k=p[node];k>=0;--k)
for(j=1;j<=m;++j)
pred[node][k][j]=f[node][k][j-1]==f[node][k][j]?pred[node][k][j-1]:j-1;
}
}
int main(){
freopen("bzoj_1017.in","r",stdin);
//freopen("bzoj_1017.out","w",stdout);
int n;
scanf("%d%d",&n,&m);
int i;
char chr;
int j;
bool root[55]={0};
for(i=1;i<=n;++i){
scanf("%d",w+i);
chr=getchar();
while(chr<'A'||chr>'B')chr=getchar();
if(chr=='A'){
scanf("%d",num+i);
for(j=0;j<num[i];++j)scanf("%d%d",sn[i]+j,sm[i]+j),root[sn[i][j]]=1;
}
else scanf("%d%d",c+i,p+i);
}
memset(sm[0],127,sizeof(sm[0]));
for(i=1;i<=n;++i)
if(!root[i])
sn[0][num[0]++]=i;
dfs(0);
printf("%d\n",f[0][0][m]);
}
vfk的代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
const int MaxN = 51;
const int MaxM = 2000;
struct equipment
{
int energy;
int nNeed;
int needIndex[MaxN];
int needSum[MaxN];
int money;
int maxsum;
char type;
friend inline istream& operator>>(istream &in, equipment &e)
{
in >> e.energy >> e.type;
switch (e.type)
{
case 'A':
in >> e.nNeed;
for (int i = 0; i < e.nNeed; i++)
in >> e.needIndex[i] >> e.needSum[i];
break;
case 'B':
e.nNeed = 0;
in >> e.money >> e.maxsum;
break;
}
return in;
}
};
equipment e[MaxN + 1];
int n, m;
int f[MaxN + 1][MaxM + 1];
int g[MaxN + 1][MaxM + 1];
template <class T>
inline bool tension(T &a, const T &b)
{
if (b < a)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool relax(T &a, const T &b)
{
if (b > a)
{
a = b;
return true;
}
return false;
}
void getInformation(int idx)
{
equipment *cur = &e[idx];
if (cur->type == 'A')
{
cur->money = 0;
cur->maxsum = INT_MAX;
for (int i = 0; i < cur->nNeed; i++)
{
getInformation(cur->needIndex[i]);
cur->money = min(cur->money+e[cur->needIndex[i]].money * cur->needSum[i],m+1);
tension(cur->maxsum, e[cur->needIndex[i]].maxsum / cur->needSum[i]);
}
tension(cur->maxsum, m / cur->money);
}
}
inline bool relaxDP(int *a, int *b)
{
bool updated = false;
int tot=0;
for(int i=0;i<m;++i)tot+=(bool)a[i];
//printf("%d\n",tot);
for (int i = m - 1; i >= 0; i--)
if (a[i] > 0 || i == 0)
for (int j = 1; i + j <= m; j++)
if (b[j] > 0 && relax(a[i + j], a[i] + b[j]))
updated = true;
int maxV = 0;
for (int i = 0; i <= m; i++)
{
if (a[i] <= maxV)
a[i] = 0;
else
maxV = a[i];
}
return updated;
}
void dfs(int idx, int sum)
{
equipment *cur = &e[idx];
for (int i = 0; i < e[idx].nNeed; i++)
dfs(cur->needIndex[i], cur->maxsum * cur->needSum[i]);
//cout<<"-------"<<idx<<"-------\n";
int *curF = f[idx], *curG = g[idx];
static int h[MaxM + 1];
fill(h, h + m + 1, 0);
for (int i = 0 ; i < cur->nNeed; i++)
{
relaxDP(h, f[cur->needIndex[i]]);
for (int j = 0; j < cur->needSum[i]; j++)
if (!relaxDP(curG, g[cur->needIndex[i]]))
break;
}
//cout<<"---\n";
bool updated = true;
for (int i = cur->maxsum - sum; i >= 0; i--)
{
int moneyi = cur->money * i, energyi = cur->energy * i;
for (int j = moneyi; j <= m; j++)
relax(curF[j], h[j - moneyi] + energyi);
if (updated)
updated = relaxDP(h, curG);
}
if (cur->money <= m)
relax(curG[cur->money], cur->energy);
}
inline int handle(int root)
{
getInformation(root);
dfs(root, 0);
return *max_element(f[root], f[root] + m + 1);
}
int main()
{
freopen("bzoj_1017.in", "r", stdin);
//freopen("1017.out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> e[i];
static bool isRoot[MaxN + 1];
fill(isRoot + 1, isRoot + n + 1, true);
for (int i = 1; i <= n; i++)
if (e[i].type == 'A')
for (int j = 0; j < e[i].nNeed; j++)
isRoot[e[i].needIndex[j]] = false;
int root = find(isRoot + 1, isRoot + n + 1, true) - isRoot;
cout << handle(root) << endl;
return 0;
}