欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)的最短路徑分析

 更新時間:2013年03月19日 16:39:16   作者:  
C#實(shí)現(xiàn)的最短路徑分析,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:

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("點(diǎn)0到點(diǎn)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("點(diǎn)0到任意點(diǎn)的路徑:");
             for (int j = 0; j < pathdist.Length; j++)
             {
                 Console.WriteLine("點(diǎn)0到" + j + "的路徑:");
                 for (int i = 0; i < length; i++)
                     Console.Write(path2[j, i].ToString() + " ");
                 Console.WriteLine("長度:" + pathdist[j]);
             }
             Console.ReadKey();

         }

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

             //初始結(jié)點(diǎn)信息
             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;
             //主循環(huán)
             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;
                     }

             }
             //輸出路徑結(jié)點(diǎn)
             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];
         }

 

 

 
         //從某一源點(diǎn)出發(fā),找到到所有結(jié)點(diǎn)的最短路徑
         static int[] getShortedPath(int[,] G, int start, int[,] path)
         {
             int[] PathID = new int[length];//路徑(用編號表示)
             bool[] s = new bool[length]; //表示找到起始結(jié)點(diǎn)與當(dāng)前結(jié)點(diǎn)間的最短路徑
             int min;  //最小距離臨時變量
             int curNode = 0; //臨時結(jié)點(diǎn),記錄當(dāng)前正計算結(jié)點(diǎn)
             int[] dist = new int[length];
             int[] prev = new int[length];
             //初始結(jié)點(diǎn)信息
             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;
             //主循環(huán)
             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;
                     }

 
             }
             //輸出路徑結(jié)點(diǎn)
             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;

         }

 
     }
 }

相關(guān)文章

  • C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義

    C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義

    ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時會顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過派生ProfessionalColorTable類,下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下
    2015-08-08
  • unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn)

    unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C#單例模式與多線程用法介紹

    C#單例模式與多線程用法介紹

    這篇文章介紹了C#單例模式與多線程的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#計時器的三種實(shí)現(xiàn)方法

    C#計時器的三種實(shí)現(xiàn)方法

    這篇文章主要介紹了C#計時器的三種實(shí)現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • C# 腳本引擎RulesEngine的使用詳解

    C# 腳本引擎RulesEngine的使用詳解

    這篇文章主要介紹了C# 腳本引擎RulesEngine的使用方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-02-02
  • C#檢查鍵盤大小寫鎖定狀態(tài)的方法

    C#檢查鍵盤大小寫鎖定狀態(tài)的方法

    這篇文章主要介紹了C#檢查鍵盤大小寫鎖定狀態(tài)的方法,涉及C#鍵盤操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式

    C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式

    這篇文章介紹了C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容

    C#使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容

    HtmlAgilityPack是一個HTML解析類庫,這篇文章主要為大家詳細(xì)介紹了C#如何使用HtmlAgilityPack實(shí)現(xiàn)解析提取HTML內(nèi)容,感興趣的小伙伴可以參考一下
    2023-12-12
  • C# 郵箱mail 發(fā)送類

    C# 郵箱mail 發(fā)送類

    此類的功能包括發(fā)送郵件,郵箱格式是否正確,和在不發(fā)送郵件的情況下判斷郵箱用戶名和密碼是否正確,鑒于POP檢查郵箱用戶名和密碼出現(xiàn)錯誤情況返回結(jié)果的延遲,用異步線程解決此問題,見代碼
    2015-06-06
  • 基于Json序列化和反序列化通用的封裝完整代碼

    基于Json序列化和反序列化通用的封裝完整代碼

    JSON 是存儲和交換文本信息的語法。類似 XML。JSON 比 XML 更小、更快,更易解析。下面通過實(shí)例代碼給大家分享Json序列化和反序列化通用的封裝,需要的的朋友參考下吧
    2017-07-07

最新評論