最短路径:Dijkstra算法 C#
阅读原文时间:2023年07月08日阅读:2
 class Program  
 {  
     const int u = ;

     static void Main(string\[\] args)  
     {  
         Console.WriteLine("各点距离矩阵如下:");  
         Console.WriteLine("   A  B  C  D  E");  
         Console.WriteLine("A  0  2  3  /  /");  
         Console.WriteLine("B  2  0  3  5  2");  
         Console.WriteLine("C  3  3  0  2  4");  
         Console.WriteLine("D  /  5  2  0  1");  
         Console.WriteLine("E  /  2  4  1  0");  
         int\[,\] matrix = new int\[, \] { { , , , u, u }, { , , , ,  }, { , , , ,  }, { u, , , ,  }, { u, , , ,  } };  
         while (true)  
         {  
             Console.WriteLine("请输入要计算的起始点:");  
             string a = Console.ReadLine();  
             int start;  
             switch (a.ToLower())  
             {  
                 default:  
                 case "a":  
                     start = ;  
                     break;  
                 case "b":  
                     start = ;  
                     break;  
                 case "c":  
                     start = ;  
                     break;  
                 case "d":  
                     start = ;  
                     break;  
                 case "e":  
                     start = ;  
                     break;  
             }  
             var list = Dijkstra(matrix, start);

             for (int i = ; i < list.Count; i++)  
             {  
                 Console.WriteLine("从" + a.ToUpper() + "出发到" + i.IndexToChar() + "的最短距离为:" + list\[i\].Distance + ",最短路径为:" + list\[i\].Path);  
             }  
         }  
     }

     public static List<ShortPath> Dijkstra(int\[,\] matrix, int start)  
     {  
         int n = matrix.GetLength();  
         int\[\] visited = new int\[n\];  
         var list = new List<ShortPath>();

         for (int i = ; i < n; i++)  
         {  
             list.Add(new ShortPath() { Index = i, Name = start.IndexToChar() + "->" + i.IndexToChar(), Path = start.IndexToChar() + "->" + i.IndexToChar(), Distance =  });  
         }  
         visited\[start\] = ;  
         for (int i = ; i < n; i++)  
         {  
             int k = -;  
             int dmin = u;  
             for (int j = ; j < n; j++)  
             {  
                 if (visited\[j\] ==  && matrix\[start, j\] < dmin)  
                 {  
                     dmin = matrix\[start, j\];  
                     k = j;  
                 }  
             }

             list\[k\].Distance = dmin;

             visited\[k\] = ;  
             for (int j = ; j < n; j++)  
             {  
                 if (visited\[j\] ==  && matrix\[start, k\] + matrix\[k, j\] < matrix\[start, j\])  
                 {  
                     matrix\[start, j\] = matrix\[start, k\] + matrix\[k, j\];  
                     list\[j\].Path = list\[k\].Path + "->" + j.IndexToChar();  
                 }  
             }  
         }

         return list;  
     }  
 }

 public static class Common  
 {  
     /// <summary>  
     /// 索引转换字母  
     /// </summary>  
     /// <param name="index">当前索引</param>  
     /// <param name="startIndex">起始索引 默认0</param>  
     /// <returns></returns>  
     public static char IndexToChar(this int index, int startIndex = )  
     {  
         return (char)('A' + index - startIndex);  
     }  
 }

 public class ShortPath  
 {  
     private int index;  
     private string name;  
     private string path;  
     private int distance;

     public string Path { get => path; set => path = value; }  
     public int Distance { get => distance; set => distance = value; }  
     public string Name { get => name; set => name = value; }  
     public int Index { get => index; set => index = value; }  
 }