小编van*_*zen的帖子

SortedSet.Remove()不会删除任何内容

我目前正在实现Dijkstra的算法,我使用C#SortedSet作为优先级队列.但是,为了跟踪我已经访问过的顶点,我想从优先级队列中删除第一个项目.

这是我的代码:

static int shortestPath(int start, int target)
    {
        SortedSet<int> PQ = new SortedSet<int>(new compareByVertEstimate());
        for (int i = 0; i < n; i++)
        {
            if (i == start - 1)
                vertices[i].estimate = 0;
            else
                vertices[i].estimate = int.MaxValue;

            PQ.Add(i);
        }

        int noOfVisited = 0;
        while (noOfVisited < n)
        {
            int u = PQ.First();
            noOfVisited++;

            foreach (Edge e in vertices[u].neighbours)
            {
                if (vertices[e.target.Item1].estimate > vertices[u].estimate + e.length)
                {
                    vertices[e.target.Item1].estimate = vertices[u].estimate + e.length;
                }
            }

            PQ.Remove(u);
        }
        return vertices[target - 1].estimate; …
Run Code Online (Sandbox Code Playgroud)

c# graph dijkstra sortedset

2
推荐指数
1
解决办法
1271
查看次数

标签 统计

c# ×1

dijkstra ×1

graph ×1

sortedset ×1