麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C#實現的最短路徑分析

2020-01-24 03:32:07
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication1
 {
     class Program
     {
         static int length = 6;
         static string[] shortedPath = new string[length];
         static int noPath = 2000;
         static int MaxSize = 1000;
         static int[,] G =
         {
             { noPath, noPath, 10, noPath, 30, 100 },
             { noPath, noPath, 5, noPath, noPath, noPath },
             { noPath, noPath, noPath, 50, noPath, noPath },
             { noPath, noPath, noPath, noPath, noPath, 10 },
             { noPath, noPath, noPath, 20, noPath, 60 },
             { noPath, noPath, noPath, noPath, noPath, noPath }
         };
         static string[] PathResult = new string[length];

         static int[] path1 = new int[length];
         static int[,] path2 = new int[length, length];
         static int[] distance2 = new int[length];

         static void Main(string[] args)
         {
             int dist1 = getShortedPath(G, 0, 1, path1);
             Console.WriteLine("點0到點5路徑:");
             for (int i = 0; i < path1.Length; i++)
                 Console.Write(path1[i].ToString() + " "); 
             Console.WriteLine("長度:" + dist1);

 
             Console.WriteLine("/r/n-----------------------------------------/r/n");

             int[] pathdist = getShortedPath(G, 0, path2);
             Console.WriteLine("點0到任意點的路徑:");
             for (int j = 0; j < pathdist.Length; j++)
             {
                 Console.WriteLine("點0到" + j + "的路徑:");
                 for (int i = 0; i < length; i++)
                     Console.Write(path2[j, i].ToString() + " ");
                 Console.WriteLine("長度:" + pathdist[j]);
             }
             Console.ReadKey();

         }

 
         //從某一源點出發,找到到某一結點的最短路徑
         static int getShortedPath(int[,]G, int start, int end,int [] path)
         {
             bool[] s = new bool[length]; //表示找到起始結點與當前結點間的最短路徑
             int min;  //最小距離臨時變量
             int curNode=0; //臨時結點,記錄當前正計算結點
             int[] dist = new int[length];
             int[] prev = new int[length];

             //初始結點信息
             for (int v = 0; v < length; v++)
             {
                 s[v] = false;
                 dist[v] = G[start, v];
                 if (dist[v] > MaxSize)
                     prev[v] = 0;
                 else
                     prev[v] = start;
             }
             path[0] = end;
             dist[start] = 0;
             s[start] = true;
             //主循環
             for (int i = 1; i < length; i++)
             {
                 min = MaxSize;
                 for (int w = 0; w < length; w++)
                 {
                     if (!s[w] && dist[w] < min)
                     {
                         curNode = w;
                         min = dist[w];
                     }
                 }

                 s[curNode] = true;
                 for (int j = 0; j < length; j++)
                     if (!s[j] && min + G[curNode, j] < dist[j])
                     {
                         dist[j] = min + G[curNode, j];
                         prev[j] = curNode;
                     }

             }
             //輸出路徑結點
             int e = end, step = 0;
             while (e != start)
             {
                 step++;
                 path[step] = prev[e];
                 e = prev[e];
             }
             for (int i = step; i > step/2; i--)
             {
                 int temp = path[step - i];
                 path[step - i] = path[i];
                 path[i] = temp;
             }
             return dist[end];
         }

 

 

 
         //從某一源點出發,找到到所有結點的最短路徑
         static int[] getShortedPath(int[,] G, int start, int[,] path)
         {
             int[] PathID = new int[length];//路徑(用編號表示)
             bool[] s = new bool[length]; //表示找到起始結點與當前結點間的最短路徑
             int min;  //最小距離臨時變量
             int curNode = 0; //臨時結點,記錄當前正計算結點
             int[] dist = new int[length];
             int[] prev = new int[length];
             //初始結點信息
             for (int v = 0; v < length; v++)
             {
                 s[v] = false;
                 dist[v] = G[start, v];
                 if (dist[v] > MaxSize)
                     prev[v] = 0;
                 else
                     prev[v] = start;
                 path[v,0] = v;
             }

             dist[start] = 0;
             s[start] = true;
             //主循環
             for (int i = 1; i < length; i++)
             {
                 min = MaxSize;
                 for (int w = 0; w < length; w++)
                 {
                     if (!s[w] && dist[w] < min)
                     {
                         curNode = w;
                         min = dist[w];
                     }
                 }

                 s[curNode] = true;

                 for (int j = 0; j < length; j++)
                     if (!s[j] && min + G[curNode, j] < dist[j])
                     {
                         dist[j] = min + G[curNode, j];
                         prev[j] = curNode;
                     }

 
             }
             //輸出路徑結點
             for (int k = 0; k < length; k++)
             {
                 int e = k, step = 0;
                 while (e != start)
                 {
                     step++;
                     path[k, step] = prev[e];
                     e = prev[e];
                 }
                 for (int i = step; i > step / 2; i--)
                 {
                     int temp = path[k, step - i];
                     path[k, step - i] = path[k, i];
                     path[k, i] = temp;
                 }
             }
             return dist;

         }

 
     }
 }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国内xxxx乱子另类 | 色视频在线 | 草草久| 一级片999 | 一边吃奶一边摸下娇喘 | 国产精品1区,2区,3区 | 久久草在线视频免费 | 免费看搡女人无遮挡的视频 | 久久精品欧美电影 | 精品国产91久久久久久浪潮蜜月 | 国产精品伦视频看免费三 | 欧洲伊人网| 欧美日韩国产一区二区三区在线观看 | 精品国产一区二区久久 | 久久逼逼 | 91精品国产乱码久久桃 | 欧美18一12sex性处hd | 亚洲第一页在线观看 | 成人啪啪色婷婷久 | 精品一区二区三区网站 | 久久久中| 毛片视频免费观看 | 日韩欧美电影一区二区三区 | 国产va在线观看免费 | 亚洲小视频在线 | 九九热精 | 国产伦精品一区二区三区 | 欧美大穴 | 污视频在线免费播放 | 日本在线精品视频 | 国产午夜免费福利 | 午夜视频在线免费观看 | 久久久成人动漫 | 九色中文字幕 | 免费看真人a一级毛片 | 在线看免电影网站 | 免费观看黄色一级视频 | 久久久久久久久久久影视 | 91精品国产综合久久青草 | 欧美日性 | 国产又粗又爽又深的免费视频 |