当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.DecreaseKey方法代码示例

本文整理汇总了C#中System.Collections.Generic.PriorityQueue.DecreaseKey方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.DecreaseKey方法的具体用法?C# PriorityQueue.DecreaseKey怎么用?C# PriorityQueue.DecreaseKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections.Generic.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.DecreaseKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DijkstraAlgorithm

        public static List<int> DijkstraAlgorithm(Dictionary<Node,Dictionary<Node,int>> graph, Dictionary<int,Node> nodes, int sourceNode, int destinationNode)
        {
            int[] previous = new int[graph.Count];
            bool[] visited = new bool[graph.Count];
            PriorityQueue<Node> priorityQueue = new PriorityQueue<Node>();
            var startNode = nodes[sourceNode];
            startNode.DistanceFromStart = 0;

            for (int i = 0; i < previous.Length; i++)
            {
                previous[i] = -1;
            }

            priorityQueue.Enqueue(startNode);

            while (priorityQueue.Count > 0)
            {
                var currentNode = priorityQueue.ExtractMin();

                if (currentNode.Index == destinationNode)
                {
                    break;
                }

                foreach (var edge in graph[currentNode])
                {
                    if (!visited[edge.Key.Index])
                    {
                        priorityQueue.Enqueue(edge.Key);
                        visited[edge.Key.Index] = true;
                    }

                    var distance = currentNode.DistanceFromStart + edge.Value;
                    if (distance < edge.Key.DistanceFromStart)
                    {
                        edge.Key.DistanceFromStart = distance;
                        previous[edge.Key.Index] = currentNode.Index;
                        priorityQueue.DecreaseKey(edge.Key);
                    }
                }
            }

            if (previous[destinationNode] == -1)
            {
                return null;
            }

            List<int> path = new List<int>();
            int current = destinationNode;
            while (current != -1)
            {
                path.Add(current);
                current = previous[current];
            }

            path.Reverse();
            return path;
        }
开发者ID:vangelov-i,项目名称:Fundamentals,代码行数:58,代码来源:DijkstraWithPriorityQueue.cs

示例2: Main

        public static void Main()
        {
            int rows = int.Parse(Console.ReadLine());
            int cols = int.Parse(Console.ReadLine());
            var matrix = new Cell[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                int[] currentRow = Console.ReadLine().Split().Select(int.Parse).ToArray();
                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = new Cell(i, j, currentRow[j]);
                }
            }

            matrix[0, 0].DijkstraDistance = 0;
            var queue = new PriorityQueue<Cell>();
            queue.Enqueue(matrix[0, 0]);
            while (queue.Count > 0)
            {
                var cell = queue.ExtractMin();
                int row = cell.Row;
                int col = cell.Col;

                if (IsValidCell(row, col + 1, rows, cols))
                {
                    var rightCell = matrix[row, col + 1];
                    if (rightCell.DijkstraDistance > cell.DijkstraDistance + rightCell.Value)
                    {
                        rightCell.DijkstraDistance = cell.DijkstraDistance + rightCell.Value;
                        rightCell.PreviousCell = cell;
                        if (queue.Contain(rightCell))
                        {
                            queue.DecreaseKey(rightCell);
                        }
                    }

                     if(!rightCell.IsVisited)
                    {
                        queue.Enqueue(rightCell);
                        rightCell.IsVisited = true;
                    }
                }

                if (IsValidCell(row + 1, col, rows, cols))
                {
                    var bottomCell = matrix[row + 1, col];
                    if (bottomCell.DijkstraDistance > cell.DijkstraDistance + bottomCell.Value)
                    {
                        bottomCell.DijkstraDistance = cell.DijkstraDistance + bottomCell.Value;
                        bottomCell.PreviousCell = cell;
                        if (queue.Contain(bottomCell))
                        {
                            queue.DecreaseKey(bottomCell);
                        }
                    }

                    if(!bottomCell.IsVisited)
                    {
                        queue.Enqueue(bottomCell);
                        bottomCell.IsVisited = true;
                    }
                }

                if (IsValidCell(row, col - 1, rows, cols))
                {
                    var leftCell = matrix[row, col - 1];
                    if (leftCell.DijkstraDistance > cell.DijkstraDistance + leftCell.Value)
                    {
                        leftCell.DijkstraDistance = cell.DijkstraDistance + leftCell.Value;
                        leftCell.PreviousCell = cell;
                        if (queue.Contain(leftCell))
                        {
                            queue.DecreaseKey(leftCell);
                        }
                    }

                    if(!leftCell.IsVisited)
                    {
                        queue.Enqueue(leftCell);
                        leftCell.IsVisited = true;
                    }
                }

                if (cell.Row == rows - 1 && cell.Col == cols -1)
                {
                    break;
                }
            }

            var lastCell = matrix[rows - 1, cols - 1];
            var path = new Stack<int>();
            while (lastCell != null)
            {
                path.Push(lastCell.Value);
                lastCell = lastCell.PreviousCell;
            }

            Console.WriteLine("Length: " + path.Sum());
            Console.WriteLine("Path: " + string.Join(" ", path));
        }
开发者ID:pavelilchev,项目名称:HomeWorks,代码行数:100,代码来源:ShortestPathInMatrix.cs


注:本文中的System.Collections.Generic.PriorityQueue.DecreaseKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。