{"id":312,"date":"2011-10-07T14:09:58","date_gmt":"2011-10-07T06:09:58","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=312"},"modified":"2020-06-06T14:11:28","modified_gmt":"2020-06-06T06:11:28","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84_%e5%9b%be_%e6%9c%80%e5%b0%8f%e7%94%9f%e6%88%90%e6%a0%91%e7%ae%97%e6%b3%95%e4%b9%8bprime%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=312","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u56fe_\u6700\u5c0f\u751f\u6210\u6811\u7b97\u6cd5\u4e4bprime\u7b97\u6cd5"},"content":{"rendered":"\n<p>\u957f\u5047\u6700\u540e\u4e00\u5929\uff0c\u524d\u516d\u5929\u65f6\u95f4\u57fa\u672c\u4e0a\u4ec0\u4e48\u90fd\u6ca1\u505a\uff0c\u4eca\u5929\u8d77\u4e86\u4e2a\u5927\u65e9\u51b3\u5b9a\u5728\u81ea\u4e60\u5ba4\u5ea6\u8fc7\uff0c\u628a\u201c\u672a\u7adf\u7684\u4e8b\u4e1a\u201d\u5b8c\u6210\uff0c\u54c8\u54c8\u3002\u3002\u3002\u6570\u636e\u7ed3\u6784\u8fdb\u5c55\u5947\u6162\uff0c\u7b97\u6cd5\u771f\u7684\u597d\u96be\u5b66\uff0c\u7406\u89e3\u4e00\u4e2a\u7b97\u6cd5\u7684\u5b9e\u73b0\u8fc7\u7a0b\u8fd8\u6bd4\u8f83\u5bb9\u6613\uff0c\u771f\u6b63\u628a\u5b83\u5199\u6210\u4ee3\u7801\u53ef\u6ca1\u90a3\u4e48\u5bb9\u6613\u4e86\u3002\u8fd9\u4e2a\u7b97\u6cd5\u7adf\u7136\u4ece\u65e9\u4e0a8\u70b9\u5199\u5230\u73b0\u5728\u624d\u5b9e\u73b0\uff0c\u89e3\u51b3\u4e86\u4e00\u4e9b\u96be\u4ee5\u53d1\u73b0\u7684\u5c0f\u7ec6\u8282\u3002\u611f\u89c9\u4e0d\u9519\uff0c\u52a0\u6cb9\u52aa\u529b\uff01<\/p>\n\n\n\n<p><br>\u6628\u5929\u770b\u5230\u4e86\u4e54\u5e03\u65af\u53bb\u4e16\u7684\u6d88\u606f\uff0c\u6709\u70b9\u610f\u5916\uff0c\u6709\u70b9\u96be\u8fc7\uff0c\u4e00\u4ee3\u5929\u624d\u5c31\u8fd9\u6837\u9668\u843d\u4e86\u3002\u4e54\u5e03\u65af\u662f\u6211\u7684\u5076\u50cf\u6765\u7740\uff0c\u6211\u7684\u684c\u9762\u80cc\u666f\uff0c\u751a\u81f3\u8f93\u5165\u6cd5\u7684\u76ae\u80a4\u90fd\u662f\u4e54\u5e03\u65af\uff0c\u6211\u559c\u6b22\u82f9\u679c\uff0c\u559c\u6b22\u4e54\u5e03\u65af\uff0c\u867d\u7136\u5230\u73b0\u5728\u4e3a\u6b62\u6211\u90fd\u6ca1\u6709\u62e5\u6709\u8fc7\u4e00\u90e8\u82f9\u679c\u7684\u4ea7\u54c1\u3002\u4e54\u5e03\u65af\u4e0d\u53ea\u662f\u4e00\u4e2a\u5929\u624d\uff0c\u4ed6\u4ee3\u8868\u4e86\u4e00\u4e2a\u65f6\u4ee3\uff0c\u5b83\u662f\u4e00\u79cd\u7b26\u53f7\uff0c\u6216\u8005\u8bf4\u4e00\u79cd\u7cbe\u795e\u7684\u8c61\u5f81\u3002\u5728\u82f9\u679c\uff0c\u4ed6\u4e0d\u53ea\u662fCEO\uff0c\u8fd8\u662f\u82f9\u679c\u7684\u7cbe\u795e\u9886\u8896\u3002\u4e0d\u77e5\u9053\u5931\u53bb\u4e86\u4e54\u5e03\u65af\u7684\u82f9\u679c\u8fd8\u80fd\u5426\u5ef6\u7eed\u8f89\u714c\u3002<\/p>\n\n\n\n<p>head.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 STATE int\/\/\u6807\u793a\u662f\u5426\u88ab\u9009\u5165\u4e86\u96c6\u5408\n#define IN 1\n#define OUT 0\n\nclass VexBox \/\/\u9876\u70b9\u7c7b\n{\npublic:\n\tVexBox();\n\tchar vexname; \/\/\u9876\u70b9\u540d\u79f0\n\tint lowcost; \/\/\u8bb0\u5f55\u4f9d\u9644\u4e8e\u8be5\u9876\u70b9\u7684\u6240\u6709\u8fb9\u4e2d\u6700\u77ed\u8fb9\u7684\u8fb9\u957f\n\tint adjvex; \/\/\u8bb0\u5f55\u4e0e\u8be5\u9876\u70b9\u5171\u540c\u6784\u6210\u4e0a\u8ff0\u90a3\u6761\u8fb9\u7684\u53e6\u4e00\u4e2a\u9876\u70b9\n\tSTATE state; \/\/\u8bb0\u5f55\u8be5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u9009\u5165\u96c6\u5408\u4e2d\n};\n\nVexBox::VexBox()\n{\n\tstate = OUT;\n}\n\nclass MSTMap \/\/\u6700\u5c0f\u751f\u6210\u6811\u7684\u5b58\u50a8\u7ed3\u6784\u7c7b\n{\npublic:\n\tMSTMap();\n\tint vexnum; \/\/\u9876\u70b9\u603b\u6570\n\tint cost&#91;MAX_VEX_NUM]&#91;MAX_VEX_NUM]; \/\/\u7528\u4e8c\u7ef4\u6570\u7ec4\u8bb0\u5f55\u6bcf\u6761\u8fb9\u7684\u6743\u503c\n\tVexBox vexbox&#91;MAX_VEX_NUM]; \/\/\u9876\u70b9\u96c6\n};\n\nMSTMap::MSTMap()\n{\n\tvexnum = 0; \/\/\u521d\u59cb\u5316\u9876\u70b9\u6570\n\tfor (int i = 0; i &lt; MAX_VEX_NUM; i++) \/\/\u521d\u59cb\u5316\u6743\u503c\n\t{\n\t\tfor (int j = 0; j &lt; MAX_VEX_NUM; j++)\n\t\t{\n\t\t\tcost&#91;i]&#91;j] = INT_MAX;\n\t\t}\n\t}\n}\n\nclass Prime \/\/\u5b9e\u73b0Prime\u7b97\u6cd5\u7684\u7c7b\n{\npublic:\n\tvoid GetMSTree(); \/\/\u63a5\u53e3\u51fd\u6570\nprivate:\n\tvoid GetVex(); \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n\tvoid GetEdge(); \/\/\u5f97\u5230\u8fb9\u7684\u4fe1\u606f\n\tvoid GetClosedge(); \/\/\u5f97\u5230\u4f9d\u9644\u4e8e\u6bcf\u4e2a\u9876\u70b9\u7684\u6700\u77ed\u8fb9\n\tvoid Spaning(); \/\/\u5904\u7406\u8fc7\u7a0b\n\tMSTMap m;\n};\n\nvoid Prime::GetMSTree() \/\/\u63a5\u53e3\u51fd\u6570\n{\n\tGetVex();\n\tGetEdge();\n\tSpaning();\n}\n;\n\nvoid Prime::GetVex() \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n{\n\tcout &lt;&lt; \"Please Enter The Name Of Each Vertex  :\" &lt;&lt; endl &lt;&lt; endl;\n\tchar name;\n\twhile (cin >> name)\n\t{\n\t\tm.vexbox&#91;m.vexnum++].vexname = name;\n\t}\n\tcin.clear();\n}\n\nvoid Prime::GetEdge() \/\/\u5f97\u5230\u8fb9\u7684\u4fe1\u606f\n{\n\tcout &lt;&lt; \"Please Enter The Edges Follow The Formate As Been Shown Below:\"\n\t\t\t&lt;&lt; endl &lt;&lt; \"vexnum1  vexnum2  weight\" &lt;&lt; endl &lt;&lt; endl;\n\tint vexnum1, vexnum2, weight;\n\twhile (cin >> vexnum1 >> vexnum2 >> weight)\n\t{\n\t\tm.cost&#91;vexnum1]&#91;vexnum2] = m.cost&#91;vexnum2]&#91;vexnum1] = weight;\n\t}\n\tcin.clear();\n}\n\nvoid Prime::GetClosedge() \/\/\u5f97\u5230\u4f9d\u9644\u4e8e\u6bcf\u4e2a\u9876\u70b9\u7684\u6700\u77ed\u8fb9\n{\n\tint min, vexpos;\n\tfor (int i = 0; i &lt; m.vexnum; i++)\n\t{\n\t\tmin = INT_MAX;\n\t\tvexpos = -1;\n\t\tfor (int j = 0; j &lt; m.vexnum; j++)\n\t\t{\n\t\t\tif (m.vexbox&#91;i].state == IN &amp;&amp; m.vexbox&#91;j].state == IN) \/\/\u6d88\u53bb\u4e24\u4e2a\u9876\u70b9\u90fd\u5728\u96c6\u5408\u4e2d\u7684\u8fb9\n\t\t\t\/\/\u8fd9\u4e2a\u6b65\u9aa4\u5f88\u91cd\u8981\u5c11\u4e86\u8fd9\u4e00\u6b65\u7684\u8bdd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u51fa\u9519\n\t\t\t{\n\t\t\t\tm.cost&#91;i]&#91;j] = m.cost&#91;j]&#91;i] = INT_MAX;\n\t\t\t}\n\t\t\tif (m.cost&#91;i]&#91;j] &lt; min)\n\t\t\t{\n\t\t\t\tmin = m.cost&#91;i]&#91;j];\n\t\t\t\tvexpos = j;\n\t\t\t}\n\t\t}\n\t\tm.vexbox&#91;i].adjvex = vexpos;\t\t\t\t\/\/\u8bb0\u5f55\u4f4d\u7f6e\u4fe1\u606f\n\t\tm.vexbox&#91;i].lowcost = min;\t\t\t\t\/\/\u8bb0\u5f55\u6700\u77ed\u8fb9\u7684\u957f\u5ea6\n\t}\n}\n\nvoid Prime::Spaning()\t\t\t\t\/\/\u5904\u7406\u8fc7\u7a0b\n{\n\tGetClosedge();\t\t\t\t\/\/\u521d\u59cb\u5316\n\tint recycle = m.vexnum - 2;\n\tint min, vexpos;\n\t\/\/\u5904\u7406\u524d\u4e24\u4e2a\u9876\u70b9\n\tint weight = m.vexbox&#91;0].lowcost;\n\tcout &lt;&lt; m.vexbox&#91;0].vexname &lt;&lt; \"--\" &lt;&lt; weight &lt;&lt; \"--\"\n\t\t\t&lt;&lt; m.vexbox&#91;m.vexbox&#91;0].adjvex].vexname &lt;&lt; endl;\n\tm.vexbox&#91;0].state = m.vexbox&#91;m.vexbox&#91;0].adjvex].state = IN;\n\tm.cost&#91;0]&#91;m.vexbox&#91;0].adjvex] = m.cost&#91;m.vexbox&#91;0].adjvex]&#91;0] = INT_MAX;\n\t\/\/\u5904\u7406\u5269\u4f59\u7684\u9876\u70b9\n\twhile (recycle--)\n\t{\n\t\tGetClosedge();\n\t\tmin = INT_MAX;\n\t\tvexpos = 0;\n\t\tfor (int i = 0; i &lt; m.vexnum; i++)\n\t\t{\n\t\t\tif (m.vexbox&#91;i].state == IN\n\t\t\t\t\t&amp;&amp; m.vexbox&#91;m.vexbox&#91;i].adjvex].state == OUT\n\t\t\t\t\t&amp;&amp; m.vexbox&#91;i].lowcost &lt; min)\n\t\t\t{\n\t\t\t\tmin = m.vexbox&#91;i].lowcost;\n\t\t\t\tvexpos = i;\n\t\t\t}\n\t\t}\n\t\tcout &lt;&lt; m.vexbox&#91;vexpos].vexname &lt;&lt; \"--\" &lt;&lt; min &lt;&lt; \"--\"\n\t\t\t\t&lt;&lt; m.vexbox&#91;m.vexbox&#91;vexpos].adjvex].vexname &lt;&lt; endl;\n\t\tweight += min;\n\t\tm.vexbox&#91;m.vexbox&#91;vexpos].adjvex].state = IN;\n\t\tm.cost&#91;vexpos]&#91;m.vexbox&#91;vexpos].adjvex] =\n\t\t\t\tm.cost&#91;m.vexbox&#91;vexpos].adjvex]&#91;vexpos] = INT_MAX;\n\t}\n\tcout &lt;&lt; \"Weight = \" &lt;&lt; weight &lt;&lt; endl;\t\t\t\t\/\/\u8f93\u51fa\u603b\u6743\u503c\u548c\n}\n<\/code><\/pre>\n\n\n\n<p>main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include\"head.h\"\n\nint main()\n{\n\tPrime p;\n\tp.GetMSTree();\n\tsystem(\"pause\");\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u957f\u5047\u6700\u540e\u4e00\u5929\uff0c\u524d\u516d\u5929\u65f6\u95f4\u57fa\u672c\u4e0a\u4ec0\u4e48\u90fd\u6ca1\u505a\uff0c\u4eca\u5929\u8d77\u4e86\u4e2a\u5927\u65e9\u51b3\u5b9a\u5728\u81ea\u4e60\u5ba4\u5ea6\u8fc7\uff0c\u628a\u201c\u672a\u7adf\u7684\u4e8b\u4e1a\u201d\u5b8c\u6210\uff0c\u54c8\u54c8\u3002\u3002\u3002\u6570\u636e\u7ed3\u6784\u8fdb\u5c55\u5947\u6162\uff0c\u7b97\u6cd5\u771f\u7684\u597d\u96be\u5b66\uff0c\u7406\u89e3\u4e00\u4e2a\u7b97\u6cd5\u7684\u5b9e\u73b0\u8fc7&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=312\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/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-312","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\/312","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=312"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/312\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=312"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}