Esempio n. 1
0
    static (long[] d, int[] from) Dijkstra(int n, int sv, int[][] es, bool directed = false)
    {
        // 使われない頂点が存在してもかまいません (1-indexed でも可)。
        var map = ToMap2(n, es, directed);

        var d    = Enumerable.Repeat(long.MaxValue, n + 1).ToArray();
        var from = Enumerable.Repeat(-1, n + 1).ToArray();
        var q    = PQ <int> .CreateWithKey(v => d[v]);

        d[sv] = 0;
        q.Push(sv);

        while (q.Count > 0)
        {
            var(v, qd) = q.Pop();
            if (d[v] < qd)
            {
                continue;
            }
            foreach (var e in map[v])
            {
                if (d[e.To] <= d[v] + e.Weight)
                {
                    continue;
                }
                d[e.To]    = d[v] + e.Weight;
                from[e.To] = v;
                q.Push(e.To);
            }
        }
        return(d, from);
    }
Esempio n. 2
0
    public static long[] Dijkstra(int n, Func <int, int[][]> nexts, int sv, int ev = -1)
    {
        var costs = Array.ConvertAll(new bool[n], _ => long.MaxValue);
        var q     = PQ <int> .CreateWithKey(v => costs[v]);

        costs[sv] = 0;
        q.Push(sv);

        while (q.Count > 0)
        {
            var(c, v) = q.Pop();
            if (v == ev)
            {
                break;
            }
            if (costs[v] < c)
            {
                continue;
            }

            foreach (var e in nexts(v))
            {
                var(nv, nc) = (e[1], c + e[2]);
                if (costs[nv] <= nc)
                {
                    continue;
                }
                costs[nv] = nc;
                q.Push(nv);
            }
        }
        return(costs);
    }
Esempio n. 3
0
    static object Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var a = Read();

        var amax = a.Max();

        if (a.All(x => x >= (amax + 1) / 2))
        {
            return(string.Join(" ", Enumerable.Repeat(-1, n)));
        }

        var a2 = a.Concat(a).Concat(a).ToArray();
        var q  = PQ <int> .CreateWithKey(i => a2[i], true);

        var end = new int[a2.Length];

        for (int i = 0; i < a2.Length; i++)
        {
            while (q.Any() && (q.First.Key + 1) / 2 > a2[i])
            {
                var i0 = q.Pop().Value;
                for (int j = i0; j >= 0 && end[j] == 0; j--)
                {
                    end[j] = i;
                }
            }
            q.Push(i);
        }

        return(string.Join(" ", end.Take(n).Select((x, i) => x - i)));
    }
Esempio n. 4
0
    static long[] Dijkstra(int n, int[][][] map, int sv, int ev = -1)
    {
        var cs = Array.ConvertAll(new bool[n], _ => long.MaxValue);
        var q  = PQ <int> .CreateWithKey(v => cs[v]);

        cs[sv] = 0;
        q.Push(sv);

        while (q.Count > 0)
        {
            var vc = q.Pop();
            var v  = vc.Value;
            if (v == ev)
            {
                break;
            }
            if (cs[v] < vc.Key)
            {
                continue;
            }

            foreach (var e in map[v])
            {
                if (cs[e[0]] <= cs[v] + e[1])
                {
                    continue;
                }
                cs[e[0]] = cs[v] + e[1];
                q.Push(e[0]);
            }
        }
        return(cs);
    }
Esempio n. 5
0
    static Tuple <long[], int[][]> Dijkstra(int n, int[][] es, bool directed, int sv, int ev = -1)
    {
        var map = Array.ConvertAll(new bool[n], _ => new List <int[]>());

        foreach (var e in es)
        {
            map[e[0]].Add(new[] { e[0], e[1], e[2] });
            if (!directed)
            {
                map[e[1]].Add(new[] { e[1], e[0], e[2] });
            }
        }

        var cs      = Array.ConvertAll(new bool[n], _ => long.MaxValue);
        var inEdges = new int[n][];
        var q       = PQ <int> .CreateWithKey(v => cs[v]);

        cs[sv] = 0;
        q.Push(sv);

        while (q.Count > 0)
        {
            var vc = q.Pop();
            var v  = vc.Value;
            if (v == ev)
            {
                break;
            }
            if (cs[v] < vc.Key)
            {
                continue;
            }

            foreach (var e in map[v])
            {
                if (cs[e[1]] <= cs[v] + e[2])
                {
                    continue;
                }
                cs[e[1]]      = cs[v] + e[2];
                inEdges[e[1]] = e;
                q.Push(e[1]);
            }
        }
        return(Tuple.Create(cs, inEdges));
    }
Esempio n. 6
0
    static string Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var s = Array.ConvertAll(new bool[n], _ => Console.ReadLine().ToCharArray());

        // WA
        var t        = NewArray2 <int>(n, n);
        var triplets = new RectGridMap <List <PB> >(n, n, () => new List <PB>());

        // Horizontal
        for (int i = 0; i < n; i++)
        {
            for (int j = 1; j < n - 1; j++)
            {
                if (s[i][j - 1] == X && s[i][j] == X && s[i][j + 1] == X)
                {
                    t[i][j - 1]++;
                    t[i][j]++;
                    t[i][j + 1]++;

                    var pb = new PB {
                        p = new Point(i, j)
                    };
                    triplets[i, j - 1].Add(pb);
                    triplets[i, j].Add(pb);
                    triplets[i, j + 1].Add(pb);
                }
            }
        }
        // Vertical
        for (int j = 0; j < n; j++)
        {
            for (int i = 1; i < n - 1; i++)
            {
                if (s[i - 1][j] == X && s[i][j] == X && s[i + 1][j] == X)
                {
                    t[i - 1][j]++;
                    t[i][j]++;
                    t[i + 1][j]++;

                    var pb = new PB {
                        p = new Point(i, j), vertical = true
                    };
                    triplets[i - 1, j].Add(pb);
                    triplets[i, j].Add(pb);
                    triplets[i + 1, j].Add(pb);
                }
            }
        }

        var q = PQ <Point> .CreateWithKey(p => t[p.i][p.j], true);

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (t[i][j] > 0)
                {
                    q.Push(new Point(i, j));
                }
            }
        }

        while (q.Count > 0)
        {
            var(c, p) = q.Pop();
            if (t[p.i][p.j] != c)
            {
                continue;
            }

            s[p.i][p.j] = O;

            foreach (var pb in triplets[p])
            {
                if (pb.end)
                {
                    continue;
                }
                pb.end = true;

                var(i, j) = pb.p;
                if (!pb.vertical)
                {
                    t[i][j - 1]--;
                    t[i][j]--;
                    t[i][j + 1]--;

                    if (t[i][j - 1] > 0)
                    {
                        q.Push(new Point(i, j - 1));
                    }
                    if (t[i][j] > 0)
                    {
                        q.Push(new Point(i, j));
                    }
                    if (t[i][j + 1] > 0)
                    {
                        q.Push(new Point(i, j + 1));
                    }
                }
                else
                {
                    t[i - 1][j]--;
                    t[i][j]--;
                    t[i + 1][j]--;

                    if (t[i - 1][j] > 0)
                    {
                        q.Push(new Point(i - 1, j));
                    }
                    if (t[i][j] > 0)
                    {
                        q.Push(new Point(i, j));
                    }
                    if (t[i + 1][j] > 0)
                    {
                        q.Push(new Point(i + 1, j));
                    }
                }
            }
        }

        return(string.Join("\n", s.Select(cs => new string(cs))));
    }