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
讓我再也不可能 將一切挽回。