Exemplo n.º 1
0
    int[] DijkstraFrom(int from)
    {
        var d     = Enumerable.Repeat(MOD, V).ToArray();
        var queue = new DialQueue(C);

        d[from] = 0;
        queue.Add(from, 0);
        while (queue.Any)
        {
            var p = queue.Pop();
            var v = p.To;
            if (d[v] < p.Cost)
            {
                continue;
            }
            foreach (var e in es[v])
            {
                var tmp = d[v] + e.Cost;
                if (d[e.To] > tmp)
                {
                    if (d[e.To] != MOD)
                    {
                        queue.Move(e.To, d[e.To], tmp);
                    }
                    else
                    {
                        queue.Add(e.To, tmp);
                    }
                    d[e.To] = tmp;
                }
            }
        }
        return(d);
    }
Exemplo n.º 2
0
    int[] DijkstraFrom(int from)
    {
        var d = new int[V];

        for (var i = 0; i < V; i++)
        {
            d[i] = INF;
        }
        var queue = new DialQueue(C);

        d[from] = 0;
        queue.Add(from, 0);
        var n   = 0;
        var ret = new int[V];

        while (true)
        {
            var p = queue.Pop();
            var v = p.To;
            if (d[v] < p.Cost)
            {
                continue;
            }
            ret[n++] = d[v];
            if (n == V)
            {
                return(ret);
            }
            foreach (var e in es[v])
            {
                var tmp = d[v] + e.Cost;
                if (d[e.To] > tmp)
                {
                    if (d[e.To] != INF)
                    {
                        queue.Move(e.To, d[e.To], tmp);
                    }
                    else
                    {
                        queue.Add(e.To, tmp);
                    }
                    d[e.To] = tmp;
                }
            }
        }
    }