{"id":516,"date":"2015-05-06T13:53:14","date_gmt":"2015-05-06T05:53:14","guid":{"rendered":"https:\/\/www.yesybl.com\/?p=516"},"modified":"2019-07-06T02:39:43","modified_gmt":"2019-07-05T18:39:43","slug":"dijkstra%e7%ae%97%e6%b3%95c%e6%bc%94%e7%a4%ba%e7%a8%8b%e5%ba%8f-3","status":"publish","type":"post","link":"https:\/\/www.yesybl.com\/?p=516","title":{"rendered":"Dijkstra\u7b97\u6cd5C#\u6f14\u793a\u7a0b\u5e8f"},"content":{"rendered":"<p>\u6700\u8fd1<a title=\"Dijkstar\u7b97\u6cd5\u8be6\u89e3\" href=\"https:\/\/www.yesybl.com\/?p=520\">\u5b66\u4e60Dijkstra\u7b97\u6cd5<\/a>\u548cC#\uff0c\u7528C#\u5199\u4e86\u4e00\u4e2aDijkstra\u7b97\u6cd5\u7684\u6f14\u793a\u7a0b\u5e8f\u3002\u56fe\u4e2d\u7684\u8282\u70b9\u8981\u6c42\u4f7f\u752826\u4e2a\u5b57\u6bcd\u6765\u8868\u793a\u3002\u8282\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e3a\u6574\u6570\uff0c\u4e0d\u76f8\u90bb\u8282\u70b9\u8f93\u5165&#8221;I&#8221;\u6216&#8221;i&#8221;\u6765\u4ee3\u8868Infinite\u3002\u8be5\u7a0b\u5e8f\u79fb\u9664\u4e86\u8f93\u5165\u7684\u5197\u4f59\uff0c\u5e76\u4e14\u663e\u793a\u8f93\u5165\u8282\u70b9\u548c\u8ddd\u79bb\u540e\u7684\u90bb\u63a5\u77e9\u9635\u3002\u540c\u65f6\u4f1a\u5728\u6700\u7ec8\u7ed3\u679c\u7ed9\u51fa\u524d\uff0c\u6253\u5370\u51fa Dijkstra\u7b97\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\u8868\u548c\u6700\u77ed\u8def\u5f84\u70b9\u7684\u6b21\u5e8f\uff08path\u6570\u7ec4\uff09\u3002\u5728\u4ee3\u7801\u4e2d\u7ed9\u51fa\u4e86\u8be6\u7ec6\u7684\u6ce8\u91ca\uff0c\u5bf9\u4e8e\u5b66\u4e60Dijkstra\u7b97\u6cd5\u7684\u4eba\u5177\u6709\u4e00\u5b9a\u5e2e\u52a9\u3002<br \/>\n\u8fd0\u884c\u6548\u679c\u56fe\uff1a<br \/>\n<a href=\"https:\/\/www.yesybl.com\/wp-content\/uploads\/2015\/05\/050615_0543_DijkstraC1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-510\" src=\"https:\/\/www.yesybl.com\/wp-content\/uploads\/2015\/05\/050615_0543_DijkstraC1-300x191.png\" alt=\"050615_0543_DijkstraC1.png\" width=\"300\" height=\"191\" srcset=\"https:\/\/www.yesybl.com\/wp-content\/uploads\/2015\/05\/050615_0543_DijkstraC1-300x191.png 300w, https:\/\/www.yesybl.com\/wp-content\/uploads\/2015\/05\/050615_0543_DijkstraC1.png 624w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace CsharpVersion\n{\n\n    class Program\n    {\n        public class DEF\n        {\n            public const int MAX = 26;\n            public const int INF = 999999;\n        }\n        public struct Graph\n        {\n            public char[] vetx;\/\/\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u8282\u70b9\u7684\u540d\u5b57\n            public int vetxNum;\/\/\u62e5\u6709\u8282\u70b9\u7684\u6570\u91cf\n            public int[,] matrix; \/\/\u76f8\u90bb\u77e9\u9635\n        }\n\n        static public Graph genGraph()\n        {\n            string input;\n            int i, j;\n\n            \/\/\u521d\u59cb\u5316\u56fe\u7684\u7ed3\u6784\u4f53\n            Graph map;\n            map.vetx = new char[DEF.MAX];\n            map.vetxNum = 0;\n            map.matrix = new int[DEF.MAX, DEF.MAX];\n\n            Console.Write(&quot;Please enter the number of nodes:&quot;);\n            input = Console.ReadLine();\n            map.vetxNum = Convert.ToInt32(input);\n\n            \/\/\u83b7\u53d6\u6bcf\u4e00\u4e2a\u8282\u70b9\u7684\u5b57\u6bcd\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n            {\n                Console.Write(&quot;Please enter the letter of nodes:&quot;);\n                map.vetx[i] = Convert.ToChar(Console.ReadLine());\n            }\n            \/\/\u521b\u5efa\u90bb\u63a5\u77e9\u9635\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n                for (j = i + 1; j &amp;lt; map.vetxNum; j++) \/\/\u53ea\u9700\u8f93\u5165\u534a\u4e2a\u77e9\u9635\n                {\n                    Console.Write(&quot;Please enter the distance from {0} to {1} (I stands for Infinte):&quot;, map.vetx[i], map.vetx[j]);\n                    input = Console.ReadLine();\n                    if (input.ToLower() == &quot;i&quot;)\n                        map.matrix[i, j] = DEF.INF;\n                    else\n                        map.matrix[i, j] = Convert.ToInt32(input);\n                }\n            \/\/\u586b\u8865\u8282\u70b9\u81ea\u5df1\u5230\u81ea\u5df1\u7684\u8ddd\u79bb = 0\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n                map.matrix[i, i] = 0;\n\n            \/\/\u586b\u8865\u91cd\u590d\u90e8\u5206\u7684\u77e9\u9635\n            for (i = map.vetxNum - 1; i &amp;gt; 0; i--)\n                for (j = i - 1; j &amp;gt;= 0; j--)\n                    map.matrix[i, j] = map.matrix[j, i];\n\n            \/\/debug: \u663e\u793a\u90bb\u63a5\u77e9\u9635\n            Console.WriteLine(&quot;*************START**************&quot;);\n            Console.WriteLine(&quot;The matrix of distance is:&quot;);\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n            {\n                for (j = 0; j &amp;lt; map.vetxNum; j++)\n                {\n                    Console.Write(&quot;{0}&quot;, map.matrix[i, j]);\n                    Console.Write(&quot;\\t&quot;);\n                }\n                Console.Write(&quot;\\r\\n&quot;);\n            }\n            Console.Write(&quot;\\r\\n&quot;);\n            return map;\n        }\n\n        \/\/Dijkstra algirthm\n        static void dijkstra(Graph map, char sNode)\n        {\n            int[,] dist = new int[map.vetxNum, map.vetxNum];\/\/\u8ddd\u79bb\u6570\u7ec4\n            bool[] flag = new bool[map.vetxNum]; \/\/\u6807\u8bb0\u6570\u7ec4\n            int min, tmp, preBaseVal = 0, i, j, preMarkedIndex, vs = 0;\n            int[] path = new int[map.vetxNum]; \/\/\u7528\u4e8e\u5b58\u50a8\u63a8\u6f14\u8868\u4e2d\u6240\u6709\u88ab\u9009\u4e2d\u7684\u4e2d\u7ee7\u70b9\n            Stack pathStack = new Stack();\n\n            \/\/\u627e\u51fa\u8d77\u59cb\u70b9\u7684\u7f16\u53f7\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n                if (map.vetx[i] == sNode)\n                    vs = i;\n            \/\/\u8ddd\u79bb\u6570\u7ec4\u521d\u59cb\u5316\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n            {\n                dist[0, i] = DEF.INF; \/\/ map.matrix[vs, i];\n                flag[i] = false;\/\/\u6240\u6709\u8282\u70b9\u6807\u5fd7\u4f4d\u5747\u8bbe\u4e3a\u672a\u6807\u8bb0\n            }\n\n            \/\/\u521d\u59cb\u5316\u8d77\u59cb\u70b9\u6570\u636e\n            dist[0, vs] = 0;\/\/\u8d77\u59cb\u70b9\u5230\u81ea\u8eab\u7684\u8ddd\u79bb\u4e3a0\n            preMarkedIndex = vs; \n\n            for (i = 0; i &amp;lt; map.vetxNum - 1; i++) \/\/i\u4ee3\u8868\u7b2c\u51e0\u884c\uff0c\n            {\n                \/\/\u5728\u63a8\u6f14\u8868\u4e2d\u7b2c\u4e00\u884c\u627e\u51fa\u6700\u5c0f\u6570\n                min = DEF.INF;\n                for (j = 0; j &amp;lt; map.vetxNum; j++) \/\/j\u4ee3\u8868\u7b2c\u51e0\u5217\n                {\n                    if (min &amp;gt; dist[i, j] &amp;amp;&amp;amp; flag[j] == false)\n                    {\n                        min = dist[i, j];\n                        preMarkedIndex = j;\n                        preBaseVal = min; \/\/\u8bb0\u5f55\u672c\u8f6e\u627e\u5230\u7684\u6700\u5c0f\u503c\n                    }\n                }\n\n                flag[preMarkedIndex] = true;\/\/\u5c06\u627e\u5230\u7684\u8282\u70b9\u6807\u8bb0\n                path[i] = preMarkedIndex;\/\/\u5c06\u627e\u5230\u7684\u8282\u70b9\uff08\u6b21\u5e8f\uff09\u4fdd\u5b58\n\n                \/\/\u628a\u5df2\u6807\u8bb0\u7684\u503c\u590d\u5236\u5230\u63a8\u6f14\u8868\u4e0b\u4e00\u884c\n                for (j = 0; j &amp;lt; map.vetxNum; j++)\n                {\n                    if (flag[j] == true) \/\/\u627e\u5230\u5df2\u6807\u8bb0\u7684\u8282\u70b9\n                        dist[i + 1, j] = dist[i, j]; \/\/\u628a\u4e0a\u4e00\u884c\u4e2d\u5bf9\u5e94\u7684\u5df2\u6807\u8bb0\u503c\u590d\u5236\u5230\u672c\u884c\u4e2d\n                }\n                \/\/\u6c42\u51fa\u5f53\u524d\u884c\u672a\u88ab\u6807\u8bb0\u5217\uff08\u8282\u70b9\uff09\u7684\u8ddd\u79bb\n                for (j = 0; j &amp;lt; map.vetxNum; j++)\n                {\n                    if (flag[j] == false) \/\/\u627e\u51fa\u672a\u6807\u8bb0\u8282\u70b9\n                    {\n                        if (map.matrix[preMarkedIndex, j] == DEF.INF) \/\/\u524d\u9a71\u7ed3\u70b9\u5230\u672c\u5217\u6240\u6307\u8282\u70b9\u4e0d\u53ef\u76f4\u63a5\u5230\u8fbe\uff0c\u590d\u5236\u4e0a\u4e00\u884c\u6570\u636e\n                        {\n                            dist[i + 1, j] = dist[i, j];\n                            continue; \/\/\u7ee7\u7eed\u53bb\u5224\u65ad\u4e0b\u4e00\u4e2a\u8282\u70b9\n                        }\n                        else \/\/\u524d\u9a71\u7ed3\u5230\u5f53\u524d\u5217\u6240\u6307\u8282\u70b9\u53ef\u4ee5\u76f4\u63a5\u5230\u8fbe\u65f6\n                        {\n                            dist[i + 1, j] = (dist[i, j] &amp;lt; (preBaseVal + map.matrix[preMarkedIndex, j]) ? dist[i, j] : (preBaseVal + map.matrix[preMarkedIndex, j]));\n                        }\n                    }\n                }\n            }\n            for (j = 0; j &amp;lt; map.vetxNum; j++) \/\/\u627e\u51fa\u6700\u540e\u4e00\u4e2a\u5269\u4f59\u7684\u8282\u70b9\uff0c\u5e76\u52a0\u5165\u5230\u8def\u5f84\u6570\u7ec4\u4e2dpath[]\n            {\n                if (flag[j] == false) \/\/\u6700\u540e\u5269\u4f59\u7684\u8282\u70b9\u5c1a\u672a\u6807\u8bb0\n                    path[map.vetxNum-1] = j;\n            }\n\n            \/\/\u6253\u5370\u63a8\u6f14\u8868\n            Console.Write(&quot;\\r\\n&quot;);\n            Console.WriteLine(&quot;The calculation table is:&quot;);\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n            {\n                for (j = 0; j &amp;lt; map.vetxNum; j++)\n                {\n                    Console.Write(&quot;{0}&quot;, dist[i, j]);\n                    Console.Write(&quot;\\t&quot;);\n                }\n                Console.Write(&quot;\\r\\n&quot;);\n            }\n\n            \/\/debug: path\u7684\u5185\u5bb9\n            Console.Write(&quot;Path=&quot;);\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n                Console.Write(&quot;{0}&quot;, map.vetx[path[i]]);\n            Console.Write(&quot;\\r\\n\\r\\n&quot;);\n\n            \/\/\u627e\u51fa\u8def\u5f84\u7ed3\u679c\n            for (i = 0; i &amp;lt; map.vetxNum; i++)\n            {\n                tmp = i; \/\/tmp\u4ee3\u8868\u4e86\u7b2c\u51e0\u5217\n                pathStack.Clear();\/\/\u6e05\u7a7a\u8def\u5f84\u6808\n                Console.Write(&quot;The shortest distance from {0} to {1} is {2}: &quot;, map.vetx[vs], map.vetx[i], dist[map.vetxNum - 1, i]);\n\n                \/\/\u627e\u51fa\u4ece\u7ed9\u5b9a\u70b9\u51fa\u53d1\u5230\u8fbe\u6bcf\u4e00\u4e2a\u8282\u70b9\u7684\u8def\u5f84\n                for (j = map.vetxNum - 1; j &amp;gt; 0; j--)\n                {\n                    if (j == map.vetxNum - 1) \/\/j\u4ee3\u8868\u4e86\u7b2c\u51e0\u884c\n                        pathStack.Push(map.vetx[i]); \/\/\u5c06\u76ee\u7684\u5730\u8282\u70b9\u538b\u5165\u8def\u5f84\u6808\n\n                    if (dist[j, tmp] == dist[j - 1, tmp])\n                        continue;\n                    else\n                    {\n                        pathStack.Push(map.vetx[path[j - 1]]);\/\/\u5c06\u524d\u9a71\u7ed3\u70b9\u540d\u538b\u5165\u8def\u5f84\u6808\n                        tmp = path[j - 1]; \/\/\u4e3a\u4e0b\u4e00\u6b21\u5faa\u73af\u5b9a\u4f4d\u5217\uff0c\u79fb\u52a8\u5230\u524d\u9a71\u7ed3\u70b9\u6240\u5728\u7684\u5217\n                    }\n                }\n\n                \/\/\u6253\u5370\u51fa\u8def\u5f84\u6808\u4e2d\u7684\u8def\u5f84\n                foreach (char c in pathStack)\n                {\n                    Console.Write(&quot;-&amp;gt;{0}&quot;, c);\n                }\n                Console.Write(&quot;\\r\\n&quot;);\n            }\n            Console.WriteLine(&quot;***************END***************&quot;);\n        }\n        static void Main(string[] args)\n        {\n            char startPoint;\n            Graph map = genGraph();\n\n            Console.Write(&quot;Please enter which node starts:&quot;);\n            startPoint = Convert.ToChar(Console.Read());\n\n            dijkstra(map, startPoint);\n\n            Console.ReadKey();\n        }\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/www.yesybl.com\/?p=516\" rel=\"bookmark\" title=\"Permalink to Dijkstra\u7b97\u6cd5C#\u6f14\u793a\u7a0b\u5e8f\"><p>\u6700\u8fd1\u5b66\u4e60Dijkstra\u7b97\u6cd5\u548cC#\uff0c\u7528C#\u5199\u4e86\u4e00\u4e2aDijkstra\u7b97\u6cd5\u7684\u6f14\u793a\u7a0b\u5e8f\u3002\u56fe\u4e2d\u7684\u8282\u70b9\u8981\u6c42\u4f7f\u752826\u4e2a\u5b57\u6bcd\u6765\u8868\u793a\u3002\u8282\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u4e3a\u6574\u6570\uff0c\u4e0d\u76f8\u90bb\u8282\u70b9\u8f93\u5165&#8221;I&#8221;\u6216&#8221;i&#8221;\u6765\u4ee3\u8868Infinite\u3002\u8be5\u7a0b\u5e8f\u79fb\u9664\u4e86\u8f93\u5165\u7684\u5197\u4f59\uff0c\u5e76\u4e14\u663e\u793a\u8f93\u5165\u8282\u70b9\u548c\u8ddd\u79bb\u540e\u7684\u90bb\u63a5\u77e9\u9635\u3002\u540c\u65f6\u4f1a\u5728\u6700\u7ec8\u7ed3\u679c\u7ed9\u51fa\u524d\uff0c\u6253\u5370\u51fa Dijkstra\u7b97\u6cd5\u7684\u8ba1\u7b97\u8fc7\u7a0b\u8868\u548c\u6700\u77ed\u8def\u5f84\u70b9\u7684\u6b21\u5e8f\uff08path\u6570\u7ec4\uff09\u3002\u5728\u4ee3\u7801\u4e2d\u7ed9\u51fa\u4e86\u8be6\u7ec6\u7684\u6ce8\u91ca\uff0c\u5bf9\u4e8e\u5b66\u4e60Dijkstra\u7b97\u6cd5\u7684\u4eba\u5177\u6709\u4e00\u5b9a\u5e2e\u52a9\u3002 \u8fd0\u884c\u6548\u679c\u56fe\uff1a<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pa4z28-8k","_links":{"self":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/516"}],"collection":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=516"}],"version-history":[{"count":5,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=\/wp\/v2\/posts\/516\/revisions\/745"}],"wp:attachment":[{"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yesybl.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}