Copy codeThe Code is as follows: 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, 50, noPath, noPath },
{NoPath, noPath, 10 },
{NoPath, 20, noPath, 60 },
{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 ("point 0 to point 5 path :");
For (int I = 0; I <path1.Length; I ++)
Console. Write (path1 [I]. ToString () + "");
Console. WriteLine ("Length:" + dist1 );
Console. WriteLine ("\ r \ n ------------------------------------------- \ r \ n ");
Int [] pathdist = getShortedPath (G, 0, path2 );
Console. WriteLine ("path from point 0 to any point :");
For (int j = 0; j <pathdist. Length; j ++)
{
Console. WriteLine ("point 0 to" + j + "Path :");
For (int I = 0; I <length; I ++)
Console. Write (path2 [j, I]. ToString () + "");
Console. WriteLine ("Length:" + pathdist [j]);
}
Console. ReadKey ();
}
// Find the shortest path of a node from a source point
Static int getShortedPath (int [,] G, int start, int end, int [] path)
{
Bool [] s = new bool [length]; // indicates finding the shortest path between the Start Node and the current node
Int min; // temporary variable with minimum distance
Int curNode = 0; // temporary node, which records the current positive computing node
Int [] dist = new int [length];
Int [] prev = new int [length];
// Initial node Information
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;
// Main Loop
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;
}
}
// Output path Node
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];
}
// Find the shortest path of all nodes from a source point
Static int [] getShortedPath (int [,] G, int start, int [,] path)
{
Int [] PathID = new int [length]; // path (represented by number)
Bool [] s = new bool [length]; // indicates finding the shortest path between the Start Node and the current node
Int min; // temporary variable with minimum distance
Int curNode = 0; // temporary node, which records the current positive computing node
Int [] dist = new int [length];
Int [] prev = new int [length];
// Initial node Information
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;
// Main Loop
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;
}
}
// Output path Node
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;
}
}
}