{"id":2979,"date":"2022-03-18T11:27:20","date_gmt":"2022-03-18T03:27:20","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2979"},"modified":"2022-03-18T14:13:12","modified_gmt":"2022-03-18T06:13:12","slug":"floyd","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/18\/floyd\/","title":{"rendered":"Floyd\u7b97\u6cd5\uff1a\u6c42\u591a\u6e90\u89e3\u8d1f\u6743\u56fe\u6700\u77ed\u8def\u5f84\u95ee\u9898"},"content":{"rendered":"\n<p class=\"has-light-pink-background-color has-background\">\u601d\u60f3\uff1a\u52a8\u6001\u89c4\u5212<\/p>\n\n\n\n<p>Floyd-Warshall\u7b97\u6cd5\uff08Floyd-Warshall algorithm\uff09\u662f\u89e3\u51b3\u4efb\u610f\u4e24\u70b9\u95f4\u7684\u6700\u77ed\u8def\u5f84\u7684\u4e00\u79cd\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u6709\u5411\u56fe\u6216\u8d1f\u6743\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u88ab\u7528\u4e8e\u8ba1\u7b97\u6709\u5411\u56fe\u7684\u4f20\u9012\u95ed\u5305\u3002<\/p>\n\n\n\n<p>Floyd-Warshall\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(N^3)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(N^2)\u3002<\/p>\n\n\n\n<h2 id=\"\u7b97\u6cd5\u63cf\u8ff0\">\u7b97\u6cd5\u63cf\u8ff0<\/h2>\n\n\n\n<h3 id=\"1-\u7b97\u6cd5\u601d\u60f3\u539f\u7406\">1) \u7b97\u6cd5\u601d\u60f3\u539f\u7406\uff1a<\/h3>\n\n\n\n<p>Floyd\u7b97\u6cd5\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u3002\u7528\u901a\u4fd7\u7684\u8bed\u8a00\u6765\u63cf\u8ff0\u7684\u8bdd\uff0c\u9996\u5148\u6211\u4eec\u7684\u76ee\u6807\u662f\u5bfb\u627e\u4ece\u70b9i\u5230\u70b9j\u7684\u6700\u77ed\u8def\u5f84\u3002<\/p>\n\n\n\n<p>\u4ece\u52a8\u6001\u89c4\u5212\u7684\u89d2\u5ea6\u770b\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u8fd9\u4e2a\u76ee\u6807\u91cd\u65b0\u505a\u4e00\u4e2a\u8be0\u91ca\uff08\u8fd9\u4e2a\u8be0\u91ca\u6b63\u662f\u52a8\u6001\u89c4\u5212\u6700\u5bcc\u521b\u9020\u529b\u7684\u7cbe\u534e\u6240\u5728\uff09<\/p>\n\n\n\n<p>\u4ece\u4efb\u610f\u8282\u70b9i\u5230\u4efb\u610f\u8282\u70b9j\u7684\u6700\u77ed\u8def\u5f84\u4e0d\u5916\u4e4e2\u79cd\u53ef\u80fd\uff0c1\u662f\u76f4\u63a5\u4ecei\u5230j\uff0c2\u662f\u4ecei\u7ecf\u8fc7\u82e5\u5e72\u4e2a\u8282\u70b9k\u5230j\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5047\u8bbeDis(i,j)\u4e3a\u8282\u70b9u\u5230\u8282\u70b9v\u7684\u6700\u77ed\u8def\u5f84\u7684\u8ddd\u79bb\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u8282\u70b9k\uff0c\u6211\u4eec\u68c0\u67e5Dis(i,k) + Dis(k,j) &lt; Dis(i,j)\u662f\u5426\u6210\u7acb\uff0c\u5982\u679c\u6210\u7acb\uff0c\u8bc1\u660e\u4ecei\u5230k\u518d\u5230j\u7684\u8def\u5f84\u6bd4i\u76f4\u63a5\u5230j\u7684\u8def\u5f84\u77ed\uff0c\u6211\u4eec\u4fbf\u8bbe\u7f6eDis(i,j) = Dis(i,k) + Dis(k,j)\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u6211\u4eec\u904d\u5386\u5b8c\u6240\u6709\u8282\u70b9k\uff0cDis(i,j)\u4e2d\u8bb0\u5f55\u7684\u4fbf\u662fi\u5230j\u7684\u6700\u77ed\u8def\u5f84\u7684\u8ddd\u79bb\u3002<\/p>\n\n\n\n<h3 id=\"2-\u7b97\u6cd5\u63cf\u8ff0\">2) \u7b97\u6cd5\u63cf\u8ff0\uff1a<\/h3>\n\n\n\n<p>a. \u4ece\u4efb\u610f\u4e00\u6761\u5355\u8fb9\u8def\u5f84\u5f00\u59cb\u3002\u6240\u6709\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u662f\u8fb9\u7684\u6743\uff0c\u5982\u679c\u4e24\u70b9\u4e4b\u95f4\u6ca1\u6709\u8fb9\u76f8\u8fde\uff0c\u5219\u6743\u4e3a\u65e0\u7a77\u5927\u3002 \u3000\u3000<\/p>\n\n\n\n<p>b. \u5bf9\u4e8e\u6bcf\u4e00\u5bf9\u9876\u70b9 u \u548c v\uff0c\u770b\u770b\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u9876\u70b9 w \u4f7f\u5f97\u4ece u \u5230 w \u518d\u5230 v \u6bd4\u5df1\u77e5\u7684\u8def\u5f84\u66f4\u77ed\u3002\u5982\u679c\u662f\u66f4\u65b0\u5b83\u3002<\/p>\n\n\n\n<h2 id=\"floyd\u7b97\u6cd5\u8fc7\u7a0b\u77e9\u9635\u7684\u8ba1\u7b97-\u5341\u5b57\u4ea4\u53c9\u6cd5\">Floyd\u7b97\u6cd5\u8fc7\u7a0b\u77e9\u9635\u7684\u8ba1\u7b97\u2014-\u5341\u5b57\u4ea4\u53c9\u6cd5<\/h2>\n\n\n\n<p>\u65b9\u6cd5\uff1a\u4e24\u6761\u7ebf\uff0c\u4ece\u5de6\u4e0a\u89d2\u5f00\u59cb\u8ba1\u7b97\u4e00\u76f4\u5230\u53f3\u4e0b\u89d2 \u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<p>\u5148\u6765\u7b80\u5355\u5206\u6790\u4e0b,\u7531\u4e8e\u77e9\u9635\u4e2d\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u59cb\u7ec8\u4e3a0,\u56e0\u6b64\u4ee5k\u4e3a\u4e2d\u95f4\u70b9\u65f6,\u4ece\u4e0a\u4e00\u4e2a\u77e9\u9635\u5230\u4e0b\u4e00\u4e2a\u77e9\u9635\u53d8\u5316\u65f6,\u77e9\u9635\u7684\u7b2ck\u884c,\u7b2ck\u5217\u548c\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u662f\u4e0d\u53d1\u751f\u6539\u53d8\u7684<strong><u>\uff08\u5bf9\u89d2\u7ebf\u4e0a\u90fd\u662f0\uff0c\u56e0\u4e3a\u4e00\u4e2a\u9876\u70b9\u5230\u81ea\u5df1\u7684\u8ddd\u79bb\u5c31\u662f0\uff0c\u4e00\u76f4\u4e0d\u53d8\uff1b\u800c\u5f53<strong>k\u4e3a\u4e2d\u95f4\u70b9\u65f6\uff0ck\u5230\u5176\u4ed6\u9876\u70b9\uff08\u7b2ck\u884c\uff09\u548c\u5176\u4ed6\u9876\u70b9\u5230k\uff08\u7b2ck\u5217\uff09\u7684\u8ddd\u79bb\u662f\u4e0d\u53d8\u7684<\/strong>\uff09<\/u><\/strong>\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u56e0\u6b64\u6bcf\u4e00\u6b65\u4e2d\u6211\u4eec\u53ea\u9700\u8981\u5224\u65ad4*4-3*4+2=6\u4e2a\u5143\u7d20\u662f\u5426\u53d1\u751f\u6539\u53d8\u5373\u53ef,\u4e5f\u5c31\u662f\u8981\u5224\u65ad\u65e2\u4e0d\u5728\u7b2ck\u884c\u7b2ck\u5217\u53c8\u4e0d\u5728\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u3002\u5177\u4f53\u8ba1\u7b97\u6b65\u9aa4\u5982\u4e0b\uff1a\u4ee5k\u4e3a\u4e2d\u95f4\u70b9\uff081\uff09\u201c\u4e09\u6761\u7ebf\u201d\uff1a\u5212\u53bb\u7b2ck\u884c\uff0c\u7b2ck\u5217\uff0c\u5bf9\u89d2\u7ebf\uff082\uff09\u201c\u5341\u5b57\u4ea4\u53c9\u6cd5\u201d\uff1a\u5bf9\u4e8e\u4efb\u4e00\u4e2a\u4e0d\u5728\u4e09\u6761\u7ebf\u4e0a\u7684\u5143\u7d20x\uff0c\u5747<strong><u>\u53ef\u4e0e\u53e6\u5916\u5728<\/u><\/strong><strong><u>k\u884c<\/u><u>k\u5217\u4e0a\u7684<\/u><u>3\u4e2a\u5143\u7d20\u6784\u6210\u4e00\u4e2a<\/u><u>2\u9636\u77e9\u9635<\/u><\/strong>\uff0c<strong>x\u662f\u5426\u53d1\u751f\u6539\u53d8\u4e0e2\u9636\u77e9\u9635\u4e2d\u4e0d\u5305\u542bx\u7684\u90a3\u6761\u5bf9\u89d2\u7ebf\u4e0a2\u4e2a\u5143\u7d20\u7684\u548c\u6709\u5173\uff0c\u82e5\u4e8c\u8005\u4e4b\u548c\u5c0f\u4e8ex\uff0c\u5219\u7528\u5b83\u4eec\u7684\u548c\u66ff\u6362x\uff0c\u5bf9\u5e94\u7684Path\u77e9\u9635\u4e2d\u7684\u4e0ex\u76f8\u5bf9\u5e94\u7684\u4f4d\u7f6e\u7528k\u6765\u66ff\u4ee3\u3002<\/strong>\u3002<\/p>\n\n\n\n<p>\u7ed9\u51fa\u77e9\u9635\uff0c\u5176\u4e2d\u77e9\u9635A\u662f\u90bb\u63a5\u77e9\u9635\uff0c\u800c\u77e9\u9635Path\u8bb0\u5f55u,v\u4e24\u70b9\u4e4b\u95f4\u6700\u77ed\u8def\u5f84\u6240\u5fc5\u987b\u7ecf\u8fc7\u7684\u70b9\uff08\u6307\u7684\u662fu->&#8230;.->\u8be5\u70b9->v\uff09<\/p>\n\n\n\n<p>\u5206\u522b\u4ee5\u6bcf\u4e00\u4e2a\u70b9\u4f5c\u4e3aK\u70b9\u8f6c\u63a5v<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/user-images.githubusercontent.com\/18375710\/73129326-d4667300-401b-11ea-969f-12b3fea9178c.png\" alt=\"image\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/user-images.githubusercontent.com\/18375710\/73129335-1394c400-401c-11ea-8cce-46487518504a.png\" alt=\"image\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/user-images.githubusercontent.com\/18375710\/73129343-4b037080-401c-11ea-8e9f-54c06447354d.png\" alt=\"image\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/user-images.githubusercontent.com\/18375710\/73129347-6c645c80-401c-11ea-8909-cd11765d45e3.png\" alt=\"image\"\/><\/figure>\n\n\n\n<h2 id=\"\u4ee3\u7801\u5b9e\u73b0\">\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<h3 id=\"java-\u7248\u672c\">java \u7248\u672c<\/h3>\n\n\n\n<p>\u6838\u5fc3\u4ee3\u7801\u53ea\u6709 4 \u884c\uff0c\u5b9e\u5728\u4ee4\u4eba\u8d5e\u53f9\u3002&nbsp;&nbsp;[java]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>private<\/strong> <strong>void<\/strong> <strong>floyd<\/strong><strong>()<\/strong> <strong>{<\/strong>\n    <strong>for<\/strong> <strong>(<\/strong><strong>int<\/strong> k <strong>=<\/strong> 0<strong>;<\/strong> k <strong>&lt;<\/strong> n<strong>;<\/strong> k<strong>++)<\/strong> <strong>{<\/strong>\n        <strong>for<\/strong> <strong>(<\/strong><strong>int<\/strong> i <strong>=<\/strong> 0<strong>;<\/strong> i <strong>&lt;<\/strong> n<strong>;<\/strong> i<strong>++)<\/strong> <strong>{<\/strong>\n            <strong>for<\/strong> <strong>(<\/strong><strong>int<\/strong> j <strong>=<\/strong> 0<strong>;<\/strong> j <strong>&lt;<\/strong> n<strong>;<\/strong> j<strong>++)<\/strong> <strong>{<\/strong>\n                a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>]<\/strong> <strong>=<\/strong> <strong>Math<\/strong><strong>.<\/strong>min<strong>(<\/strong>a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>],<\/strong> a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>k<strong>]<\/strong> <strong>+<\/strong> a<strong>&#91;<\/strong>k<strong>]&#91;<\/strong>j<strong>]);<\/strong>\n            <strong>}<\/strong>\n        <strong>}<\/strong>\n    <strong>}<\/strong>\n\n    <em>\/\/ \u6253\u5370<\/em>\n    <strong>for<\/strong> <strong>(<\/strong><strong>int<\/strong> i <strong>=<\/strong> 0<strong>;<\/strong> i <strong>&lt;<\/strong> n<strong>;<\/strong> i<strong>++)<\/strong> <strong>{<\/strong>\n        <strong>for<\/strong> <strong>(<\/strong><strong>int<\/strong> j <strong>=<\/strong> i <strong>+<\/strong> 1<strong>;<\/strong> j <strong>&lt;<\/strong> n<strong>;<\/strong> j<strong>++)<\/strong> <strong>{<\/strong>\n            <strong>System<\/strong><strong>.<\/strong>out<strong>.<\/strong>println<strong>(<\/strong>i <strong>+<\/strong> \" \" <strong>+<\/strong> j <strong>+<\/strong> \":\" <strong>+<\/strong> a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>]);<\/strong>\n        <strong>}<\/strong>\n    <strong>}<\/strong>\n<strong>}<\/strong>\n<\/code><\/pre>\n\n\n\n<h3 id=\"\u6b63\u786e\u6027\u5206\u6790\">\u6b63\u786e\u6027\u5206\u6790<\/h3>\n\n\n\n<p>\u6838\u5fc3\u4ee3\u7801\u53ea\u6709\u56db\u884c\uff0c\u4e09\u884c\u5faa\u73af\uff0c\u4e00\u884c\u66f4\u65b0\uff0c\u7684\u786e\u5341\u5206\u7b80\u6d01\u4f18\u96c5\uff0c\u53ef\u662f\u8fd9\u56db\u884c\u4ee3\u7801\u4e3a\u4ec0\u4e48\u5c31\u80fd\u6c42\u51fa\u4efb\u610f\u4e24\u70b9\u7684\u6700\u77ed\u8def\u5f84\u5462\uff1f<\/p>\n\n\n\n<p>\u770b\u4ee3\u7801\u7684\u7279\u70b9\uff0c\u5f88\u663e\u7136\u7279\u522b\u50cf\u662f\u4e00\u79cd\u52a8\u6001\u89c4\u5212\uff0c\u786e\u5b9e\uff0c\u4e4b\u524d\u4e5f\u8bf4\u8fc7\u8be5\u7b97\u6cd5\u662f\u57fa\u4e8e\u52a8\u6001\u89c4\u5212\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4ece\u52a8\u6001\u89c4\u5212\u7684\u89d2\u5ea6\u8003\u8651\uff0c\u89e3\u52a8\u6001\u89c4\u5212\u9898\u76ee\u7684\u91cd\u70b9\u5c31\u662f\u5408\u7406\u7684\u5b9a\u4e49\u72b6\u6001\uff0c\u5212\u5206\u9636\u6bb5\uff0c\u6211\u4eec\u5b9a\u4e49 f[k][i][j] \u4e3a\u7ecf\u8fc7\u524dk\u7684\u8282\u70b9\uff0c\u4ecei\u5230j\u6240\u80fd\u5f97\u5230\u7684\u6700\u77ed\u8def\u5f84\uff0cf[k][i][j]\u53ef\u4ee5\u4ecef[k-1][i][j]\u8f6c\u79fb\u8fc7\u6765\uff0c\u5373\u4e0d\u7ecf\u8fc7\u7b2ck\u4e2a\u8282\u70b9\uff0c\u4e5f\u53ef\u4ee5\u4ecef[k-1][i][k]+f[k-1][k][j]\u8f6c\u79fb\u8fc7\u6765\uff0c\u5373\u7ecf\u8fc7\u7b2ck\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u89c2\u5bdf\u4e00\u4e0b\u8fd9\u4e2a\u72b6\u6001\u7684\u5b9a\u4e49\uff0c\u6ee1\u8db3\u4e0d\u6ee1\u8db3<strong>\u6700\u4f18\u5b50\u7ed3\u6784\u548c\u65e0\u540e\u6548\u6027\u539f\u5219\u3002<\/strong><\/p>\n\n\n\n<ul><li>\u6700\u4f18\u5b50\u7ed3\u6784<\/li><\/ul>\n\n\n\n<p>\u56fe\u7ed3\u6784\u4e2d\u4e00\u4e2a\u663e\u800c\u6613\u89c1\u7684\u5b9a\u7406\uff1a<\/p>\n\n\n\n<p>\u6700\u77ed\u8def\u5f84\u7684\u5b50\u8def\u5f84\u4ecd\u7136\u662f\u6700\u77ed\u8def\u5f84, \u8fd9\u4e2a\u5b9a\u7406\u663e\u800c\u6613\u89c1\uff0c\u6bd4\u5982\u4e00\u6761\u4ecea\u5230e\u7684\u6700\u77ed\u8defa-&gt;b-&gt;c-&gt;d-&gt;e \u90a3\u4e48 a-&gt;b-&gt;c \u4e00\u5b9a\u662fa\u5230c\u7684\u6700\u77ed\u8defc-&gt;d-&gt;e\u4e00\u5b9a\u662fc\u5230e\u7684\u6700\u77ed\u8def\uff0c\u53cd\u8fc7\u6765\uff0c\u5982\u679c\u8bf4\u4e00\u6761\u6700\u77ed\u8def\u5fc5\u987b\u8981\u7ecf\u8fc7\u70b9k\uff0c\u90a3\u4e48i-&gt;k\u7684\u6700\u77ed\u8def\u52a0\u4e0ak-&gt;j\u7684\u6700\u77ed\u8def\u4e00\u5b9a\u662fi-&gt;j \u7ecf\u8fc7k\u7684\u6700\u77ed\u8def\uff0c\u56e0\u6b64\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u53ef\u4ee5\u4fdd\u8bc1\u3002<\/p>\n\n\n\n<ul><li>\u65e0\u540e\u6548\u6027<\/li><\/ul>\n\n\n\n<p>\u72b6\u6001f[k][i][j]\u7531f[k-1][i][j]\uff0cf[k-1][i,k] \u4ee5\u53caf[k-1][k][j]\u8f6c\u79fb\u8fc7\u6765\uff0c\u5f88\u5bb9\u6613\u53ef\u4ee5\u770b\u51fa\uff0cf[k] \u7684\u72b6\u6001\u5b8c\u5168\u7531f[k-1]\u8f6c\u79fb\u8fc7\u6765\uff0c\u53ea\u8981\u6211\u4eec\u628ak\u653e\u5012\u6700\u5916\u5c42\u5faa\u73af\u4e2d\uff0c\u5c31\u80fd\u4fdd\u8bc1\u65e0\u540e\u6548\u6027\u3002<\/p>\n\n\n\n<p>\u6ee1\u8db3\u4ee5\u4e0a\u4e24\u4e2a\u8981\u6c42\uff0c\u6211\u4eec\u5373\u8bc1\u660e\u4e86Floyd\u7b97\u6cd5\u662f\u6b63\u786e\u7684\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6700\u540e\u5f97\u51fa\u4e00\u4e2a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a<code>f[k][i][j] = min(f[k-1][i][k],f[k-1][i][k],f[k-1][k][j])<\/code>&nbsp;\uff0c\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u8fd9\u4e2a\u4e09\u7ef4\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53ef\u4ee5\u4f7f\u7528\u6eda\u52a8\u6570\u7ec4\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n\n\n\n<h3 id=\"k-\u4e3a\u4ec0\u4e48\u8981\u653e\u5728\u6700\u5916\u5c42\">K \u4e3a\u4ec0\u4e48\u8981\u653e\u5728\u6700\u5916\u5c42<\/h3>\n\n\n\n<p>\u91c7\u7528\u52a8\u6001\u89c4\u5212\u601d\u60f3\uff0c<code>f[k][i][j]<\/code>&nbsp;\u8868\u793a i \u548c j \u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7\u7f16\u53f7\u4e3a 1\u2026k \u7684\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\u3002<\/p>\n\n\n\n<p><code>f[0][i][j]<\/code>&nbsp;\u521d\u503c\u4e3a\u539f\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u3002<\/p>\n\n\n\n<p><code>f[k][i][j]<\/code>\u5219\u53ef\u4ee5\u4ece<code>f[k-1][i][j]<\/code>\u8f6c\u79fb\u6765\uff0c\u8868\u793a i \u5230 j \u4e0d\u7ecf\u8fc7 k \u8fd9\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5&nbsp;<code>f[k-1][i][k]+f[k-1][k][j]<\/code>&nbsp;\u4ece\u8f6c\u79fb\u8fc7\u6765\uff0c\u8868\u793a\u7ecf\u8fc7 k \u8fd9\u4e2a\u70b9\u3002<\/p>\n\n\n\n<p>\u610f\u601d\u5373&nbsp;<code>f[k][i][j]=min(f[k-1][i][j], f[k-1][i][k]+f[k-1][k][j])<\/code><\/p>\n\n\n\n<p>\u7136\u540e\u4f60\u5c31\u4f1a\u53d1\u73b0 f \u6700\u5916\u5c42\u4e00\u7ef4\u7a7a\u95f4\u53ef\u4ee5\u7701\u7565\uff0c\u56e0\u4e3a f[k] \u53ea\u4e0e f[k-1] \u6709\u5173\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u8fd9\u4e2a\u7b97\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u4e5f\u9700\u8981\u627e\u70b9\u65f6\u95f4\u7406\u89e3\u8fd9\u4e2a\u7b97\u6cd5\uff0c\u5c31\u4e0d\u4f1a\u518d\u6709\u8fd9\u79cd\u95ee\u9898\u5566\u3002<\/p>\n\n\n\n<p><strong>Floyd\u7b97\u6cd5\u7684\u672c\u8d28\u662fDP\uff0c\u800ck\u662fDP\u7684\u9636\u6bb5\uff0c\u56e0\u6b64\u8981\u5199\u6700\u5916\u9762\u3002<\/strong><\/p>\n\n\n\n<p>\u60f3\u8c61\u4e00\u4e2a\u56fe\uff0c<strong>\u8ba8\u8bba\u7684\u662f\u8981\u4ece1\u70b9\u5230\u8fbe3\u70b9\uff0c\u662f\u76f4\u63a5\u8d70\u8fd8\u662f\u7ecf\u8fc7\u4e2d\u95f4\u70b92\uff0c\u4ece\u800c\u786e\u5b9a\u4e24\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84<\/strong>\u3002<\/p>\n\n\n\n<h1 id=\"\u6eda\u52a8\u6570\u7ec4\u4f18\u5316\">\u6eda\u52a8\u6570\u7ec4\u4f18\u5316<\/h1>\n\n\n\n<p>\u6eda\u52a8\u6570\u7ec4\u662f\u4e00\u79cd\u52a8\u6001\u89c4\u5212\u4e2d\u5e38\u89c1\u7684\u964d\u7ef4\u4f18\u5316\u7684\u65b9\u5f0f\uff0c\u5e94\u7528\u5e7f\u6cdb\uff08\u80cc\u5305dp\u7b49\uff09\uff0c\u53ef\u4ee5\u6781\u5927\u7684\u51cf\u5c11\u7a7a\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u6c42\u51fa\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\uff0c\u6211\u4eec\u5728\u66f4\u65b0f[k]\u5c42\u72b6\u6001\u7684\u65f6\u5019\uff0c\u7528\u5230f[k-1]\u5c42\u7684\u503c\uff0cf[k-2] f[k-3]\u5c42\u7684\u503c\u5c31\u76f4\u63a5\u5e9f\u5f03\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u5927\u53ef\u8ba9\u7b2c\u4e00\u5c42\u7684\u5927\u5c0f\u4ecen\u53d8\u62102,\u518d\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u5728f[k]\u5c42\u66f4\u65b0f[k][i][j]\u7684\u65f6\u5019\uff0cf[k-1][i][k] \u548c f[k-1][k][j] \u6211\u4eec\u5982\u679c\u80fd\u4fdd\u8bc1\uff0c\u5728\u66f4\u65b0k\u5c42\u53e6\u5916\u4e00\u7ec4\u8def\u5f84m-&gt;n\u7684\u65f6\u5019\uff0c\u4e0d\u53d7\u524d\u9762\u66f4\u65b0\u8fc7\u7684f[k][i][j]\u7684\u5f71\u54cd\uff0c\u5c31\u53ef\u4ee5\u628a\u7b2c\u4e00\u7ef4\u5ea6\u53bb\u6389\u4e86\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u53bb\u6389\u7b2c\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u5199\u6210\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u7684\u90a3\u6837\uff0c\u5c31\u662ff[i][j] \u4f9d\u8d56 f[i][k] + f[k][j] \u6211\u4eec\u5728\u66f4\u65b0f[m][n]\u7684\u65f6\u5019\uff0c\u7528\u5230\u4e86f[m][k] + f[k][n] \u5047\u8bbef[i][j]\u7684\u66f4\u65b0\u5f71\u54cd\u5230\u4e86f[m][k] \u6216\u8005 f[k][m] \u5373 {m=i,k=j} \u6216\u8005 {k=i,n=j} \u8fd9\u65f6\u5019\u6709\u4e24\u79cd\u60c5\u51b5\uff0cj\u548ck\u662f\u540c\u4e00\u4e2a\u70b9\uff0c\u6216\u8005i\u548ck\u662f\u540c\u4e00\u4e2a\u70b9\uff0c\u90a3\u4e48 f[i][j] = f[i][j] + f[j][j]\uff0c\u6216\u8005f[i][j] = f[i][i]+f[i][j] \u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u6240\u8c13\u7684\u201c\u524d\u9762\u66f4\u65b0\u7684\u70b9\u5bf9\u201d\u8fd8\u662f\u8fd9\u4e24\u4e2a\u70b9\u672c\u6765\u7684\u8def\u5f84\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u552f\u4e00\u4e24\u79cd\u5728\u67d0\u4e00\u5c42\u5148\u66f4\u65b0\u7684\u70b9\uff0c\u5f71\u54cd\u5230\u540e\u66f4\u65b0\u7684\u70b9\u7684\u60c5\u51b5\uff0c\u662f\u5b8c\u5168\u5408\u7406\u7684\uff0c\u6240\u4ee5\u8bf4\uff0c\u6211\u4eec\u5373\u65f6\u628a\u7b2c\u4e00\u7ef4\u53bb\u6389\uff0c\u4e5f\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u539f\u5219\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u53ef\u4ee5\u7528\u6eda\u52a8\u6570\u7ec4\u4f18\u5316\u3002<\/p>\n\n\n\n<p>\u4f18\u5316\u4e4b\u540e\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5373\u4e3a\uff1a<code>f[i][j] = min(f[i][j],f[i][k]+f[k][j])<\/code>\u3002<\/p>\n\n\n\n<h1 id=\"\u6c42\u5177\u4f53\u8def\u5f84\">\u6c42\u5177\u4f53\u8def\u5f84\uff1a<\/h1>\n\n\n\n<p>\u6211\u4eec\u4e0a\u9762\u4ec5\u4ec5\u662f\u77e5\u9053\u4e86\u6700\u77ed\u8def\u5f84\u7684\u957f\u5ea6\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u6211\u4eec\u5e38\u5e38\u662f\u9700\u8981\u77e5\u9053\u5177\u4f53\u7684\u8def\u5f84\uff0c\u5728Floyd\u7b97\u6cd5\u4e2d\u600e\u4e48\u6c42\u5177\u4f53\u8def\u5f84\u5462\uff0c\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f55\u4e0b\u6765\u5728\u66f4\u65b0f[i][j]\u7684\u65f6\u5019\uff0c\u7528\u5230\u7684\u4e2d\u95f4\u8282\u70b9\u662f\u54ea\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u7528path[i][j]\u8bb0\u5f55\u4ecei\u5230j\u677e\u5f1b\u7684\u8282\u70b9k\uff0c\u90a3\u4e48\u4ecei\u5230j,\u80af\u5b9a\u662f\u5148\u4ecei\u5230k\uff0c\u7136\u540e\u518d\u4ecek\u5230j\uff0c \u90a3\u4e48\u6211\u4eec\u5728\u627e\u51fapath[i][k] , path[k][j]\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u5373 i\u5230k\u7684\u6700\u77ed\u8def\u662f&nbsp;<code>i -&gt; path[i][k] -&gt; k -&gt; path[k][j] -&gt; k<\/code>&nbsp;q` \u7136\u540e\u6c42path[i][k]\u548cpath[k][j] \uff0c\u4e00\u76f4\u5230\u67d0\u4e24\u4e2a\u8282\u70b9\u6ca1\u6709\u4e2d\u95f4\u8282\u70b9\u4e3a\u6b62\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u5728\u66f4\u65b0\u8def\u5f84\u7684\u65f6\u5019\uff1a&nbsp;&nbsp;[java]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>if<\/strong><strong>(<\/strong>a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>k<strong>]&gt;<\/strong>temp<strong>){<\/strong>\n    a<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>]<\/strong> <strong>=<\/strong> temp<strong>;<\/strong>\n    path<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>]<\/strong> <strong>=<\/strong> k<strong>;<\/strong>\n<strong>}<\/strong>\n<\/code><\/pre>\n\n\n\n<p>\u6c42\u8def\u5f84\u7684\u65f6\u5019\uff1a&nbsp;&nbsp;[java]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>String<\/strong> <strong>getPath<\/strong><strong>(<\/strong><strong>int<\/strong><strong>&#91;]&#91;]<\/strong> path<strong>,<\/strong> <strong>int<\/strong> i<strong>,<\/strong> <strong>int<\/strong> j<strong>)<\/strong> <strong>{<\/strong>\n    <strong>if<\/strong> <strong>(<\/strong>path<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>]<\/strong> <strong>==<\/strong> <strong>-<\/strong>1<strong>)<\/strong> <strong>{<\/strong>\n        <strong>return<\/strong> \" \"<strong>+<\/strong>i<strong>+<\/strong>\" \"<strong>+<\/strong>j<strong>;<\/strong>\n    <strong>}<\/strong> <strong>else<\/strong> <strong>{<\/strong>\n        <strong>int<\/strong> k <strong>=<\/strong> path<strong>&#91;<\/strong>i<strong>]&#91;<\/strong>j<strong>];<\/strong>\n        <strong>return<\/strong> <strong>getPath<\/strong><strong>(<\/strong>path<strong>,<\/strong> i<strong>,<\/strong> k<strong>)+<\/strong>\" \"<strong>+<\/strong>getPath<strong>(<\/strong>path<strong>,<\/strong> k<strong>,<\/strong> j<strong>)+<\/strong>\" \"<strong>;<\/strong>\n    <strong>}<\/strong>\n<strong>}<\/strong>\n<\/code><\/pre>\n\n\n\n<h1 id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h1>\n\n\n\n<p>Floyd\u7b97\u6cd5\u53ea\u80fd\u5728\u4e0d\u5b58\u5728\u8d1f\u6743\u73af\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff0c\u56e0\u4e3a\u5176\u5e76\u4e0d\u80fd\u5224\u65ad\u8d1f\u6743\u73af\uff0c\u4e0a\u9762\u4e5f\u8bf4\u8fc7\uff0c\u5982\u679c\u6709\u8d1f\u6743\u73af\uff0c\u90a3\u4e48\u6700\u77ed\u8def\u5c06\u65e0\u610f\u4e49\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u4e0d\u65ad\u8d70\u8d1f\u6743\u73af\uff0c\u8fd9\u6837\u6700\u77ed\u8def\u5f84\u503c\u4fbf\u6210\u4e3a\u4e86\u8d1f\u65e0\u7a77\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5176\u53ef\u4ee5\u5904\u7406\u5e26\u8d1f\u6743\u8fb9\u4f46\u662f\u65e0\u8d1f\u6743\u73af\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u6c42\u591a\u6e90\u6700\u77ed\u8def\u7684Floyd\u7b97\u6cd5\uff0c\u57fa\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u5341\u5206\u4f18\u96c5\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5176\u590d\u6742\u5ea6\u786e\u5b9e\u662f\u4e0d\u6562\u606d\u7ef4\uff0c\u56e0\u4e3a\u8981\u7ef4\u62a4\u4e00\u4e2a\u8def\u5f84\u77e9\u9635\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u8fbe\u5230\u4e86O(n^2)\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8fbe\u5230\u4e86O(n^3)\uff0c\u53ea\u6709\u5728\u6570\u636e\u89c4\u6a21\u5f88\u5c0f\u7684\u65f6\u5019\uff0c\u624d\u9002\u5408\u4f7f\u7528\u8fd9\u4e2a\u7b97\u6cd5\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u7684\u5e94\u7528\u4e2d\uff0c\u6c42\u5355\u6e90\u6700\u77ed\u8def\u662f\u6700\u5e38\u89c1\u7684\uff0c\u6bd4\u5982\u5728\u8def\u7531\u7b97\u6cd5\u4e2d\uff0c\u67d0\u4e2a\u8282\u70b9\u4ec5\u9700\u8981\u77e5\u9053\u4ece\u8fd9\u4e2a\u8282\u70b9\u51fa\u53d1\u5230\u8fbe\u5176\u4ed6\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\uff0c\u800c\u4e0d\u9700\u8981\u77e5\u9053\u5176\u4ed6\u70b9\u7684\u60c5\u51b5\uff0c\u56e0\u6b64\u5728\u8def\u7531\u7b97\u6cd5\u4e2d\u6700\u9002\u5408\u7684\u5e94\u8be5\u662f\u5355\u6e90\u6700\u77ed\u8def\u7b97\u6cd5\uff0cDijkstra\u7b97\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u60f3\uff1a\u52a8\u6001\u89c4\u5212 Floyd-Warshall\u7b97\u6cd5\uff08Floyd-Warshall algorithm\uff09\u662f\u89e3\u51b3\u4efb\u610f &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/18\/floyd\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Floyd\u7b97\u6cd5\uff1a\u6c42\u591a\u6e90\u89e3\u8d1f\u6743\u56fe\u6700\u77ed\u8def\u5f84\u95ee\u9898<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2979"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2979"}],"version-history":[{"count":8,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2979\/revisions"}],"predecessor-version":[{"id":2990,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2979\/revisions\/2990"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2979"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}