Ejemplo n.º 1
0
    static void Main()
    {
        var(n, m, s, t) = Read4();
        var p   = Read();
        var map = GraphConsole.ReadUnweightedMap(n + 1, m, false);

        int x = p[s - 1], y = 0;
        var u = new bool[n + 1];
        var q = PriorityQueue <int> .CreateWithKey(v => p[v - 1], true);

        u[s] = true;
        q.Push(s);

        while (q.Any)
        {
            var(v, pv) = q.Pop();
            if (x > pv)
            {
                x = pv;
                y++;
            }

            foreach (var nv in map[v])
            {
                if (u[nv])
                {
                    continue;
                }
                u[nv] = true;
                q.Push(nv);
            }
        }
        Console.WriteLine(y);
    }
Ejemplo n.º 2
0
    static object Solve()
    {
        Console.ReadLine();
        var(n, m) = Read2();
        var map = GraphConsole.ReadUnweightedMap(n + 1, m, true);

        var r  = map.Bfs(1);
        var dp = Array.ConvertAll(new bool[n + 1], _ => - 1L);

        void Dfs(int v)
        {
            if (dp[v] >= 0)
            {
                return;
            }
            dp[v] = r[v];

            foreach (var nv in map[v])
            {
                if (r[nv] <= r[v])
                {
                    dp[v] = Math.Min(dp[v], r[nv]);
                }
                else
                {
                    Dfs(nv);
                    dp[v] = Math.Min(dp[v], dp[nv]);
                }
            }
        }

        Dfs(1);
        return(string.Join(" ", dp[1..]));
Ejemplo n.º 3
0
    static object Solve()
    {
        Console.ReadLine();
        var(n, m) = Read2();
        var map = GraphConsole.ReadUnweightedMap(n + 1, m, true);

        var r  = map.Bfs(1);
        var dp = r.RawCosts.ToArray();

        var q = Enumerable.Range(1, n).Select(v => (v, d: r[v])).OrderBy(t => - t.d);

        foreach (var(v, d) in q)
        {
            foreach (var nv in map[v])
            {
                if (r[nv] <= r[v])
                {
                    dp[v] = Math.Min(dp[v], r[nv]);
                }
                else
                {
                    dp[v] = Math.Min(dp[v], dp[nv]);
                }
            }
        }
        return(string.Join(" ", dp[1..]));
Ejemplo n.º 4
0
    static void Main()
    {
        var(n, m) = Read2();
        var map = GraphConsole.ReadUnweightedMap(n + 1, m, false);

        var r = map.Bfs(1);

        Console.WriteLine("Yes");
        Console.WriteLine(string.Join("\n", r.RawInVertexes[2..]));
Ejemplo n.º 5
0
    static object Solve()
    {
        var(n, m) = Read2();
        var map = GraphConsole.ReadUnweightedMap(n + 1, m, true);

        var r = 0;

        for (int v = 1; v <= n; v++)
        {
            var result = map.Bfs(v);
            r += result.RawCosts.Count(x => x != long.MaxValue);
        }

        return(r);
    }