Ejemplo n.º 1
0
    int[] DijkstraFrom(int from)
    {
        var d = new int[V];

        for (var i = 0; i < V; i++)
        {
            d[i] = Inf;
        }
        var queue = new PQ();

        d[from] = 0;
        queue.Enqueue(new P(from, 0));
        while (queue.Count > 0)
        {
            var p = queue.Dequeue();
            int u = p.First, tmp = d[u] + (dead.Contains(u) ? 1 : 0);
            if (d[u] < p.Second)
            {
                continue;
            }
            var k = es[u].Count;
            for (var i = 0; i < k; i++)
            {
                int v = es[u][i];
                if (d[v] > tmp)
                {
                    queue.Enqueue(new P(v, d[v] = tmp));
                }
            }
        }
        return(d);
    }
Ejemplo n.º 2
0
    List <int> Calc()
    {
        var N = G()[0];
        var a = G();
        var r = new int[N];

        for (var i = 0; i < N; i++)
        {
            r[--a[i]] = i;
        }
        var        par = new[] { new RMQ(a, 0), new RMQ(a, 1) };
        var        q   = new PQ();
        var        d   = new Dictionary <int, P>();
        Action <P> add = p => { if (p.r - p.l > 1)
                                {
                                    var z = par[p.l & 1].Min(p); d[z] = p; q.Enqueue(z);
                                }
        };

        add(new P(0, N));
        var L = new List <int>();

        while (q.N > 0)
        {
            int x = q.Dequeue(), i = r[x];
            var p = d[x];
            int f = p.l & 1, y = par[1 - f].Min(i + 1, p.r), j = r[y];
            L.Add(x + 1);
            L.Add(y + 1);
            add(new P(p.l, i));
            add(new P(i + 1, j));
            add(new P(j + 1, p.r));
        }
        return(L);
    }