本文整理汇总了C#中System.Collections.Generic.PriorityQueue.DequeueWithPriority方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.DequeueWithPriority方法的具体用法?C# PriorityQueue.DequeueWithPriority怎么用?C# PriorityQueue.DequeueWithPriority使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Generic.PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue.DequeueWithPriority方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTreeWeight
// a standard implementation of Dijkstra's algorithm using a priority queue
// with a minor modification for the problem
static int GetTreeWeight(Graph graph, int hospital, int verticeCount, IEnumerable<int> hospitals)
{
var distances = new PriorityQueue<int, int>((e1, e2) => -e1.CompareTo(e2));
foreach (var adj in graph[hospital])
{
distances.Enqueue(adj.Item2, adj.Item1);
}
// return value
int ret = 0;
int edgeCount = 0;
while (edgeCount < verticeCount - hospitals.Count())
{
edgeCount += 1;
// edge nearest to 'hospital'
var min = distances.DequeueWithPriority();
var weight = min.Item1;
// the new vertex in the tree
var v1 = min.Item2;
// modification of algorithm:
// sum the distance to the root of all nodes that aren't
// hospitals
if (!hospitals.Contains(v1))
ret += weight;
// update the priorities of all edges incident on the vertex
// we've just added
foreach (var adj in graph[v1])
{
var v2 = adj.Item1;
if (v2 == hospital)
continue;
var priority = distances.PriorityOrDefault(v2, int.MaxValue);
if (priority > weight + adj.Item2)
distances.Rekey(v2, weight + adj.Item2);
}
}
return ret;
}