After reading this article to write the small program, Floyd shortest path algorithm, the shortest distance from one point to another point, the middle can pass through any other Points. Three for loop, from I to J in the shortest distance through k, the outermost for loop is through the point k, the internal two loops from I (0) to J (0,1,2,3) through K (0) the shortest distance, from I (1) to J (0,1,2,3) through K (0) the shortest distance, ... From I (3) to J (0,1,2,3) through the shortest distance of K (0), on the basis of K (0) and then through K (2), from I (0) to J (0,1,2,3) through the shortest distance of K (1), from I (1) to J (0,1,2,3) through the shortest distance of K (1),
The program was written according to the above Article.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingsystem.linq;4 usingsystem.text;5 usingSystem.Threading.Tasks;6 7 namespaceFloyd Shortest Path algorithm8 {9 class programTen { one Static voidMain (string[] Args) a { - int[,] e =New int[4,4] { {0,2,6,4 }, -{9999,0,3,9999 }, the{7,9999,0,1 }, -{5,9999, a,0 } }; - - int[,] e1 =New int[4,4] { {0,2,6,4 }, +{9999,0,3,9999 }, -{7,9999,0,1 }, +{5,9999, a,0 } }; a at for(inti =0; I <4; i++) - { - for(intj =0; J <4; J + +) - { -Console.Write (e1[i, j] +" "); - } in Console.WriteLine (); - } to for(intK =0; K <4; K++)//through four locations, respectively . + { - for(inti =0; I <4; I++)//I was the first place the { * for(intj =0; J <4; J + +)//J is the second location . $ {Panax Notoginseng //from I to K,k to J less than I to J - if(e1[i, k] + e1[k, j] <e1[i, j]) the { +e1[i, j] = e1[i, k] +e1[k, j]; a theConsole.WriteLine ("Location { 2} from position {0} to location {1}, original path distance {3}, optimized distance {4}", i+1, j+1, K +1, e[i,j],e1[i,j]); + } - } $ } $ } -Console.WriteLine ("----------------------------------"); - for(inti =0; I <4; i++) the { - for(intj =0; J <4; J + +)Wuyi { theConsole.Write (e1[i, j] +" "); - } wu Console.WriteLine (); - } about Console.readkey (); $ } - } -}
Floyd Shortest Path algorithm