Codevs P1155 金明的预算方案

来源:互联网 发布:java运行class文件原理 编辑:程序博客网 时间:2024/05/21 04:17

Codevs P1155 金明的预算方案


题目描述 Description

金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”。今天一早,金明就开始做预算了,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:

主件 附件 电脑 打印机,扫描仪 书柜 图书 书桌 台灯,文具 工作椅 无

如果要买归类为附件的物品,必须先买该附件所属的主件。每个主件可以有0个、1个或2个附件。附件不再有从属于自己的附件。金明想买的东西很多,肯定会超过妈妈限定的N元。于是,他把每件物品规定了一个重要度,分为5等:用整数1~5表示,第5等最重要。他还从因特网上查到了每件物品的价格(都是10元的整数倍)。他希望在不超过N元(可以等于N元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第j件物品的价格为v[j],重要度为w[j],共选中了k件物品,编号依次为j1,j2,……,jk,则所求的总和为:
v[j1]w[j1]+v[j2]*w[j2]+ …+v[jk]*w[jk]。(其中为乘号)
请你帮助金明设计一个满足要求的购物单。


输入输出 Input&Output


输入描述 Input Description

第1行,为两个正整数,用一个空格隔开:
N m
(其中N(<32000)表示总钱数,m(<60)为希望购买物品的个数。)
从第2行到第m+1行,第j行给出了编号为j-1的物品的基本数据,每行有3个非负整数
v p q
(其中v表示该物品的价格(v<10000),p表示该物品的重要度(1~5),q表示该物品是主件还是附件。如果q=0,表示该物品为主件,如果q>0,表示该物品为附件,q是所属主件的编号)


输出描述 Output Description

只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值(<200000)

样例


样例输入 Sample Input

1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0


样例输出 Sample Output

2200


数据范围及提示 Data Size & Hint


分析

显然该题目是一个依赖背包,但由于其题设为了简化题目使得以来背包可以转换为分组背包
且组内仅能选一件,那么我们先枚举物品组,然后再枚举当前体积,也就是确定当前状态,再枚举组内物品,这样在该体积下枚举组内物品也就是说,状态确定情况下,这种状态是选哪一件,所以只选了一件组内物品。


代码如下

program p1155;type rec=record      v,p,q:longint;      attachment:array[0..2] of longint;     end;var n,m,i,j,k,v,p,q,sum:longint;    father:array[1..60] of longint;    item:array[1..60] of rec;    item_group:array[1..60,1..1000] of record                                        v,p:longint;                                       end;    total:array[1..60] of longint;    f:array[0..10000] of longint;function max(a,b:longint):longint;begin if a>b then exit(a); exit(b);end;begin readln(n,m); for i:=1 to m do  begin   begin    readln(v,p,q);    if q=0     then      begin       item[i].v:=v;       item[i].p:=p;       item[i].q:=i;      end     else      begin       item[i].v:=v;       item[i].p:=p;       item[i].q:=item[q].q;       inc(item[q].attachment[0]);       item[q].attachment[item[q].attachment[0]]:=i;      end;   end;  end; fillchar(total,sizeof(total),0); sum:=0; for i:=1 to m do  begin   if item[i].q=i    then     begin      inc(sum);      inc(total[sum]);      item_group[sum,total[sum]].v:=item[i].v;      item_group[sum,total[sum]].p:=item[i].p*item[i].v;      for j:=1 to item[i].attachment[0] do       begin        inc(total[sum]);        item_group[sum,total[sum]].v:=item[i].v+item[item[i].attachment[j]].v;        item_group[sum,total[sum]].p:=item[i].p*item[i].v+item[item[i].attachment[j]].p*item[item[i].attachment[j]].v;       end;      if item[i].attachment[0]>1       then        begin         for j:=1 to item[i].attachment[0] do          for k:=1 to item[i].attachment[0] do           begin            if j<k             then              begin               inc(total[sum]);               item_group[sum,total[sum]].v:=item[i].v+item[item[i].attachment[j]].v+item[item[i].attachment[k]].v;               item_group[sum,total[sum]].p:=item[i].p*item[i].v+item[item[i].attachment[j]].p*item[item[i].attachment[j]].v+item[item[i].attachment[k]].p*item[item[i].attachment[k]].v;              end;           end;        end;     end;  end; for i:=1 to sum do  for k:=n downto 0 do   for j:=1 to total[i] do    begin     if k-item_group[i,j].v>=0 then     f[k]:=max(f[k],f[k-item_group[i,j].v]+item_group[i,j].p);    end; write(f[n]);end.

评测结果

测试通过 Accepted

总耗时: 41 ms
0 / 0 数据通过测试.
运行结果
测试点#budget1.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#budget10.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#budget2.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#budget3.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#budget4.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
测试点#budget5.in 结果:AC 内存使用量: 256kB 时间使用量: 0ms
测试点#budget6.in 结果:AC 内存使用量: 368kB 时间使用量: 4ms
测试点#budget7.in 结果:AC 内存使用量: 368kB 时间使用量: 4ms
测试点#budget8.in 结果:AC 内存使用量: 368kB 时间使用量: 12ms
测试点#budget9.in 结果:AC 内存使用量: 492kB 时间使用量: 20ms


总结

看书要认真。。。

Drenched –曲婉婷

When minutes become hours
当须臾化作长久  
When days become years  
当昼夜渐成四季。  
And I don’t know where you are  
你却依旧无处可寻  
Color seems so dull without you  
没有你,斑斓也失去了色彩  
Have we lost our minds?  
我们都疯了么?  
What have we done  
我们到底做了什么?  
But it all doesn’t seem to matter anymore  
但一切怀疑早已经 无关痛痒。  
When you kissed me on that street, I kissed you back  
只因在街的那边,你我的那一吻 
You held me in your arms, I held you in mine  
你揽我入怀 不自觉的相拥。  
You picked me up to lay me down  
希冀与失落 都源自于你。  
When I look into your eyes  
当我凝视你的双眸,  
I can hear you cry for a little bit more of you and I  
我能听到你的泪水里 有你我不能掌控的无奈。  
I’m drenched in your love  
不禁沉溺于你给的爱。  
I’m no longer able to hold it back  
让我再也不可能 将一切挽回。  
Is it too late to ask for love?  
是否对爱渴求已为时太晚?  
Is it wrong to feel right?  
是否这一切 似是而非?  
When the world is winding down  
可是当周遭尘埃落定。  
Thoughts of you linger around  
而我的脑海里只剩下你逡巡徘徊。  
Have we lost our minds?  
是我们都疯了么? 
What have we done?  
我们到底做了什么?  
But it all doesn’t seem to matter anymore  
但这一切早已经变得 无关痛痒  
When you kissed me on that street, I kissed you back  
只因在街的那边 你我的那一吻。  
You held me in your arms, I held you in mine  
你揽我入怀 不自觉的相拥  
You picked me up to lay me down  
希冀与失落 都源自你。  
When I look into your eyes  
当我凝视你的双眸。  
I can hear you cry for a little bit more of you and I  
我能听到你的泪水里 有你我不能掌控的无奈。  
I’m drenched in your love  
不禁沉溺于你给的爱 
I’m no longer able to hold it back  
让我再也不可能 将一切挽回。
这里写图片描述

0 0
原创粉丝点击