多重背包问题1、2

it2026-10-05  1

所有背包思想来自闫总的ACWing


题目链接:https://www.acwing.com/problem/content/4/

#mermaid-svg-Mnw8EpqZyIANoNjb .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .label text{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .node rect,#mermaid-svg-Mnw8EpqZyIANoNjb .node circle,#mermaid-svg-Mnw8EpqZyIANoNjb .node ellipse,#mermaid-svg-Mnw8EpqZyIANoNjb .node polygon,#mermaid-svg-Mnw8EpqZyIANoNjb .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Mnw8EpqZyIANoNjb .node .label{text-align:center;fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .node.clickable{cursor:pointer}#mermaid-svg-Mnw8EpqZyIANoNjb .arrowheadPath{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-Mnw8EpqZyIANoNjb .flowchart-link{stroke:#333;fill:none}#mermaid-svg-Mnw8EpqZyIANoNjb .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-Mnw8EpqZyIANoNjb .edgeLabel rect{opacity:0.9}#mermaid-svg-Mnw8EpqZyIANoNjb .edgeLabel span{color:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-Mnw8EpqZyIANoNjb .cluster text{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-Mnw8EpqZyIANoNjb .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Mnw8EpqZyIANoNjb text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-Mnw8EpqZyIANoNjb .actor-line{stroke:grey}#mermaid-svg-Mnw8EpqZyIANoNjb .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-Mnw8EpqZyIANoNjb #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .sequenceNumber{fill:#fff}#mermaid-svg-Mnw8EpqZyIANoNjb #sequencenumber{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb #crosshead path{fill:#333;stroke:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .messageText{fill:#333;stroke:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-Mnw8EpqZyIANoNjb .labelText,#mermaid-svg-Mnw8EpqZyIANoNjb .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-Mnw8EpqZyIANoNjb .loopText,#mermaid-svg-Mnw8EpqZyIANoNjb .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-Mnw8EpqZyIANoNjb .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-Mnw8EpqZyIANoNjb .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Mnw8EpqZyIANoNjb .noteText,#mermaid-svg-Mnw8EpqZyIANoNjb .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-Mnw8EpqZyIANoNjb .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-Mnw8EpqZyIANoNjb .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-Mnw8EpqZyIANoNjb .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-Mnw8EpqZyIANoNjb .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .section{stroke:none;opacity:0.2}#mermaid-svg-Mnw8EpqZyIANoNjb .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-Mnw8EpqZyIANoNjb .section2{fill:#fff400}#mermaid-svg-Mnw8EpqZyIANoNjb .section1,#mermaid-svg-Mnw8EpqZyIANoNjb .section3{fill:#fff;opacity:0.2}#mermaid-svg-Mnw8EpqZyIANoNjb .sectionTitle0{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .sectionTitle1{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .sectionTitle2{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .sectionTitle3{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-Mnw8EpqZyIANoNjb .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .grid path{stroke-width:0}#mermaid-svg-Mnw8EpqZyIANoNjb .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-Mnw8EpqZyIANoNjb .task{stroke-width:2}#mermaid-svg-Mnw8EpqZyIANoNjb .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .taskText:not([font-size]){font-size:11px}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-Mnw8EpqZyIANoNjb .task.clickable{cursor:pointer}#mermaid-svg-Mnw8EpqZyIANoNjb .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-Mnw8EpqZyIANoNjb .taskText0,#mermaid-svg-Mnw8EpqZyIANoNjb .taskText1,#mermaid-svg-Mnw8EpqZyIANoNjb .taskText2,#mermaid-svg-Mnw8EpqZyIANoNjb .taskText3{fill:#fff}#mermaid-svg-Mnw8EpqZyIANoNjb .task0,#mermaid-svg-Mnw8EpqZyIANoNjb .task1,#mermaid-svg-Mnw8EpqZyIANoNjb .task2,#mermaid-svg-Mnw8EpqZyIANoNjb .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutside0,#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutside2{fill:#000}#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutside1,#mermaid-svg-Mnw8EpqZyIANoNjb .taskTextOutside3{fill:#000}#mermaid-svg-Mnw8EpqZyIANoNjb .active0,#mermaid-svg-Mnw8EpqZyIANoNjb .active1,#mermaid-svg-Mnw8EpqZyIANoNjb .active2,#mermaid-svg-Mnw8EpqZyIANoNjb .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-Mnw8EpqZyIANoNjb .activeText0,#mermaid-svg-Mnw8EpqZyIANoNjb .activeText1,#mermaid-svg-Mnw8EpqZyIANoNjb .activeText2,#mermaid-svg-Mnw8EpqZyIANoNjb .activeText3{fill:#000 !important}#mermaid-svg-Mnw8EpqZyIANoNjb .done0,#mermaid-svg-Mnw8EpqZyIANoNjb .done1,#mermaid-svg-Mnw8EpqZyIANoNjb .done2,#mermaid-svg-Mnw8EpqZyIANoNjb .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-Mnw8EpqZyIANoNjb .doneText0,#mermaid-svg-Mnw8EpqZyIANoNjb .doneText1,#mermaid-svg-Mnw8EpqZyIANoNjb .doneText2,#mermaid-svg-Mnw8EpqZyIANoNjb .doneText3{fill:#000 !important}#mermaid-svg-Mnw8EpqZyIANoNjb .crit0,#mermaid-svg-Mnw8EpqZyIANoNjb .crit1,#mermaid-svg-Mnw8EpqZyIANoNjb .crit2,#mermaid-svg-Mnw8EpqZyIANoNjb .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-Mnw8EpqZyIANoNjb .activeCrit0,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCrit1,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCrit2,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-Mnw8EpqZyIANoNjb .doneCrit0,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCrit1,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCrit2,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-Mnw8EpqZyIANoNjb .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-Mnw8EpqZyIANoNjb .milestoneText{font-style:italic}#mermaid-svg-Mnw8EpqZyIANoNjb .doneCritText0,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCritText1,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCritText2,#mermaid-svg-Mnw8EpqZyIANoNjb .doneCritText3{fill:#000 !important}#mermaid-svg-Mnw8EpqZyIANoNjb .activeCritText0,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCritText1,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCritText2,#mermaid-svg-Mnw8EpqZyIANoNjb .activeCritText3{fill:#000 !important}#mermaid-svg-Mnw8EpqZyIANoNjb .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-Mnw8EpqZyIANoNjb g.classGroup text .title{font-weight:bolder}#mermaid-svg-Mnw8EpqZyIANoNjb g.clickable{cursor:pointer}#mermaid-svg-Mnw8EpqZyIANoNjb g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Mnw8EpqZyIANoNjb g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-Mnw8EpqZyIANoNjb .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-Mnw8EpqZyIANoNjb .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Mnw8EpqZyIANoNjb .dashed-line{stroke-dasharray:3}#mermaid-svg-Mnw8EpqZyIANoNjb #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb .commit-id,#mermaid-svg-Mnw8EpqZyIANoNjb .commit-msg,#mermaid-svg-Mnw8EpqZyIANoNjb .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-Mnw8EpqZyIANoNjb g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-Mnw8EpqZyIANoNjb g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-Mnw8EpqZyIANoNjb g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-Mnw8EpqZyIANoNjb .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-Mnw8EpqZyIANoNjb .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-Mnw8EpqZyIANoNjb .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-Mnw8EpqZyIANoNjb .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-Mnw8EpqZyIANoNjb .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-Mnw8EpqZyIANoNjb .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-Mnw8EpqZyIANoNjb .edgeLabel text{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-Mnw8EpqZyIANoNjb .node circle.state-start{fill:black;stroke:black}#mermaid-svg-Mnw8EpqZyIANoNjb .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-Mnw8EpqZyIANoNjb #statediagram-barbEnd{fill:#9370db}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-state .divider{stroke:#9370db}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-Mnw8EpqZyIANoNjb .note-edge{stroke-dasharray:5}#mermaid-svg-Mnw8EpqZyIANoNjb .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-Mnw8EpqZyIANoNjb .error-icon{fill:#522}#mermaid-svg-Mnw8EpqZyIANoNjb .error-text{fill:#522;stroke:#522}#mermaid-svg-Mnw8EpqZyIANoNjb .edge-thickness-normal{stroke-width:2px}#mermaid-svg-Mnw8EpqZyIANoNjb .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-Mnw8EpqZyIANoNjb .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-Mnw8EpqZyIANoNjb .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-Mnw8EpqZyIANoNjb .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-Mnw8EpqZyIANoNjb .marker{fill:#333}#mermaid-svg-Mnw8EpqZyIANoNjb .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-Mnw8EpqZyIANoNjb { color: rgba(0, 0, 0, 0.75); font: ; } DP 状态表示 选择集合中前i个元素耗费不超过j的方案 属性:Max 计算 (对于第i个元素) f(i,j)=max{f(i-1,j),f(i-1,j-v)+w,f(i-1,j-2v)+2w...}(注意小于s即可)

朴素算法:

#include<iostream> using namespace std; int N,V; int dp[3000]; int main(){ cin>>N>>V; int v,w,s; for(int i=0;i<N;i++){ cin>>v>>w>>s; for(int j=V;j>=0;j--){ for(int k=1;k*v<=j&&k<=s;k++){ dp[j]=max(dp[j],dp[j-k*v]+k*w); } } } cout<<dp[V]<<endl; return 0; }

二进制优化算法: ①把一个数x分解为:1+2+4+…+(x-1-2-4) 括号内数值大于0 ②多重背包——>化为01背包问题

/* 多重背包的二进制优化 二进制优化 1+2+4+...+(n-1-2-4-...) */ #include<iostream> #include<vector> using namespace std; struct Good{ int v,w; }; int dp[20020]; vector<Good>goods; int main(){ int n,m; cin>>n>>m; for(int i=0;i<n;i++){ int v,w,s; cin>>v>>w>>s; for(int k=1;s>=k;k*=2){ s-=k; goods.push_back({k*v,k*w}); } if(s>0)goods.push_back({s*v,s*w}); } for(auto good: goods) for(int j=m;j>=good.v;j--) dp[j]=max(dp[j],dp[j-good.v]+good.w); cout<<dp[m]<<endl; return 0; }
最新回复(0)