{"id":6036,"date":"2024-04-18T00:07:43","date_gmt":"2024-04-17T16:07:43","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=6036"},"modified":"2024-04-18T00:07:43","modified_gmt":"2024-04-17T16:07:43","slug":"%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/18\/%e5%8a%a8%e6%80%81%e8%a7%84%e5%88%92%e5%9f%ba%e7%a1%80\/","title":{"rendered":"\u52a8\u6001\u89c4\u5212\u57fa\u7840"},"content":{"rendered":"<h1>\u52a8\u6001\u89c4\u5212<\/h1>\n<h2>1\u3001\u57fa\u672c\u5185\u5bb9<\/h2>\n<p>1\u3001\u52a8\u6001\u89c4\u5212\uff0c\u82f1\u6587\uff1aDynamic Programming\uff0c\u7b80\u79f0DP\uff0c\u5982\u679c\u67d0\u4e00\u95ee\u9898\u6709\u5f88\u591a\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u662f\u6700\u6709\u6548\u7684\u3002<\/p>\n<p>2\u3001\u6240\u4ee5\u52a8\u6001\u89c4\u5212\u4e2d\u6bcf\u4e00\u4e2a\u72b6\u6001\u4e00\u5b9a\u662f\u7531\u4e0a\u4e00\u4e2a\u72b6\u6001\u63a8\u5bfc\u51fa\u6765\u7684\uff0c<strong>\u8fd9\u4e00\u70b9\u5c31\u533a\u5206\u4e8e\u8d2a\u5fc3<\/strong>\uff0c\u8d2a\u5fc3\u6ca1\u6709\u72b6\u6001\u63a8\u5bfc\uff0c\u800c\u662f\u4ece\u5c40\u90e8\u76f4\u63a5\u9009\u6700\u4f18\u7684\uff0c<\/p>\n<p>3\u3001<strong>\u5bf9\u4e8e\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u6211\u5c06\u62c6\u89e3\u4e3a\u5982\u4e0b\u4e94\u6b65\u66f2\uff0c\u8fd9\u4e94\u6b65\u90fd\u641e\u6e05\u695a\u4e86\uff0c\u624d\u80fd\u8bf4\u628a\u52a8\u6001\u89c4\u5212\u771f\u7684\u638c\u63e1\u4e86\uff01<\/strong><\/p>\n<ol>\n<li>\u786e\u5b9adp\u6570\u7ec4\uff08dp table\uff09\u4ee5\u53ca\u4e0b\u6807\u7684\u542b\u4e49<\/li>\n<li>\u786e\u5b9a\u9012\u63a8\u516c\u5f0f<\/li>\n<li>dp\u6570\u7ec4\u5982\u4f55\u521d\u59cb\u5316<\/li>\n<li>\u786e\u5b9a\u904d\u5386\u987a\u5e8f<\/li>\n<li>\u4e3e\u4f8b\u63a8\u5bfcdp\u6570\u7ec4<\/li>\n<\/ol>\n<h2>2\u3001\u80cc\u5305\u95ee\u9898<\/h2>\n<p>1\u300101\u80cc\u5305<\/p>\n<p>2\u3001\u5b8c\u5168\u80cc\u5305<\/p>\n<p>3\u3001\u591a\u91cd\u80cc\u5305<\/p>\n<p>4\u3001\u5206\u7ec4\u80cc\u5305<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240331152938767.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240331152938767.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20240331152938767\" \/><\/div><\/p>\n<h3>1\u300101\u80cc\u5305<\/h3>\n<pre><code class=\"language-java\">public class BagProblem {\n    public static void main(String[] args) {\n        int[] weight = {1,3,4};\n        int[] value = {15,20,30};\n        int bagSize = 4;\n        testWeightBagProblem(weight,value,bagSize);\n    }\n\n    \/**\n     * \u52a8\u6001\u89c4\u5212\u83b7\u5f97\u7ed3\u679c\n     * @param weight  \u7269\u54c1\u7684\u91cd\u91cf\n     * @param value   \u7269\u54c1\u7684\u4ef7\u503c\n     * @param bagSize \u80cc\u5305\u7684\u5bb9\u91cf\n     *\/\n    public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){\n\n        \/\/ \u521b\u5efadp\u6570\u7ec4\n        int goods = weight.length;  \/\/ \u83b7\u53d6\u7269\u54c1\u7684\u6570\u91cf\n        int[][] dp = new int[goods][bagSize + 1];\n\n        \/\/ \u521d\u59cb\u5316dp\u6570\u7ec4\n        \/\/ \u521b\u5efa\u6570\u7ec4\u540e\uff0c\u5176\u4e2d\u9ed8\u8ba4\u7684\u503c\u5c31\u662f0\n        for (int j = weight[0]; j &lt;= bagSize; j++) {\n            dp[0][j] = value[0];\n        }\n\n        \/\/ \u586b\u5145dp\u6570\u7ec4\n        for (int i = 1; i &lt; weight.length; i++) {\n            for (int j = 1; j &lt;= bagSize; j++) {\n                if (j &lt; weight[i]) {\n                    \/**\n                     * \u5f53\u524d\u80cc\u5305\u7684\u5bb9\u91cf\u90fd\u6ca1\u6709\u5f53\u524d\u7269\u54c1i\u5927\u7684\u65f6\u5019\uff0c\u662f\u4e0d\u653e\u7269\u54c1i\u7684\n                     * \u90a3\u4e48\u524di-1\u4e2a\u7269\u54c1\u80fd\u653e\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u5c31\u662f\u5f53\u524d\u60c5\u51b5\u7684\u6700\u5927\u4ef7\u503c\n                     *\/\n                    dp[i][j] = dp[i-1][j];\n                } else {\n                    \/**\n                     * \u5f53\u524d\u80cc\u5305\u7684\u5bb9\u91cf\u53ef\u4ee5\u653e\u4e0b\u7269\u54c1i\n                     * \u90a3\u4e48\u6b64\u65f6\u5206\u4e24\u79cd\u60c5\u51b5\uff1a\n                     *    1\u3001\u4e0d\u653e\u7269\u54c1i\n                     *    2\u3001\u653e\u7269\u54c1i\n                     * \u6bd4\u8f83\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c\u54ea\u79cd\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u6700\u5927\n                     *\/\n                    dp[i][j] = Math.max(dp[i-1][j] , dp[i-1][j-weight[i]] + value[i]);\n                }\n            }\n        }\n\n        \/\/ \u6253\u5370dp\u6570\u7ec4\n        for (int i = 0; i &lt; goods; i++) {\n            for (int j = 0; j &lt;= bagSize; j++) {\n                System.out.print(dp[i][j] + &quot;\\t&quot;);\n            }\n            System.out.println(&quot;\\n&quot;);\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u89c4\u5212 1\u3001\u57fa\u672c\u5185\u5bb9 1\u3001\u52a8\u6001\u89c4\u5212\uff0c\u82f1\u6587\uff1aDynamic Programming\uff0c\u7b80\u79f0DP\uff0c\u5982\u679c\u67d0\u4e00\u95ee\u9898\u6709\u5f88 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-6036","post","type-post","status-publish","format-standard","hentry","category-33"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6036"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6036\/revisions"}],"predecessor-version":[{"id":6037,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/6036\/revisions\/6037"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6036"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}