{"id":176179,"date":"2022-04-15T00:43:06","date_gmt":"2022-04-14T16:43:06","guid":{"rendered":"http:\/\/www.idc.net\/help\/176179\/"},"modified":"2022-04-15T00:43:06","modified_gmt":"2022-04-14T16:43:06","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython-dijkstra%e7%ae%97%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/176179\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1aPython Dijkstra\u7b97\u6cd5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/661\/422\/691\/1628479253290248.png\" class=\"aligncenter\"><\/p>\n<p><strong>\u8bf4\u660e<\/strong><\/p>\n<p style=\"line-height: 2em\">1\u3001Dijkstra\u7b97\u6cd5\u662f\u7ecf\u5178\u7684\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u5b83\u662f\u6570\u636e\u7ed3\u6784\u3001\u56fe\u8bba\u3001\u8fd0\u7b79\u5b66\u7b49\u57fa\u7840\u6559\u5b66\u7b97\u6cd5\u3002<\/p>\n<p style=\"line-height: 2em\">\u4ee4\u4eba\u611f\u5174\u8da3\u7684\u662f\uff0cDijkstra\u7b97\u6cd5\u901a\u5e38\u662f\u6309\u7167\u8d2a\u5fc3\u65b9\u6cd5\u6765\u63cf\u8ff0\u7684\uff0c\u800c\u5728\u8fd0\u7b79\u5b66\u4e2d\u628aDijkstra\u7b97\u6cd5\u89c6\u4e3a\u52a8\u6001\u89c4\u5212\u3002<\/p>\n<p style=\"line-height: 2em\">2\u3001Dijkstra\u7b97\u6cd5\u4ece\u8d77\u59cb\u70b9\u5f00\u59cb\uff0c\u91c7\u7528\u8d2a\u5fc3\u6cd5\u3002<\/p>\n<p style=\"line-height: 2em\">\u6bcf\u4e00\u904d\u904d\u5386\u4e00\u4e2a\u8ddd\u79bb\u8d77\u70b9\u6700\u8fd1\u4e14\u6ca1\u6709\u5230\u8fbe\u7684\u90bb\u63a5\u9876\u70b9\uff0c\u5c42\u5c42\u5c55\u5f00\uff0c\u76f4\u81f3\u7ed3\u675f\u3002<\/p>\n<p style=\"line-height: 2em\">Dijkstra\u7b97\u6cd5\u6c42\u89e3\u52a0\u6743\u6700\u77ed\u8def\u5f84\u7684\u6700\u4f18\u89e3\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO^2\u3002\u5f53\u8fb9\u6570\u8fdc\u5c0f\u4e8en^2\u65f6\uff0c\u590d\u6742\u5ea6\u53ef\u4ee5\u964d\u4f4e\uff0c\u5e76\u4ee5\u5806\u7ed3\u6784\u7684\u5f62\u5f0f\u5c06\u5176\u964d\u4f4e\u4e3aO`(m+n)log(n))\u3002<\/p>\n<p style=\"line-height: 2em\">Dijkstar\u7b97\u6cd5\u65e0\u6cd5\u5904\u7406\u8d1f\u6743\u8fb9\uff0c\u8fd9\u662f\u7531\u8d2a\u5fc3\u6cd5\u7684\u9009\u62e9\u89c4\u5219\u6240\u51b3\u5b9a\u7684\u3002<\/p>\n<p style=\"line-height: 2em\"><strong>\u5b9e\u4f8b<\/strong><\/p>\n<pre>def&nbsp;dijstra(adj,&nbsp;src,&nbsp;dst,&nbsp;n):\n&nbsp;&nbsp;&nbsp;&nbsp;dist&nbsp;=&nbsp;[Inf]&nbsp;*&nbsp;n\n&nbsp;&nbsp;&nbsp;&nbsp;dist[src]&nbsp;=&nbsp;0\n&nbsp;&nbsp;&nbsp;&nbsp;book&nbsp;=&nbsp;[0]&nbsp;*&nbsp;n&nbsp;#&nbsp;\u8bb0\u5f55\u5df2\u7ecf\u786e\u5b9a\u7684\u9876\u70b9\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u6bcf\u6b21\u627e\u5230\u8d77\u70b9\u5230\u8be5\u70b9\u7684\u6700\u77ed\u9014\u5f84\n&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bsp;and&nbsp;dist[u]&nbsp;+&nbsp;w&nbsp;&lt;&nbsp;dist[v]:&nbsp;#&nbsp;\u5224\u65ad\u7ed3\u70b9\u662f\u5426\u5df2\u7ecf\u786e\u5b9a\u4e86\uff0c\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[v]&nbsp;=&nbsp;dist[u]&nbsp;+&nbsp;w\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;dist[v]&nbsp;&lt;&nbsp;minVal:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next_u,&nbsp;minVal&nbsp;=&nbsp;v,&nbsp;dist[v]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5f00\u59cb\u4e0b\u4e00\u8f6e\u904d\u5386\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u&nbsp;=&nbsp;next_u\n&nbsp;&nbsp;&nbsp;&nbsp;print(dist)\nreturn&nbsp;dist[dst]<\/pre>\n<p style=\"line-height: 2em\">\u4ee5\u4e0a\u5c31\u662fPython Dijkstra\u7b97\u6cd5\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<span style=\"font-size: 16px\">\u66f4\u591aPython\u5b66\u4e60\u6307\u8def\uff1a<\/span><span style=\"font-size: 16px\">\u540e\u6d6a\u4e91python\u6559\u7a0b<\/span><\/p>\n<p style=\"line-height: 2em\"><span style=\"font-size: 14px\">\u672c\u6587\u6559\u7a0b\u64cd\u4f5c\u73af\u5883\uff1awindows7\u7cfb\u7edf\u3001Python 3.9.1\uff0cDELL G3\u7535\u8111\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u660e 1\u3001Dijkstra\u7b97\u6cd5\u662f\u7ecf\u5178\u7684\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u5b83\u662f\u6570\u636e\u7ed3\u6784\u3001\u56fe\u8bba\u3001\u8fd0\u7b79\u5b66\u7b49\u57fa\u7840\u6559\u5b66\u7b97\u6cd5\u3002 \u4ee4\u4eba\u611f\u5174\u8da3\u7684\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":176180,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-176179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/176179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=176179"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/176179\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/176180"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=176179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=176179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=176179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}