Exemplo n.º 1
0
        public static void ElectrificationPlan()
        {
            var input         = Console.ReadLine().Split();
            var n             = Int32.Parse(input[0]);
            var k             = Int32.Parse(input[1]);
            var dj            = new DisjointSet(n);
            var edges         = new List <Edge <int> >();
            var matrix        = new double[n, n];
            var builtStations = Console.ReadLine().Split(' ').Select(Int32.Parse).ToList();

            for (int i = 0; i < n; i++)
            {
                var inp = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = Int32.Parse(inp[j]);
                }
            }

            foreach (var item in builtStations)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (item == j || builtStations.Contains(j))
                    {
                        continue;
                    }
                    edges.Add(new Edge <int>(item, j, matrix[item - 1, j - 1]));
                }
            }

            for (int i = 1; i <= n; i++)
            {
                if (builtStations.Contains(i))
                {
                    continue;
                }
                for (int j = 1; j <= n; j++)
                {
                    if (edges.Any(x => x.To == i && x.From == j) || i == j)
                    {
                        continue;
                    }
                    edges.Add(new Edge <int>(i, j, matrix[i - 1, j - 1]));
                }
            }
            var a = dj.KruskalMTS(edges, builtStations);

            double sum = a.Sum(t => t.Weight);

            Console.WriteLine(sum);
        }
Exemplo n.º 2
0
        public static void Network()
        {
            var nm    = Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray();
            var n     = nm[0];
            var m     = nm[1];
            var edges = new List <Edge <int> >();
            var dj    = new DisjointSet(n);

            for (int i = 1; i <= m; i++)
            {
                var input = Console.ReadLine().Split().Select(Int32.Parse).ToArray();
                edges.Add(new Edge <int>(input[0], input[1], input[2]));
            }

            var k = dj.KruskalMTS(edges);

            Console.WriteLine(k.Max(x => x.Weight));
            Console.WriteLine(k.Count);
            foreach (var k0 in k)
            {
                Console.WriteLine(k0.From + " " + k0.To);
            }
        }