private static void Main() { var cableGraph = new Graph<int>(); var inputLines = int.Parse(Console.ReadLine()); for (var i = 0; i < inputLines; i++) { var edgeInfo = Console.ReadLine().Split().Select(int.Parse).ToArray(); cableGraph.AddConnection(edgeInfo[0], edgeInfo[1], edgeInfo[2], true); } var resultTree = cableGraph.PrimeMinimumSpanningTree(1); Console.WriteLine(resultTree.Sum(edge => edge.Distance)); }
private static Graph<int> InitializeGraph() { var graph = new Graph<int>(); for (var i = 0; i < 6; i++) { graph.AddNode(i); } graph.AddConnection(1, 2, 1, true); graph.AddConnection(1, 4, 2, true); graph.AddConnection(2, 3, 3, true); graph.AddConnection(2, 5, 13, true); graph.AddConnection(3, 4, 4, true); graph.AddConnection(3, 6, 3, true); graph.AddConnection(4, 6, 16, true); graph.AddConnection(4, 7, 14, true); graph.AddConnection(5, 6, 12, true); graph.AddConnection(5, 8, 1, true); graph.AddConnection(5, 9, 13, true); graph.AddConnection(6, 7, 1, true); graph.AddConnection(6, 9, 1, true); return graph; }
public static void Main() { Graph<int> graph = new Graph<int>(); graph.AddConnection(1, 2, 14, true); graph.AddConnection(1, 3, 10, true); graph.AddConnection(2, 4, 14, true); graph.AddConnection(1, 5, 18, true); graph.AddConnection(2, 6, 9, true); graph.AddConnection(3, 6, 10, true); graph.AddConnection(3, 4, 14, true); graph.AddConnection(7, 8, 9, false); graph.AddConnection(4, 7, 15, false); graph.AddConnection(4, 8, 11, true); graph.AddConnection(9, 10, 7, false); graph.AddConnection(1, 10, 10, true); graph.AddConnection(3, 9, 11, true); graph.AddConnection(8, 11, 8, false); graph.AddConnection(11, 12, 12, true); graph.AddConnection(9, 12, 17, true); graph.AddConnection(5, 10, 13, true); graph.AddConnection(6, 12, 15, true); graph.AddConnection(7, 13, 7, true); graph.AddConnection(8, 13, 10, true); graph.AddConnection(11, 13, 13, true); graph.AddConnection(7, 12, 12, true); graph.AddConnection(11, 14, 7, true); graph.AddConnection(12, 14, 10, false); List<Edge<int>> edges = graph.PrimeMinimumSpanningTree(1); Console.WriteLine("The edges of the minimum spanning tree from node 1 is:"); foreach (var edje in edges) { Console.WriteLine(edje); } }