{"id":316,"date":"2011-10-18T14:14:00","date_gmt":"2011-10-18T06:14:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=316"},"modified":"2020-06-06T14:15:05","modified_gmt":"2020-06-06T06:15:05","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84_%e5%9b%be_%e6%9c%80%e7%9f%ad%e8%b7%af%e5%be%84_%e5%bc%97%e6%b4%9b%e4%bc%8a%e5%be%b7floyed%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=316","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u56fe_\u6700\u77ed\u8def\u5f84_\u5f17\u6d1b\u4f0a\u5fb7(Floyed)\u7b97\u6cd5"},"content":{"rendered":"\n<p>Floyed.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\nusing namespace std;\n\n#define MAX_VEX_NUM 20\n#define INFINITY INT_MAX\n\nclass Graph \/\/\u56fe\u7684\u5b58\u50a8\u7ed3\u6784\n{\npublic:\n\tGraph();\n\tint vexnum; \/\/\u9876\u70b9\u6570\n\tint dist&#91;MAX_VEX_NUM]&#91;MAX_VEX_NUM]; \/\/dist&#91;i]&#91;j]\u8868\u793a\u4ecevi\u5230vj\u7684\u5f27\u4e0a\u7684\u6743\u503c\u6ca1\u6709\u5f27\u76f8\u540c\u7684\u8bdd\u5c31\u662fINFINITY\n\tbool path&#91;MAX_VEX_NUM]&#91;MAX_VEX_NUM]&#91;MAX_VEX_NUM]; \/\/path&#91;i]&#91;j]&#91;k]==true\u8868\u793avk\u5728vi\u5230vj\u7684\u8def\u5f84\u4e0a\n\tchar name&#91;MAX_VEX_NUM]; \/\/\u9876\u70b9\u540d\u5b57\n};\n\nGraph::Graph()\n{\n\tvexnum = 0;\n\tfor (int i = 0; i &lt; MAX_VEX_NUM; i++)\n\t\tfor (int j = 0; j &lt; MAX_VEX_NUM; j++)\n\t\t\tdist&#91;i]&#91;j] = INFINITY;\n\tmemset(path, 0, sizeof(path));\n}\n\nclass ShortestPath_Floyd\n{\npublic:\n\tvoid Floyod(); \/\/\u63a5\u53e3\u51fd\u6570\nprivate:\n\tvoid GetVex(); \/\/\u5f97\u5230\u9876\u70b9\n\tvoid GetArc(); \/\/\u5f97\u5230\u5f27\n\tvoid GetShortestPath(); \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n\tvoid PrintPath(); \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n\tGraph g;\n};\n\nvoid ShortestPath_Floyd::Floyod() \/\/\u63a5\u53e3\u51fd\u6570\n{\n\tGetVex(); \/\/\u5f97\u5230\u9876\u70b9\n\tGetArc(); \/\/\u5f97\u5230\u5f27\n\tGetShortestPath(); \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n\tPrintPath(); \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n}\n\nvoid ShortestPath_Floyd::GetVex() \/\/\u5f97\u5230\u9876\u70b9\n{\n\tcout &lt;&lt; \"Please Input The Name Of Each Vertex :\" &lt;&lt; endl;\n\twhile (cin >> g.name&#91;g.vexnum])\n\t\tg.vexnum++;\n\tcin.clear();\n}\n\nvoid ShortestPath_Floyd::GetArc() \/\/\u5f97\u5230\u5f27\n{\n\tcout &lt;&lt; \"Please Input The Information Of Each Arc :\" &lt;&lt; endl\n\t\t\t&lt;&lt; \"tail head weight\" &lt;&lt; endl;\n\tint tail, head, weight;\n\twhile (cin >> tail >> head >> weight)\n\t{\n\t\tg.dist&#91;tail]&#91;head] = weight;\n\t\tg.path&#91;tail]&#91;head]&#91;tail] = g.path&#91;tail]&#91;head]&#91;head] = true; \/\/\u5c06tail\u548chead\u653e\u7f6e\u5728\u4ecetail\u5230head\u7684\u6700\u77ed\u8def\u5f84\u4e0a\n\t}\n\tcin.clear();\n}\n\nvoid ShortestPath_Floyd::GetShortestPath() \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n{\n\tfor (int u = 0; u &lt; g.vexnum; u++)\n\t\tfor (int v = 0; v &lt; g.vexnum; v++)\n\t\t\tfor (int w = 0; w &lt; g.vexnum; w++)\n\t\t\t\tif (g.dist&#91;v]&#91;u] != INFINITY &amp;&amp; g.dist&#91;u]&#91;w] != INFINITY\n\t\t\t\t\t\t&amp;&amp; g.dist&#91;v]&#91;u] + g.dist&#91;u]&#91;w] &lt; g.dist&#91;v]&#91;w])\n\t\t\t\t\/\/\u5982\u679c\u4ecev\u5230u\uff0c\u4eceu\u5230w\u5747\u6709\u8def\u5f84\u76f8\u901a\u4e14\u5927\u4e8ev\u5230w\u7684\u5f53\u524d\u6700\u5c0f\u8def\u5f84\u957f\u5ea6\n\t\t\t\t{\n\t\t\t\t\tg.dist&#91;v]&#91;w] = g.dist&#91;v]&#91;u] + g.dist&#91;u]&#91;w]; \/\/\u66f4\u65b0v\u5230w\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\n\t\t\t\t\tfor (int i = 0; i &lt; g.vexnum; i++)\n\t\t\t\t\t\tg.path&#91;v]&#91;w]&#91;i] = g.path&#91;v]&#91;u]&#91;i] || g.path&#91;u]&#91;w]&#91;i]; \/\/\u5c06\u76f8\u5173\u70b9\u52a0\u5230\u4ecev\u5230w\u7684\u8def\u5f84\u4e0a\n\t\t\t\t}\n}\n\nvoid ShortestPath_Floyd::PrintPath() \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n{\n\tfor (int i = 0; i &lt; g.vexnum; i++)\n\t{\n\t\tfor (int j = 0; j &lt; g.vexnum; j++)\n\t\t{\n\t\t\tif (i != j &amp;&amp; g.dist&#91;i]&#91;j] != INFINITY)\n\t\t\t{\n\t\t\t\tcout &lt;&lt; g.name&#91;i] &lt;&lt; \"-\" &lt;&lt; g.dist&#91;i]&#91;j] &lt;&lt; \"->\" &lt;&lt; g.name&#91;j]\n\t\t\t\t\t\t&lt;&lt; \" : \" &lt;&lt; endl;\n\t\t\t\tfor (int k = 0; k &lt; g.vexnum; k++)\n\t\t\t\t{\n\t\t\t\t\tif (g.path&#91;i]&#91;j]&#91;k])\n\t\t\t\t\t\tcout &lt;&lt; g.name&#91;k] &lt;&lt; \" \";\n\t\t\t\t}\n\t\t\t\tcout &lt;&lt; endl;\n\t\t\t} \/\/if\n\t\t} \/\/for\n\t} \/\/for\n}\n<\/code><\/pre>\n\n\n\n<p>main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include\"Floyd.h\"\n \nint main()\n{\n\tShortestPath_Floyd f;\n\tf.Floyod();\n\tsystem(\"pause\");\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Floyed.h main.cpp<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-06-02-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}