{"id":300,"date":"2011-10-13T14:01:31","date_gmt":"2011-10-13T06:01:31","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=300"},"modified":"2020-06-06T14:02:54","modified_gmt":"2020-06-06T06:02:54","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_%e7%8b%84%e6%9d%b0%e6%96%af%e7%89%b9%e6%8b%89dijkstra%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=300","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u56fe_\u6700\u77ed\u8def\u5f84_\u72c4\u6770\u65af\u7279\u62c9(Dijkstra)\u7b97\u6cd5"},"content":{"rendered":"\n<p>\u6b64\u7b97\u6cd5\u6ca1\u6709\u91c7\u7528\u300a\u6570\u636e\u7ed3\u6784C\u8bed\u8a00\u7248\u300b\u4e2d\u7684\u5b58\u50a8\u7ed3\u6784\uff0c\u800c\u662f\u91c7\u7528\u90bb\u63a5\u8868\u7684\u65b9\u6cd5\u5b58\u50a8\u56fe\uff0c\u7ecf\u8fc7\u6539\u8fdb\uff0c\u8fd8\u80fd\u8f93\u51fa\u6700\u77ed\u8def\u5f84\u3002<\/p>\n\n\n\n<p>Dijkstra.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#define CANTFIND -1\n\nclass Path \/\/\u8bb0\u5f55\u6e90\u70b9\u5230\u6bcf\u4e00\u4e2a\u70b9\u7684\u8def\u5f84\n{\npublic:\n\tPath();\n\tint pathnode;\n\tPath *next;\n};\n\nPath::Path()\n{\n\tpathnode = 0;\n\tnext = NULL;\n}\n\nclass NextNode \/\/\u90bb\u63a5\u8868\u5b58\u50a8\u4ee5\u67d0\u8282\u70b9\u4e3a\u5c3e\u7684\u5f27\u7684\u76f8\u5173\u4fe1\u606f\n{\npublic:\n\tNextNode();\n\tint nextvex; \/\/\u9876\u70b9\u7f16\u53f7\n\tint weight; \/\/\u5f27\u4e0a\u7684\u6743\u503c\n\tNextNode *next; \/\/\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\n};\n\nNextNode::NextNode()\n{\n\tnextvex = weight = 0;\n\tnext = NULL;\n}\n\nclass VexNode \/\/\u9876\u70b9\u76f8\u5173\u4fe1\u606f\n{\npublic:\n\tVexNode();\n\tchar name; \/\/\u9876\u70b9\u540d\u79f0\n\tbool choose; \/\/\u6807\u793a\u6b64\u8282\u70b9\u6807\u793a\u7684\u6700\u77ed\u8def\u5f84\u662f\u5426\u5df2\u7ecf\u88ab\u9009\u62e9\n\tint dist; \/\/\u6b64\u8282\u70b9\u5f53\u524d\u5b58\u653e\u7684\u6700\u77ed\u8def\u5f84\n\tNextNode *firstnext;\n\tPath *path;\n};\n\nVexNode::VexNode()\n{\n\tchoose = false;\n\tdist = INFINITY;\n\tfirstnext = NULL;\n\tpath = NULL;\n}\n\nclass VexBox \/\/\u9876\u70b9\u96c6\u5408\n{\npublic:\n\tVexBox();\n\tint vexnum; \/\/\u5b9a\u70b9\u6570\u76ee\n\tVexNode vexbox&#91;MAX_VEX_NUM]; \/\/\u9876\u70b9\u96c6\u5408\n};\n\nVexBox::VexBox()\n{\n\tvexnum = 0;\n}\n\nclass ShortestPath_DIJ\n{\npublic:\n\tvoid ShortestPath(); \/\/\u63a5\u53e3\u51fd\u6570\nprivate:\n\tvoid GetVex(); \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n\tvoid GetArc(); \/\/\u5f97\u5230\u5f27\u7684\u76f8\u5173\u4fe1\u606f\t\n\tvoid GetShortestPath(); \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n\tvoid PrintShortestPath(); \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n\tint GetShortestNode(); \/\/\u5f97\u5230dist\u6700\u5c0f\u7684\u8282\u70b9\n\tvoid UpdatePath(int, int); \/\/\u66f4\u65b0\u8def\u5f84\n\tvoid ReMove(Path*); \/\/\u9500\u6bc1\u539f\u8def\u5f84\n\tVexBox v; \/\/\u9876\u70b9\u96c6\u5408\n\tint sourse;\n};\n\nvoid ShortestPath_DIJ::ShortestPath()\n{\n\tGetVex(); \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n\tGetArc(); \/\/\u5f97\u5230\u5f27\u7684\u76f8\u5173\u4fe1\u606f\t\n\tGetShortestPath(); \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n\tPrintShortestPath(); \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n}\n\nvoid ShortestPath_DIJ::GetVex() \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n{\n\tcout &lt;&lt; \"Please Input The Name Of Each Vertex :\" &lt;&lt; endl &lt;&lt; endl;\n\tchar name;\n\twhile (cin >> name)\n\t{\n\t\tv.vexbox&#91;v.vexnum++].name = name;\n\t}\n\tcin.clear();\n}\n\nvoid ShortestPath_DIJ::GetArc() \/\/\u5f97\u5230\u5f27\u7684\u76f8\u5173\u4fe1\u606f\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\tNextNode *newnode, *p;\n\twhile (cin >> tail >> head >> weight)\n\t{\n\t\tnewnode = new NextNode;\n\t\tnewnode->nextvex = head;\n\t\tnewnode->weight = weight;\n\t\tif ((p = v.vexbox&#91;tail].firstnext) == NULL)\n\t\t{\n\t\t\tv.vexbox&#91;tail].firstnext = newnode;\n\t\t} \/\/if\n\t\telse\n\t\t{\n\t\t\twhile (p->next != NULL)\n\t\t\t\tp = p->next;\n\t\t\tp->next = newnode;\n\t\t} \/\/else\n\t} \/\/while\n\tcin.clear();\n}\n\nint ShortestPath_DIJ::GetShortestNode() \/\/\u5f97\u5230dist\u6700\u5c0f\u7684\u8282\u70b9\n{\n\tint ans = CANTFIND, min = INT_MAX;\n\tfor (int i = 0; i &lt; v.vexnum; i++)\n\t{\n\t\tif (v.vexbox&#91;i].choose == false &amp;&amp; v.vexbox&#91;i].dist &lt; min)\n\t\t{\n\t\t\tmin = v.vexbox&#91;i].dist;\n\t\t\tans = i;\n\t\t} \/\/if\n\t} \/\/for\n\tv.vexbox&#91;ans].choose = true; \/\/\u6807\u8bb0\u4e3a\u5df2\u9009\u62e9\n\treturn ans;\n}\n\nvoid ShortestPath_DIJ::ReMove(Path *p) \/\/\u9500\u6bc1\u539f\u8def\u5f84\n{\n\tif (p->next != NULL)\n\t\tReMove(p->next);\n\tdelete p;\n}\n\nvoid ShortestPath_DIJ::UpdatePath(int a, int b) \/\/\u66f4\u65b0\u8def\u5f84\n{\n\tPath *p, *pt, *newpath;\n\tReMove(v.vexbox&#91;b].path);\n\tnewpath = new Path;\n\tnewpath->pathnode = v.vexbox&#91;a].path->pathnode;\n\tv.vexbox&#91;b].path = newpath;\n\tp = v.vexbox&#91;a].path->next;\n\tpt = v.vexbox&#91;b].path;\n\twhile (p != NULL)\n\t{\n\t\tnewpath = new Path;\n\t\tnewpath->pathnode = p->pathnode;\n\t\tpt->next = newpath;\n\t\tp = p->next;\n\t\tpt = pt->next;\n\t}\n}\n\nvoid ShortestPath_DIJ::GetShortestPath() \/\/\u5f97\u5230\u6700\u77ed\u8def\u5f84\n{\n\tPath *newpath, *pt;\n\tcout &lt;&lt; \"Please Input The Sourse :\" &lt;&lt; endl;\n\twhile (cin >> sourse &amp;&amp; !(sourse &lt; v.vexnum))\n\t\t; \/\/\u5f97\u5230\u7b26\u5408\u6761\u4ef6\u7684\u6e90\u70b9\n\tcout &lt;&lt; v.vexbox&#91;sourse].name &lt;&lt; \" : Start\" &lt;&lt; endl;\n\tv.vexbox&#91;sourse].dist = 0; \/\/\u5bf9\u6e90\u70b9\u7684\u76f8\u5173\u5904\u7406\n\tfor (int i = 0; i &lt; v.vexnum; i++) \/\/\u521d\u59cb\u5316\u8def\u5f84\n\t{\n\t\tif (i != sourse)\n\t\t{\n\t\t\tnewpath = new Path;\n\t\t\tnewpath->pathnode = sourse;\n\t\t\tv.vexbox&#91;i].path = newpath;\n\t\t}\n\t}\n\tNextNode *p;\n\tp = v.vexbox&#91;sourse].firstnext;\n\twhile (p != NULL)\n\t{\n\t\tnewpath = new Path;\n\t\tnewpath->pathnode = p->nextvex;\n\t\tv.vexbox&#91;p->nextvex].path->next = newpath;\n\t\tv.vexbox&#91;p->nextvex].dist = p->weight;\n\t\tp = p->next;\n\t}\n\tint ncase = v.vexnum - 1;\n\tint node;\n\twhile (ncase--) \/\/\u5904\u7406\u6e90\u70b9\u5230\u5176\u4ed6\u70b9\u7684\u6700\u77ed\u8def\u5f84\u957f\u5ea6\n\t{\n\t\tnode = GetShortestNode(); \/\/\u83b7\u5f97\u5f53\u524d\u60c5\u51b5\u4e0b\u672a\u88ab\u9009\u62e9\u7684\u6700\u77ed\u8fb9\n\t\tif (node == CANTFIND) \/\/\u63a7\u5236\u8df3\u51fa\n\t\t\tbreak;\n\t\tp = v.vexbox&#91;node].firstnext;\n\t\twhile (p != NULL) \/\/\u5982\u6709\u5fc5\u8981\u4fee\u6539\u548c\u8be5\u9876\u70b9\u76f8\u5173\u7684\u8282\u70b9\u7684Dist\u503c\n\t\t{\n\t\t\tif (v.vexbox&#91;node].dist + p->weight &lt; v.vexbox&#91;p->nextvex].dist)\n\t\t\t{\n\t\t\t\tv.vexbox&#91;p->nextvex].dist = v.vexbox&#91;node].dist + p->weight;\n\t\t\t\tUpdatePath(node, p->nextvex);\n\t\t\t\tnewpath = new Path;\n\t\t\t\tnewpath->pathnode = p->nextvex;\n\t\t\t\tpt = v.vexbox&#91;p->nextvex].path;\n\t\t\t\twhile (pt->next != NULL)\n\t\t\t\t\tpt = pt->next;\n\t\t\t\tpt->next = newpath;\n\t\t\t}\n\t\t\tp = p->next;\n\t\t}\n\t}\n}\n\nvoid ShortestPath_DIJ::PrintShortestPath() \/\/\u6253\u5370\u6700\u77ed\u8def\u5f84\n{\n\tPath *p;\n\tfor (int i = 0; i &lt; v.vexnum; i++)\n\t{\n\t\tif (i != sourse)\n\t\t{\n\t\t\tp = v.vexbox&#91;i].path;\n\t\t\tcout &lt;&lt; v.vexbox&#91;i].name &lt;&lt; \" : \";\n\t\t\tif (v.vexbox&#91;i].dist == INFINITY)\n\t\t\t\tcout &lt;&lt; \"\u221e\" &lt;&lt; endl;\n\t\t\telse\n\t\t\t{\n\t\t\t\tcout &lt;&lt; v.vexbox&#91;i].dist &lt;&lt; \" Path :\"; \/\/\u6253\u5370\u8def\u5f84\n\t\t\t\tcout &lt;&lt; v.vexbox&#91;p->pathnode].name;\n\t\t\t\tp = p->next;\n\t\t\t\twhile (p != NULL)\n\t\t\t\t{\n\t\t\t\t\tcout &lt;&lt; \"->\" &lt;&lt; v.vexbox&#91;p->pathnode].name;\n\t\t\t\t\tp = p->next;\n\t\t\t\t}\n\t\t\t\tcout &lt;&lt; endl;\n\t\t\t}\n\t\t} \/\/if\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\"Dijkstra.h\"\n \nint main()\n{\n\tShortestPath_DIJ d;\n\td.ShortestPath();\n\tsystem(\"pause\");\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6b64\u7b97\u6cd5\u6ca1\u6709\u91c7\u7528\u300a\u6570\u636e\u7ed3\u6784C\u8bed\u8a00\u7248\u300b\u4e2d\u7684\u5b58\u50a8\u7ed3\u6784\uff0c\u800c\u662f\u91c7\u7528\u90bb\u63a5\u8868\u7684\u65b9\u6cd5\u5b58\u50a8\u56fe\uff0c\u7ecf\u8fc7\u6539\u8fdb\uff0c\u8fd8\u80fd\u8f93\u51fa\u6700\u77ed\u8def\u5f84\u3002 Dijkstra.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-300","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\/300","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=300"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}