資源簡介
Dijkstra(迪杰斯特拉)算法是典型的單源最短路徑算法,用于計算一個節點到其他所有節點的最短路徑。主要特點是以起始點為中心向外層層擴展,直到擴展到終點為止。本實例實現了求最小路徑的權值還能繪出最小路徑的走法;

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?最短路徑
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????Point[]?S;
????????Graphics?g;
????????int[]?t;
????????int?n?=?0;
????????List?W;
????????private?void?pictureBox1_MouseDown(object?sender?MouseEventArgs?e)
????????{?????????
????????}
????????///?
????????///?最小值
????????///?
????????///?
????????///?
????????///?
????????private?int?MIN(int[]?Q?out?int?j)
????????{???????????
????????????int?a=10000;
????????????j?=?0;
????????????for?(int?i?=?0;?i? ????????????{????????????????????????????????????????????
????????????????if?(a?>=Q[i])
????????????????{
????????????????????????a?=?Q[i];
????????????????????????j?=?i;
????????????????}??????????????
????????????}
????????????return?a;
????????}
????????///?
????????///?最短路徑
????????///?
????????///???????
????????///?
????????///?
????????private?void?shortpan(int[]?tint?vout?int[]?d)
????????{
????????????int[]?h;
????????????int[]?Q;
????????????h?=?new?int[t.GetLength(0)?t.GetLength(0)];??????????
????????????d?=?new?int[t.GetLength(0)];
????????????Q?=?new?int[t.GetLength(0)];??????????
????????????d[v]?=?0;//源點到源點為0;
????????????int?u?=?v;//記錄移除的節點????????????????????
????????????int?w?=?v;//記錄前一個節點
????????????int?max?=?10000;?
????????????//判斷是否存在邊,初始化Q
????????????for?(int?i?=?0;?i?????????????{
????????????????for?(int?j?=?0;?j?????????????????{
??????????????????if(t[ij] ??????????????????{
??????????????????????h[i?j]?=?1;?????????????????????
??????????????????}
????????????????}
???????????????Q[i]?=?max;
????????????}
????????????//更新最短路徑
????????????for?(int?j?=?1;?j?????????????{
????????????????int?l?=?0;
????????????????for?(int?i?=?0;?i? ????????????????{
????????????????????if?(h[u?i]?==?1)
????????????????????{
????????????????????????if?(t[u?i]?+?d[u]?<=?Q[i])
????????????????????????{
????????????????????????????g.DrawLine(new?Pen(Brushes.Red?2)?S[u]?S[i]);
????????????????????????????System.Threading.Thread.Sleep(500);
????????????????????????????if?(Q[i]!=?max&&h[wi]!=0)???????????????????????????
????????????????????????????????g.DrawLine(new?Pen(Brushes.Yellow?2)?S[w]?S[i]);
????????????????????????????Q[i]?=?t[u?i]?+?d[u];?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????187??2015-11-23?22:24??最短路徑\最短路徑\App.config
?????文件??????16384??2015-11-28?10:24??最短路徑\最短路徑\bin\Debug\最短路徑.exe
?????文件????????187??2015-11-23?22:24??最短路徑\最短路徑\bin\Debug\最短路徑.exe.config
?????文件??????28160??2015-11-28?10:24??最短路徑\最短路徑\bin\Debug\最短路徑.pdb
?????文件??????24224??2015-11-28?10:25??最短路徑\最短路徑\bin\Debug\最短路徑.vshost.exe
?????文件????????187??2015-11-23?22:24??最短路徑\最短路徑\bin\Debug\最短路徑.vshost.exe.config
?????文件????????490??2015-07-10?19:01??最短路徑\最短路徑\bin\Debug\最短路徑.vshost.exe.manifest
?????文件???????7951??2015-11-28?10:44??最短路徑\最短路徑\Form1.cs
?????文件???????6488??2015-11-28?09:57??最短路徑\最短路徑\Form1.Designer.cs
?????文件???????8391??2015-11-28?09:57??最短路徑\最短路徑\Form1.resx
?????文件???????1453??2015-11-24?08:21??最短路徑\最短路徑\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7031??2015-11-23?22:25??最短路徑\最短路徑\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2015-11-23?22:25??最短路徑\最短路徑\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2015-11-23?22:25??最短路徑\最短路徑\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2015-11-23?22:25??最短路徑\最短路徑\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件????????752??2015-11-28?10:25??最短路徑\最短路徑\obj\Debug\最短路徑.csproj.FileListAbsolute.txt
?????文件????????977??2015-11-28?10:10??最短路徑\最短路徑\obj\Debug\最短路徑.csproj.GenerateResource.Cache
?????文件??????16384??2015-11-28?10:24??最短路徑\最短路徑\obj\Debug\最短路徑.exe
?????文件???????1942??2015-11-28?10:10??最短路徑\最短路徑\obj\Debug\最短路徑.Form1.resources
?????文件??????28160??2015-11-28?10:24??最短路徑\最短路徑\obj\Debug\最短路徑.pdb
?????文件????????180??2015-11-28?10:10??最短路徑\最短路徑\obj\Debug\最短路徑.Properties.Resources.resources
?????文件????????524??2015-11-23?22:25??最短路徑\最短路徑\Program.cs
?????文件???????1348??2015-11-23?22:25??最短路徑\最短路徑\Properties\AssemblyInfo.cs
?????文件???????2876??2015-11-23?22:25??最短路徑\最短路徑\Properties\Resources.Designer.cs
?????文件???????5612??2015-11-23?22:24??最短路徑\最短路徑\Properties\Resources.resx
?????文件???????1099??2015-11-23?22:25??最短路徑\最短路徑\Properties\Settings.Designer.cs
?????文件????????249??2015-11-23?22:24??最短路徑\最短路徑\Properties\Settings.settings
?????文件???????3804??2015-11-23?22:30??最短路徑\最短路徑\最短路徑.csproj
?????文件???????1005??2015-11-23?22:25??最短路徑\最短路徑.sln
????..A..H.?????34304??2015-11-28?10:44??最短路徑\最短路徑.v12.suo
............此處省略11個文件信息
評論
共有 條評論