Submission #30304848


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks
{
    public class F
    {
        public static void Main()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (N, M) = Scanner.Scan<int, int>();
            var G = new List<int>[N].Select(x => new List<int>()).ToArray();
            for (var i = 0; i < M; i++)
            {
                var (u, v) = Scanner.Scan<int, int>();
                u--; v--;
                G[u].Add(v);
                G[v].Add(u);
            }

            const int inf = (int)1e9;
            var dp = new int[1 << N, N];
            for (var i = 1; i < 1 << N; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    dp[i, j] = inf;
                }
            }

            var queue = new Queue<(int U, int S, int L)>();
            for (var i = 0; i < N; i++)
            {
                var s = 1 << i;
                var l = 1;
                dp[s, i] = l;
                queue.Enqueue((i, s, l));
                while (queue.Count > 0)
                {
                    var (u, cs, cl) = queue.Dequeue();
                    // if (l > dp[cs]) continue;
                    foreach (var v in G[u])
                    {
                        var ns = (cs >> v & 1) == 0 ? cs + (1 << v) : cs - (1 << v);
                        var nl = cl + 1;
                        if (nl >= dp[ns, v]) continue;
                        dp[ns, v] = nl;
                        queue.Enqueue((v, ns, nl));
                    }
                }
            }

            var answer = 0;
            for (var i = 0; i < 1 << N; i++)
            {
                var min = inf;
                for (var j = 0; j < N; j++)
                {
                    min = Math.Min(min, dp[i, j]);
                }

                answer += min;
            }
            Console.WriteLine(answer);
        }

        public static class Scanner
        {
            public static string ScanLine() => Console.ReadLine()?.Trim() ?? string.Empty;
            public static string[] Scan() => ScanLine().Split(' ');
            public static T Scan<T>() where T : IConvertible => Convert<T>(Scan()[0]);
            public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]));
            }
            public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]));
            }
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]));
            }
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]));
            }
            public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]), Convert<T6>(line[5]));
            }
            public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => Scan().Select(Convert<T>);
            private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
        }
    }
}

Submission Info

Submission Time
Task F - Shortest Good Path
User AconCavy
Language C# (.NET Core 3.1.201)
Score 500
Code Size 4559 Byte
Status AC
Exec Time 797 ms
Memory 70288 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 33
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 75 ms 28032 KiB
001.txt AC 693 ms 53592 KiB
002.txt AC 695 ms 53424 KiB
003.txt AC 438 ms 41120 KiB
004.txt AC 518 ms 45216 KiB
005.txt AC 263 ms 45556 KiB
006.txt AC 336 ms 41116 KiB
007.txt AC 332 ms 41392 KiB
008.txt AC 343 ms 41612 KiB
009.txt AC 361 ms 41376 KiB
010.txt AC 369 ms 41164 KiB
011.txt AC 85 ms 27916 KiB
012.txt AC 117 ms 29528 KiB
013.txt AC 777 ms 53428 KiB
014.txt AC 88 ms 28420 KiB
015.txt AC 308 ms 40608 KiB
016.txt AC 79 ms 27996 KiB
017.txt AC 103 ms 29372 KiB
018.txt AC 75 ms 27872 KiB
019.txt AC 80 ms 28004 KiB
020.txt AC 636 ms 53548 KiB
021.txt AC 557 ms 53404 KiB
022.txt AC 636 ms 53576 KiB
023.txt AC 565 ms 53416 KiB
024.txt AC 631 ms 53600 KiB
025.txt AC 747 ms 53756 KiB
026.txt AC 797 ms 53712 KiB
027.txt AC 686 ms 53564 KiB
028.txt AC 641 ms 53420 KiB
029.txt AC 619 ms 70288 KiB
030.txt AC 744 ms 53620 KiB
example0.txt AC 76 ms 27892 KiB
example1.txt AC 84 ms 28252 KiB