{"id":609,"date":"2013-09-11T13:50:22","date_gmt":"2013-09-11T05:50:22","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=609"},"modified":"2013-09-11T13:50:22","modified_gmt":"2013-09-11T05:50:22","slug":"1030-travel-plan-30","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=609","title":{"rendered":"1030. Travel Plan (30)"},"content":{"rendered":"<p>\u65f6\u95f4\u9650\u5236\uff1a400ms<br \/>\nA traveler&#8217;s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his\/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (&lt;=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:<br \/>\nCity1 City2 Distance Cost<br \/>\nwhere the numbers are all integers no more than 500, and are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.<br \/>\n<b>Sample Input<\/b><\/p>\n<pre>4 5 0 3\n0 1 1 20\n1 3 2 30\n0 3 4 10\n0 2 2 20\n2 3 1 20<\/pre>\n<p><b>Sample Output<\/b><\/p>\n<pre>0 2 3 3 40<\/pre>\n<p>=================================================<br \/>\n\u8fd9\u9898\u76ee\u561b\u662f\u5355\u5143\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u7684\u53d8\u79cd\uff0c\u4e3b\u8981\u53d8\u4e86\u4fe9\u5730\u65b9\uff1a<br \/>\n1.\u6700\u77ed\u8def\u5f84\u53ef\u80fd\u4e0d\u6b621\u6761<br \/>\n2.\u6700\u540e\u627e\u51fa\u6700\u77ed\u8def\u4e2d\u6743\u91cdcost\u6700\u5c0f\u7684\u4e00\u4e2a<br \/>\n\u6211\u4e0d\u77e5\u9053\u6211\u7684\u89e3\u6cd5\u4e2d\u6570\u636e\u7ed3\u6784\u662f\u5426\u6709\u5197\u4f59\u7684\uff0c\u4e0d\u8fc7\u53cd\u6b63\u662f\u901a\u8fc7\u4e86\uff08\u53ef\u80fd\u5185\u5b58\u591a\u5360\u70b9\u5427\uff09<br \/>\n\u4e3b\u8981\u5c31\u662f\u7528Dijkstra\u7b97\u6cd5\uff0c\u4f46\u662f<br \/>\n1.\u66f4\u65b0\u8ddd\u79bb\u7ec4<dist, LastHop>\u7684\u65f6\u5019\uff0c\u5982\u679c\u78b0\u5230\u8ddf\u5f53\u524d\u6700\u597d\u8ddd\u79bb\u4e00\u6837\u7684\uff0c\u4e5f\u5e94\u8be5\u52a0\u4e0a<br \/>\n2.\u591a\u7b97\u4e00\u4e2a<cost, LastHop>\u7684\u6700\u4f73\u6743\u91cd\u7ec4<br \/>\n   \uff0c\u66f4\u65b0\u6761\u4ef6\u662f\uff1aA)\u53d1\u73b0\u4e86\u66f4\u597d\u7684\u8ddd\u79bb\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u66f4\u65b0\u6743\u91cd\u7ec4\uff0c\u56e0\u4e3a\u8ddd\u79bb\u662f\u9996\u5148\u8981\u8003\u8651\u7684<br \/>\n               B)\u53d1\u73b0\u4e86\u548c\u73b0\u5728\u76f8\u540c\u7684\u8ddd\u79bb\u7684\u65f6\u5019\uff0c\u5982\u679ccost\u6bd4\u8f83\u5c0f\uff0c\u66f4\u65b0\u4e00\u4e0b<br \/>\n\u8fd9\u6837\u7b49Dijkstra\u7b97\u6cd5\u7ed3\u675f\u540e\uff0c\u6211\u4eec\u5df2\u7ecf\u80fd\u5f97\u5230\u6700\u77ed\u8ddd\u79bb\u4e14\u6743\u91cdcost\u6700\u5c0f\u7684\u8def\u5f84\u4e86\u3002\u4ee5\u524d\u8fd8\u60f3\u7740\u518d\u6839\u636e\u6700\u77ed\u8ddd\u79bb\u7684\u5404\u79cd\u56de\u6eaf\u904d\u5386\u4e00\u4e0b\u6c42\u6700\u5c0f\u8017\u8d39\uff0c\u4eca\u5929\u4e00\u60f3\u5176\u5b9e\u6839\u672c\u4e0d\u9700\u8981\uff01<br \/>\n\u7b97\u6cd5\u4e2d\u8fd8\u6709\u70b9\u4e0d\u786e\u5b9a\u7684\u5730\u65b9\uff0c\u89c1\u4e0b\u9762\u7684\u6ce8\u91ca\u3002<br \/>\n====================================================<br \/>\n<code lang=\"c++\"><br \/>\n#include <stdio.h><br \/>\n#include <vector><br \/>\n#include <stack><br \/>\n#include <set><br \/>\nusing namespace std;<br \/>\nstruct Node<br \/>\n{<br \/>\n\tvector<int> best_from;<br \/>\n\tint best_dist;<br \/>\n\tint best_cost;<br \/>\n\tint best_cost_from;<br \/>\n\tvector<int> linkedNodes;<br \/>\n\tNode()<br \/>\n\t{<br \/>\n\t\tbest_dist = 65535;<br \/>\n\t\tbest_cost = 65535;<br \/>\n\t\tbest_cost_from = -1;<br \/>\n\t}<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\n\tint N, M, S, D;<br \/>\n\tscanf(\"%d %d %d %d\", &N, &M, &S, &D);<br \/>\n\tint **distances = new int*[N];<br \/>\n\tint **costs = new int*[N];<br \/>\n\tNode *nodes = new Node[N];<br \/>\n\tset<int> working;<br \/>\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tdistances[i] = new int[N];\n\t\tcosts[i] = new int[N];\n\t\tif(i != S)\n\t\t\tworking.insert(i);\n\t}\n\tfor(int i=0; i<m; i++)\n\t{\n\t\tint c1, c2, d, c;\n\t\tscanf(\"%d %d %d %d\", &#038;c1, &#038;c2, &#038;d, &#038;c);\n\t\tdistances[c1][c2] = d;\n\t\tdistances[c2][c1] = d;\n\t\tcosts[c1][c2] = c;\n\t\tcosts[c2][c1] = c;\n\t\tnodes[c1].linkedNodes.push_back(c2);\n\t\tnodes[c2].linkedNodes.push_back(c1);\n\t}\n\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\t\/\/\u521d\u59cb\u5316\u6e90\u8282\u70b9\n\tnodes[S].best_dist = 0;\n\tnodes[S].best_cost = 0;\n\tint last_found_best = S; \/\/\u4e0a\u6b21\u627e\u5230\u7684\u786e\u5b9a\u70b9\n\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\twhile(!working.empty())\n\t{\n\t\t\/\/\u4ece\u4e0a\u6b21\u7684\u786e\u5b9a\u70b9\u4e2d\u66f4\u65b0\n\t\tNode *ptrCurrNode = &#038;nodes[last_found_best];\n\t\t\/\/int currBestId = -1;\n\t\t\/\/int currBestDist = 65535;\n\t\tvector<int>::iterator it = ptrCurrNode->linkedNodes.begin();<br \/>\n\t\tfor(; it!=ptrCurrNode->linkedNodes.end(); it++)<br \/>\n\t\t{<br \/>\n\t\t\tint linkedId = *it;<br \/>\n\t\t\tif( working.find(linkedId) == working.end())<br \/>\n\t\t\t{<br \/>\n\t\t\t\t\/\/\u8fd9\u4e2a\u70b9\u5df2\u7ecf\u662f\u786e\u5b9a\u70b9\u4e86...<br \/>\n\t\t\t\t\/\/\u662f\u4e0d\u662f\u4e0d\u518d\u8003\u8651?<br \/>\n\t\t\t}<br \/>\n\t\t\tNode *ptrLinkedNode = &nodes[linkedId];<br \/>\n\t\t\tint distViaMe = ptrCurrNode->best_dist + distances[last_found_best][linkedId];<br \/>\n\t\t\tint costViaMe = ptrCurrNode->best_cost + costs[last_found_best][linkedId];<br \/>\n\t\t\tif(distViaMe < ptrLinkedNode->best_dist)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tptrLinkedNode->best_dist = distViaMe;<br \/>\n\t\t\t\tptrLinkedNode->best_from.clear();<br \/>\n\t\t\t\tptrLinkedNode->best_from.push_back(last_found_best);<br \/>\n\t\t\t\tptrLinkedNode->best_cost = costViaMe;<br \/>\n\t\t\t\tptrLinkedNode->best_cost_from = last_found_best;<br \/>\n\t\t\t}<br \/>\n\t\t\telse if (distViaMe == ptrLinkedNode->best_dist)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tptrLinkedNode->best_from.push_back(last_found_best);<br \/>\n\t\t\t\tif(costViaMe < ptrLinkedNode->best_cost)<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\t\tptrLinkedNode->best_cost = costViaMe;<br \/>\n\t\t\t\t\tptrLinkedNode->best_cost_from = last_found_best;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t\t\/\/\u66f4\u65b0\u5b8c\u540e\u627e\u51fa\u4e0d\u786e\u5b9a\u70b9\u4e2d\u6700\u5c0f\u8ddd\u79bb\u7684\uff0c\u52a0\u5165\u786e\u5b9a\u70b9\u4e2d<br \/>\n\t\tint minDist = 65535;<br \/>\n\t\tset<int>::iterator set_it = working.begin();<br \/>\n\t\tfor(; set_it != working.end(); set_it++)<br \/>\n\t\t{<br \/>\n\t\t\tint id = *set_it;<br \/>\n\t\t\tif( nodes[id].best_dist < minDist)\n\t\t\t{\n\t\t\t\tminDist = nodes[id].best_dist;\n\t\t\t\tlast_found_best = id;\n\t\t\t}\n\t\t}\n\t\tset_it = working.find(last_found_best);\n\t\tworking.erase(set_it);\n\t}\n\t\/\/\u7b2c\u4e09\u6b65\n\tstack<int> route;<br \/>\n\tint currIndex = D;<br \/>\n\twhile(currIndex != S)<br \/>\n\t{<br \/>\n\t\troute.push(currIndex);<br \/>\n\t\tcurrIndex = nodes[currIndex].best_cost_from;<br \/>\n\t}<br \/>\n\tprintf(\"%d \", S);<br \/>\n\twhile(!route.empty())<br \/>\n\t{<br \/>\n\t\tprintf(\"%d \", route.top());<br \/>\n\t\troute.pop();<br \/>\n\t}<br \/>\n\tprintf(\"%d %d\", nodes[D].best_dist, nodes[D].best_cost);<br \/>\n\treturn 0;<br \/>\n}<br \/>\n<\/code><br \/>\n======================================================<\/p>\n<pre>\n\u6d4b\u8bd5\u70b9\t\u7ed3\u679c\t\u7528\u65f6(ms)\t\u5185\u5b58(kB)\t\u5f97\u5206\/\u6ee1\u5206\n0\t\u7b54\u6848\u6b63\u786e\t0\t710\t18\/18\n1\t\u7b54\u6848\u6b63\u786e\t0\t790\t6\/6\n2\t\u7b54\u6848\u6b63\u786e\t0\t2570\t6\/6\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236\uff1a400ms A traveler&#8217;s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his\/her starting city and the destination. If such a shortest path is not unique, you are supposed to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=609"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/609\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}