{"id":41,"date":"2026-04-05T16:01:57","date_gmt":"2026-04-05T08:01:57","guid":{"rendered":"https:\/\/blog.sjll.top\/?p=41"},"modified":"2026-04-05T16:01:57","modified_gmt":"2026-04-05T08:01:57","slug":"%e5%88%86%e7%bb%84%e8%83%8c%e5%8c%85%e9%97%ae%e9%a2%98%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e8%af%a6%e8%a7%a3%ef%bc%9ac%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.sjll.top\/index.php\/2026\/04\/05\/%e5%88%86%e7%bb%84%e8%83%8c%e5%8c%85%e9%97%ae%e9%a2%98%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e8%af%a6%e8%a7%a3%ef%bc%9ac%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%ba%94%e7%94%a8\/","title":{"rendered":"\u5206\u7ec4\u80cc\u5305\u95ee\u9898\u52a8\u6001\u89c4\u5212\u8be6\u89e3\uff1aC++\u5b9e\u73b0\u4e0e\u5e94\u7528"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u5206\u7ec4\u80cc\u5305\uff1f<\/h2>\n\n\n\n<p><strong>\u5206\u7ec4\u80cc\u5305\uff08Group Knapsack\uff09<\/strong> \u662f 0-1 \u80cc\u5305\u7684\u6269\u5c55\u3002\u5728\u5206\u7ec4\u80cc\u5305\u4e2d\uff0c\u7269\u54c1\u88ab\u5206\u6210\u82e5\u5e72\u7ec4\uff0c\u6bcf\u7ec4\u5185\u7684\u7269\u54c1\u4e92\u65a5\u2014\u2014\u6bcf\u7ec4\u6700\u591a\u53ea\u80fd\u9009\u62e9<strong>\u4e00\u4ef6<\/strong>\u7269\u54c1\uff08\u6216\u4e0d\u9009\uff09\u3002\u8fd9\u662f\u5b9e\u9645\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u4e00\u7c7b\u51b3\u7b56\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ee\u9898\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u6709 n \u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a W \u7684\u80cc\u5305\u3002\u7b2c i \u7ec4\u6709 k[i] \u4ef6\u7269\u54c1\uff0c\u7b2c i \u7ec4\u7684\u7b2c j \u4ef6\u7269\u54c1\u91cd\u91cf\u4e3a w[i][j]\uff0c\u4ef7\u503c\u4e3a v[i][j]\u3002\u6bcf\u7ec4\u6700\u591a\u9009\u4e00\u4ef6\uff0c\u6c42\u6700\u5927\u4ef7\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u52a8\u6001\u89c4\u5212\u89e3\u6cd5<\/h3>\n\n\n\n<p>\u5b9a\u4e49 <code>dp[i][w]<\/code> \u8868\u793a\uff1a\u8003\u8651\u524d i \u7ec4\u7269\u54c1\uff0c\u80cc\u5305\u5bb9\u91cf\u4e3a w \u65f6\u7684\u6700\u5927\u4ef7\u503c\u3002<\/p>\n\n\n\n<p>\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dp[i][w] = max(dp[i-1][w], dp[i-1][w - w[i][j]] + v[i][j])\n          \u5176\u4e2d j \u4e3a\u7b2c i \u7ec4\u7684\u4efb\u4e00\u7269\u54c1<\/code><\/pre>\n\n\n\n<p>\u6838\u5fc3\u601d\u60f3\uff1a\u5bf9\u4e8e\u6bcf\u4e00\u7ec4\uff0c\u679a\u4e3e\u7ec4\u5185\u6240\u6709\u7269\u54c1\u7684\u9009\u62e9\u60c5\u51b5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++ \u4ee3\u7801\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\n\/\/ \u5206\u7ec4\u80cc\u5305 - \u4e8c\u7ef4 DP\nint group_knapsack_2d(const vector&lt;vector&lt;int&gt;&gt;&amp; weight, \n                      const vector&lt;vector&lt;int&gt;&gt;&amp; value, int W) {\n    int n = weight.size();  \/\/ \u7ec4\u6570\n    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(W + 1, 0));\n    \n    for (int i = 1; i &lt;= n; i++) {\n        for (int w = 0; w &lt;= W; w++) {\n            \/\/ \u9996\u5148\u9ed8\u8ba4\u4e0d\u9009\u8fd9\u4e00\u7ec4\u7684\u4efb\u4f55\u7269\u54c1\n            dp[i][w] = dp[i-1][w];\n            \n            \/\/ \u679a\u4e3e\u7b2c i \u7ec4\u7684\u6240\u6709\u9009\u62e9\n            for (int j = 0; j &lt; weight[i-1].size(); j++) {\n                if (w &gt;= weight[i-1][j]) {\n                    dp[i][w] = max(dp[i][w], \n                        dp[i-1][w - weight[i-1][j]] + value[i-1][j]);\n                }\n            }\n        }\n    }\n    \n    return dp[n][W];\n}\n\n\/\/ \u5206\u7ec4\u80cc\u5305 - \u4e00\u7ef4 DP\uff08\u7a7a\u95f4\u4f18\u5316\uff09\nint group_knapsack_1d(const vector&lt;vector&lt;int&gt;&gt;&amp; weight, \n                      const vector&lt;vector&lt;int&gt;&gt;&amp; value, int W) {\n    int n = weight.size();\n    vector&lt;int&gt; dp(W + 1, 0);\n    \n    for (int i = 0; i &lt; n; i++) {\n        \/\/ \u9006\u5e8f\u904d\u5386\u5bb9\u91cf\uff080-1\u80cc\u5305\u7279\u6027\uff0c\u6bcf\u7ec4\u6700\u591a\u9009\u4e00\u4ef6\uff09\n        for (int w = W; w &gt;= 0; w--) {\n            \/\/ \u679a\u4e3e\u7b2c i \u7ec4\u7684\u6240\u6709\u9009\u62e9\n            for (int j = 0; j &lt; weight[i].size(); j++) {\n                if (w &gt;= weight[i][j]) {\n                    dp[w] = max(dp[w], \n                        dp[w - weight[i][j]] + value[i][j]);\n                }\n            }\n        }\n    }\n    \n    return dp[W];\n}\n\n\/\/ \u8bb0\u5f55\u5177\u4f53\u7684\u9009\u62e9\u65b9\u6848\nstruct Choice {\n    int group_idx;\n    int item_idx;\n};\n\nvector&lt;Choice&gt; group_knapsack_with_choice(const vector&lt;vector&lt;int&gt;&gt;&amp; weight,\n                                          const vector&lt;vector&lt;int&gt;&gt;&amp; value, int W) {\n    int n = weight.size();\n    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(W + 1, 0));\n    \n    \/\/ \u586b\u5145 DP \u8868\n    for (int i = 1; i &lt;= n; i++) {\n        for (int w = 0; w &lt;= W; w++) {\n            dp[i][w] = dp[i-1][w];\n            for (int j = 0; j &lt; weight[i-1].size(); j++) {\n                if (w &gt;= weight[i-1][j]) {\n                    dp[i][w] = max(dp[i][w], \n                        dp[i-1][w - weight[i-1][j]] + value[i-1][j]);\n                }\n            }\n        }\n    }\n    \n    \/\/ \u56de\u6eaf\u627e\u51fa\u9009\u62e9\n    vector&lt;Choice&gt; choices;\n    int w = W;\n    for (int i = n; i &gt; 0 &amp;&amp; w &gt; 0; i--) {\n        for (int j = 0; j &lt; weight[i-1].size(); j++) {\n            if (w &gt;= weight[i-1][j] &amp;&amp; \n                dp[i][w] == dp[i-1][w - weight[i-1][j]] + value[i-1][j]) {\n                choices.push_back({i-1, j});\n                w -= weight[i-1][j];\n                break;  \/\/ \u627e\u5230\u8fd9\u4e00\u7ec4\u7684\u9009\u62e9\u540e\u9000\u51fa\n            }\n        }\n    }\n    \n    return choices;\n}\n\nint main() {\n    \/\/ \u793a\u4f8b\uff1a3\u7ec4\u7269\u54c1\n    \/\/ \u7b2c0\u7ec4\uff1a{\u624b\u673a, \u5e73\u677f} - \u53ea\u80fd\u9009\u4e00\u4e2a\n    \/\/ \u7b2c1\u7ec4\uff1a{\u8033\u673a, \u97f3\u7bb1} - \u53ea\u80fd\u9009\u4e00\u4e2a  \n    \/\/ \u7b2c2\u7ec4\uff1a{\u952e\u76d8, \u9f20\u6807} - \u53ea\u80fd\u9009\u4e00\u4e2a\n    \n    vector&lt;vector&lt;int&gt;&gt; weight = {\n        {2, 3},   \/\/ \u7b2c0\u7ec4\uff1a\u91cd\u91cf2\u62163\n        {1, 4},   \/\/ \u7b2c1\u7ec4\uff1a\u91cd\u91cf1\u62164\n        {2, 1}    \/\/ \u7b2c2\u7ec4\uff1a\u91cd\u91cf2\u62161\n    };\n    \n    vector&lt;vector&lt;int&gt;&gt; value = {\n        {3000, 4000},  \/\/ \u7b2c0\u7ec4\uff1a\u4ef7\u503c3000\u62164000\n        {500, 1500},   \/\/ \u7b2c1\u7ec4\uff1a\u4ef7\u503c500\u62161500\n        {400, 200}     \/\/ \u7b2c2\u7ec4\uff1a\u4ef7\u503c400\u6216200\n    };\n    \n    int W = 5;  \/\/ \u80cc\u5305\u5bb9\u91cf\n    \n    cout &lt;&lt; \"\u5206\u7ec4\u80cc\u5305\u793a\u4f8b:\" &lt;&lt; endl;\n    cout &lt;&lt; \"\u80cc\u5305\u5bb9\u91cf: \" &lt;&lt; W &lt;&lt; endl;\n    cout &lt;&lt; \"\\n\u5404\u7ec4\u7269\u54c1:\" &lt;&lt; endl;\n    for (int i = 0; i &lt; weight.size(); i++) {\n        cout &lt;&lt; \"\u7b2c\" &lt;&lt; i &lt;&lt; \"\u7ec4: \";\n        for (int j = 0; j &lt; weight[i].size(); j++) {\n            cout &lt;&lt; \"[w=\" &lt;&lt; weight[i][j] &lt;&lt; \",v=\" &lt;&lt; value[i][j] &lt;&lt; \"] \";\n        }\n        cout &lt;&lt; endl;\n    }\n    \n    cout &lt;&lt; \"\\n\u6700\u5927\u4ef7\u503c (\u4e8c\u7ef4DP): \" &lt;&lt; group_knapsack_2d(weight, value, W) &lt;&lt; endl;\n    cout &lt;&lt; \"\u6700\u5927\u4ef7\u503c (\u4e00\u7ef4DP): \" &lt;&lt; group_knapsack_1d(weight, value, W) &lt;&lt; endl;\n    \n    \/\/ \u8f93\u51fa\u9009\u62e9\u65b9\u6848\n    vector&lt;Choice&gt; choices = group_knapsack_with_choice(weight, value, W);\n    cout &lt;&lt; \"\\n\u9009\u62e9\u7684\u7269\u54c1: \";\n    for (auto&amp; c : choices) {\n        cout &lt;&lt; \"(\u7ec4\" &lt;&lt; c.group_idx &lt;&lt; \", \u7269\u54c1\" &lt;&lt; c.item_idx \n             &lt;&lt; \": w=\" &lt;&lt; weight[c.group_idx][c.item_idx] \n             &lt;&lt; \", v=\" &lt;&lt; value[c.group_idx][c.item_idx] &lt;&lt; \") \";\n    }\n    cout &lt;&lt; endl;\n    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u65b9\u6cd5<\/th><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th><th>\u7a7a\u95f4\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td>\u4e8c\u7ef4 DP<\/td><td>O(n \u00d7 W \u00d7 m)<\/td><td>O(n \u00d7 W)<\/td><\/tr><tr><td>\u4e00\u7ef4 DP<\/td><td>O(n \u00d7 W \u00d7 m)<\/td><td>O(W)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5176\u4e2d n \u662f\u7ec4\u6570\uff0cW \u662f\u5bb9\u91cf\uff0cm \u662f\u5e73\u5747\u6bcf\u7ec4\u7269\u54c1\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u7ec4\u80cc\u5305\u7684\u5178\u578b\u5e94\u7528<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u65c5\u884c\u6253\u5305<\/strong>\uff1a\u4e0d\u540c\u7c7b\u522b\u7684\u7269\u54c1\uff0c\u6bcf\u7c7b\u6700\u591a\u5e26\u4e00\u4ef6<\/li><li><strong>\u9884\u7b97\u5206\u914d<\/strong>\uff1a\u4e0d\u540c\u7c7b\u578b\u7684\u6295\u8d44\uff0c\u6bcf\u7c7b\u9009\u62e9\u4e00\u4e2a\u9879\u76ee<\/li><li><strong>\u8bfe\u7a0b\u5b89\u6392<\/strong>\uff1a\u4e0d\u540c\u65f6\u6bb5\u7684\u8bfe\u7a0b\uff0c\u6bcf\u65f6\u6bb5\u9009\u4e00\u95e8<\/li><li><strong>\u6e38\u620f\u88c5\u5907<\/strong>\uff1a\u4e0d\u540c\u90e8\u4f4d\u7684\u88c5\u5907\uff0c\u6bcf\u90e8\u4f4d\u6700\u591a\u7a7f\u4e00\u4ef6<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u5206\u7ec4\u80cc\u5305\u662f 0-1 \u80cc\u5305\u7684\u81ea\u7136\u6269\u5c55\uff0c\u6838\u5fc3\u5728\u4e8e<strong>\u7ec4\u5185\u4e92\u65a5<\/strong>\u7684\u7ea6\u675f\u3002\u89e3\u9898\u65f6\u53ea\u9700\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5bf9\u6bcf\u4e00\u7ec4\u5185\u7684\u7269\u54c1\u8fdb\u884c\u679a\u4e3e\u5373\u53ef\u3002\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u4e0e 0-1 \u80cc\u5305\u76f8\u540c\uff0c\u4f7f\u7528\u4e00\u7ef4\u6570\u7ec4\u65f6\u9700\u8981\u9006\u5e8f\u904d\u5386\u5bb9\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5206\u7ec4\u80cc\u5305\uff1f \u5206\u7ec4\u80cc\u5305\uff08Group Knapsack\uff09 \u662f 0-1 \u80cc\u5305\u7684\u6269\u5c55\u3002\u5728\u5206\u7ec4\u80cc\u5305\u4e2d\uff0c\u7269\u54c1\u88ab\u5206\u6210\u82e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[51,53,52,50],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-3","tag-choice","tag-const-vector","tag-group-knapsack","tag-c"],"_links":{"self":[{"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}