Dijkstra算法(单源最短路径)C#版

class Program
    {
        struct MGraph
        {
            public int[,] matrix;
            public int n;
            public int e;
        }

        const int INT_MAX = int.MaxValue;

        static void DijkstraPath(MGraph g, int[] dist, int[] path, int v0)   //v0表示源顶点 
        {
            int i, j, k;
            bool[] visited = new bool[g.n];
            for (i = 0; i < g.n; i++)     //初始化
            {
                if (g.matrix[v0,i] > 0 && i != v0)
                {
                    dist[i] = g.matrix[v0,i];
                    path[i] = v0;     //path记录最短路径上从v0到i的前一个顶点
                }
                else
                {
                    dist[i] = INT_MAX;    //若i不与v0直接相邻,则权值置为无穷大
                    path[i] = -1;
                }
                visited[i] = false;
                path[v0] = v0;
                dist[v0] = 0;
            }
            visited[v0] = true;
            for (i = 1; i < g.n; i++)     //循环扩展n-1次
            {
                int min = INT_MAX;
                int u = -1;
                for (j = 0; j < g.n; j++)    //寻找未被扩展的权值最小的顶点
                {
                    if (visited[j] == false && dist[j] < min)
                    {
                        min = dist[j];
                        u = j;
                    }
                }
                visited[u] = true;
                for (k = 0; k < g.n; k++)   //更新dist数组的值和路径的值
                {
                    if (visited[k] == false && g.matrix[u,k] > 0 && min + g.matrix[u,k] < dist[k])
                    {
                        dist[k] = min + g.matrix[u,k];
                        path[k] = u;
                    }
                }
            }
        }

        static void showPath(int[] path, int v, int v0)   //打印最短路径上的各个顶点
        {
            Stack<int> s = new Stack<int>();
            int u = v;
            while (v != v0)
            {
                s.Push(v);
                v = path[v];
            }
            s.Push(v);
            while (s.Count > 0)
            {
                Console.WriteLine(s.Peek());
                s.Pop();
            }
        }

        static void Main(string[] args)
        {
            int n, e;     //表示输入的顶点数和边数
            Console.Write("n: ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write("e: ");
            e = Convert.ToInt32(Console.ReadLine());
            int i, j;
            int s, t, w;      //表示存在一条边s->t,权值为w
            MGraph g = new MGraph();
            g.matrix = new int[100,100];
            int v0;
            int[] dist = new int[n];
            int[] path = new int[n];
            for (i = 0; i < 100; i++)
                for (j = 0; j < 100; j++)
                    g.matrix[i,j] = 0;
            g.n = n;
            g.e = e;
            for (i = 0; i < e; i++)
            {
                Console.Write("s: ");
                s = Convert.ToInt32(Console.ReadLine());
                Console.Write("t: ");
                t = Convert.ToInt32(Console.ReadLine());
                Console.Write("w: ");
                w = Convert.ToInt32(Console.ReadLine());
                g.matrix[s,t] = w;
            }
            Console.Write("v0: ");
            v0 = Convert.ToInt32(Console.ReadLine());
            DijkstraPath(g, dist, path, v0);
            for (i = 0; i < n; i++)
            {
                if (i != v0)
                {
                    showPath(path, i, v0);
                    Console.WriteLine(dist[i]);
                }
            }
            Console.ReadLine();
        }
    }

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/zuig2/article/details/52037525
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞