{"id":302,"date":"2011-10-13T14:03:05","date_gmt":"2011-10-13T06:03:05","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=302"},"modified":"2020-06-06T14:04:18","modified_gmt":"2020-06-06T06:04:18","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84_%e5%9b%be_%e6%9c%89%e5%90%91%e6%97%a0%e7%8e%af%e5%9b%be%e5%ba%94%e7%94%a8%e4%b9%8b%e6%b1%82%e5%85%b3%e9%94%ae%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=302","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u56fe_\u6709\u5411\u65e0\u73af\u56fe\u5e94\u7528\u4e4b\u6c42\u5173\u952e\u8def\u5f84"},"content":{"rendered":"\n<p>head.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;stack>\nusing namespace std;\n\n#define MAX_VEX_NUM 20\/\/\u6700\u5927\u8282\u70b9\u6570\n\nclass NextNode \/\/\u5b58\u50a8\u5f27\u8282\u70b9\u76f8\u5173\u4fe1\u606f\/\/\u56fe\u4ee5\u90bb\u63a5\u8868\u505a\u5b58\u50a8\u7ed3\u6784\n{\npublic:\n\tNextNode();\n\tint nextvex; \/\/\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u7f16\u53f7\n\tint weight; \/\/\u5f27\u4e0a\u7684\u6743\u503c\n\tNextNode *next;\n};\n\nNextNode::NextNode()\n{\n\tnextvex = weight = 0;\n\tnext = NULL;\n}\n\nclass VexNode \/\/\u8282\u70b9\u4fe1\u606f\n{\npublic:\n\tVexNode();\n\tchar name; \/\/\u8282\u70b9\u540d\u79f0\n\tint ve; \/\/\u4e8b\u4ef6\u7684\u6700\u65e9\u53d1\u751f\u65f6\u95f4\n\tint vl; \/\/\u65f6\u95f4\u7684\u6700\u665a\u53d1\u751f\u65f6\u95f4\n\tint indegree; \/\/\u8282\u70b9\u5165\u5ea6\n\tNextNode *firstnext; \/\/\u6307\u5411\u5b58\u50a8\u4ee5\u8be5\u8282\u70b9\u505a\u5f27\u5c3e\u7684\u5f27\u7684\u5f27\u5934\u9876\u70b9\u76f8\u5173\u4fe1\u606f\u7684\u8282\u70b9\n};\n\nVexNode::VexNode()\n{\n\tve = vl = indegree = 0;\n\tfirstnext = 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 CriPath \/\/Critical Path\n{\npublic:\n\tvoid GetCriticalPath(); \/\/\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\n\tvoid GetVe(); \/\/\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u65e9\u53d1\u751f\u65f6\u95f4\n\tvoid GetLe(); \/\/\u9006\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u665a\u53d1\u751f\u65f6\u95f4\n\tvoid PrintCriPath(); \/\/\u8f93\u51fa\u5173\u952e\u8def\u5f84\n\tVexBox v; \/\/\u9876\u70b9\u96c6\u5408\n\tstack&lt;int> s; \/\/\u50a8\u5b58\u62d3\u6251\u6709\u5e8f\u5e8f\u5217\n\tbool available; \/\/\u6807\u793a\u8be5\u56fe\u662f\u5426\u662f\u65e0\u73af\u56fe\n};\n\nvoid CriPath::GetCriticalPath() \/\/\u63a5\u53e3\u51fd\u6570\n{\n\tGetVex(); \/\/\u5f97\u5230\u9876\u70b9\u4fe1\u606f\n\tGetArc(); \/\/\u5f97\u5230\u5f27\u7684\u76f8\u5173\u4fe1\u606f\n\tGetVe(); \/\/\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u65e9\u53d1\u751f\u65f6\u95f4\n\tGetLe(); \/\/\u9006\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u665a\u53d1\u751f\u65f6\u95f4\n\tPrintCriPath(); \/\/\u8f93\u51fa\u5173\u952e\u8def\u5f84\n}\n\nvoid CriPath::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 CriPath::GetArc() \/\/\u5f97\u5230\u5f27\u7684\u76f8\u5173\u4fe1\u606f\n{\n\tcout &lt;&lt; \"Please Input The Information Of Eack 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\tv.vexbox&#91;head].indegree++;\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\nvoid CriPath::GetVe() \/\/\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u65e9\u53d1\u751f\u65f6\u95f4\n{\n\tNextNode *p;\n\tbool find = false;\n\tint count = 0;\n\twhile (1) \/\/\u62d3\u6251\u6392\u5e8f\n\t{\n\t\tfor (int i = 0; i &lt; v.vexnum; i++) \/\/\u626b\u63cf\u6240\u6709\u8282\u70b9\n\t\t{\n\t\t\tif (v.vexbox&#91;i].indegree == 0) \/\/\u5982\u679c\u5165\u5ea6\u4e3a\u96f6\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t\tfind = true;\n\t\t\t\ts.push(i); \/\/\u5b58\u50a8\u62d3\u6251\u6709\u5e8f\u5e8f\u5217\n\t\t\t\tv.vexbox&#91;i].indegree--;\n\t\t\t\tp = v.vexbox&#91;i].firstnext;\n\t\t\t\twhile (p != NULL)\n\t\t\t\t{\n\t\t\t\t\tif (v.vexbox&#91;i].ve + p->weight > v.vexbox&#91;p->nextvex].ve) \/\/\u83b7\u5f97\u5f53\u524d\u8282\u70b9\u7684ve\n\t\t\t\t\t\tv.vexbox&#91;p->nextvex].ve = v.vexbox&#91;i].ve + p->weight;\n\t\t\t\t\tv.vexbox&#91;p->nextvex].indegree--;\n\t\t\t\t\tp = p->next;\n\t\t\t\t} \/\/while\n\t\t\t\tbreak;\n\t\t\t} \/\/if\n\t\t} \/\/for\n\t\tif (find == true)\n\t\t\tfind = false;\n\t\telse\n\t\t\tbreak;\n\t} \/\/while\n\tif (count &lt; v.vexnum) \/\/\u5224\u65ad\u56fe\u4e2d\u662f\u5426\u5b58\u5728\u73af\n\t{\n\t\tcout &lt;&lt; \"There Are Cycle In This Graph .\" &lt;&lt; endl;\n\t\tavailable = false;\n\t}\n\telse\n\t{\n\t\tavailable = true;\n\t}\n}\n\nvoid CriPath::GetLe() \/\/\u9006\u62d3\u6251\u6392\u5e8f\u5e76\u6c42\u5f97\u4e8b\u4ef6\u7684\u6700\u665a\u53d1\u751f\u65f6\u95f4\n{\n\tif (available == true) \/\/\u5982\u679c\u8be5\u56fe\u662f\u65e0\u73af\u56fe\n\t{\n\t\tNextNode *p;\n\t\tint node = s.top(); \/\/\u5904\u7406\u6c47\u70b9\n\t\ts.pop();\n\t\tv.vexbox&#91;node].vl = v.vexbox&#91;node].ve;\n\t\twhile (!s.empty()) \/\/\u5904\u7406\u62d3\u6251\u6709\u5e8f\u5e8f\u5217\u4e2d\u5269\u4e0b\u7684\u8282\u70b9\n\t\t{\n\t\t\tnode = s.top();\n\t\t\ts.pop();\n\t\t\tp = v.vexbox&#91;node].firstnext;\n\t\t\twhile (p != NULL) \/\/\u83b7\u5f97\u5f53\u524d\u8282\u70b9\u7684vl\n\t\t\t{\n\t\t\t\tif (v.vexbox&#91;node].vl == 0)\n\t\t\t\t\tv.vexbox&#91;node].vl = v.vexbox&#91;p->nextvex].vl - p->weight;\n\t\t\t\telse if (v.vexbox&#91;p->nextvex].vl - p->weight\n\t\t\t\t\t\t&lt; v.vexbox&#91;node].vl)\n\t\t\t\t\tv.vexbox&#91;node].vl = v.vexbox&#91;p->nextvex].vl - p->weight;\n\t\t\t\tp = p->next;\n\t\t\t} \/\/while\n\t\t} \/\/while\n\t} \/\/if\n}\n\nvoid CriPath::PrintCriPath() \/\/\u8f93\u51fa\u5173\u952e\u8def\u5f84\n{\n\tif (available == true) \/\/\u5982\u679c\u8be5\u56fe\u662f\u65e0\u73af\u56fe\n\t{\n\t\tfor (int i = 0; i &lt; v.vexnum; i++)\n\t\t{\n\t\t\tif (v.vexbox&#91;i].ve == v.vexbox&#91;i].vl)\n\t\t\t\/\/\u8f93\u51fa\u6240\u6709\u6700\u65e9\u53d1\u751f\u4e8b\u4ef6\u548c\u6700\u665a\u53d1\u751f\u65f6\u95f4\u76f8\u540c\u7684\u70b9\u8fd9\u4e9b\u70b9\u6784\u6210\u4e86\u4e00\u6761\u5173\u952e\u8def\u5f84\n\t\t\t{\n\t\t\t\tcout &lt;&lt; v.vexbox&#91;i].name &lt;&lt; endl;\n\t\t\t} \/\/if\n\t\t} \/\/for\n\t} \/\/if\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\tCriPath cp;\n\tcp.GetCriticalPath();\n\tsystem(\"pause\");\n\treturn 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>head.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-302","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\/302","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=302"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/302\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}