Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics.CodeAnalysis;
- namespace Energy
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- private static Dictionary<int, List<Node>> graph = new Dictionary<int, List<Node>>();
- private static HashSet<int> visited = new HashSet<int>();
- //private static long sum = 0;
- private static long minSum = int.MaxValue;
- public static void Main()
- {
- ReadInput();
- foreach (var node in graph.Keys)
- {
- //sum = 0;
- var currentSum = FindMinimumSpanningTree(node);
- if (currentSum < minSum)
- {
- minSum = currentSum;
- }
- }
- Console.WriteLine(minSum);
- }
- private static void ReadInput()
- {
- var n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- var from = edge[0];
- var to = edge[1];
- var weight = edge[2];
- if (!graph.ContainsKey(from))
- {
- graph[from] = new List<Node>();
- }
- if (!graph.ContainsKey(to))
- {
- graph[to] = new List<Node>();
- }
- var fromNode = new Node(from, weight);
- var toNode = new Node(to, weight);
- // two way
- graph[from].Add(toNode);
- graph[to].Add(fromNode);
- }
- }
- //Prim
- private static long FindMinimumSpanningTree(int startLabel)
- {
- var queue = new PriorityQueue<Edge>();
- //var used = new bool[numberOfNodes + 1];
- var visited = new HashSet<int>();
- var tree = new HashSet<Edge>();
- queue.Enqueue(new Edge(-1, startLabel, 0));
- while (queue.Count > 0)
- {
- Edge current = queue.Dequeue(); //.First(); //.Min;
- //queue.Remove(current);
- //!used[current.To]
- if (visited.Contains(current.To))
- {
- continue;
- }
- visited.Add(current.To); //used[current.To] = true; // we "visit" this node
- tree.Add(current);
- foreach (var node in graph[current.To])
- {
- //if (visited.Contains(node.Label))
- //{
- // return;
- //}
- queue.Enqueue(new Edge(current.To, node.Label, node.Weight));
- }
- //// add neighbours
- //foreach (var edg in graph)
- //{
- // if (!tree.Contains(edg))
- // {
- // if (current.To == edg.From && !visited.Contains(edg.To)) //!used[edg.To]
- // {
- // queue.Add(edg);
- // }
- // }
- //}
- }
- long sum = 0;
- foreach (var treeEdge in tree)
- {
- sum += treeEdge.Weight;
- //Console.WriteLine("From: " + treeEdge.From + " To: " + treeEdge.To + " Weight: " + treeEdge.Weight);
- }
- return sum;
- }
- }
- public class Edge : IComparable<Edge>, IComparable
- {
- public Edge(int startNode, int endNode, int weight)
- {
- this.From = startNode;
- this.To = endNode;
- this.Weight = weight;
- }
- public int From { get; set; }
- public int To { get; set; }
- public int Weight { get; set; }
- public int CompareTo(Edge other)
- {
- int weightCompared = this.Weight.CompareTo(other.Weight);
- if (weightCompared == 0)
- {
- return this.From.CompareTo(other.From);
- }
- return weightCompared;
- }
- public override string ToString()
- {
- return string.Format("({0} {1}) -> {2}", this.From, this.To, this.Weight);
- }
- public int CompareTo(object obj)
- {
- var other = obj as Edge;
- int weightCompared = this.Weight.CompareTo(other.Weight);
- if (weightCompared == 0)
- {
- return this.From.CompareTo(other.From);
- }
- return weightCompared;
- }
- }
- public class Node : IComparable<Node>, IComparable
- {
- public Node(int label, int weight)
- {
- this.Label = label;
- this.Weight = weight;
- }
- public int Label { get; set; }
- public int Weight { get; set; }
- public int CompareTo(Node other)
- {
- if (this.Weight.CompareTo(other.Weight) == 0)
- {
- return this.Label.CompareTo(other.Label);
- }
- return this.Weight.CompareTo(other.Weight);
- }
- public int CompareTo(object obj)
- {
- Node other = obj as Node;
- return this.CompareTo(other);
- }
- }
- public class PriorityQueue<T> where T : IComparable
- {
- private T[] heap;
- private int index;
- public int Count
- {
- get
- {
- return this.index - 1;
- }
- }
- public PriorityQueue()
- {
- this.heap = new T[16];
- this.index = 1;
- }
- public void Enqueue(T element)
- {
- if (this.index >= this.heap.Length)
- {
- IncreaseArray();
- }
- this.heap[this.index] = element;
- int childIndex = this.index;
- int parentIndex = childIndex / 2;
- this.index++;
- while (parentIndex >= 1 && this.heap[childIndex].CompareTo(this.heap[parentIndex]) < 0)
- {
- T swapValue = this.heap[parentIndex];
- this.heap[parentIndex] = this.heap[childIndex];
- this.heap[childIndex] = swapValue;
- childIndex = parentIndex;
- parentIndex = childIndex / 2;
- }
- }
- public T Dequeue()
- {
- T result = this.heap[1];
- this.heap[1] = this.heap[this.Count];
- this.index--;
- int rootIndex = 1;
- int minChild;
- while (true)
- {
- int leftChildIndex = rootIndex * 2;
- int rightChildIndex = rootIndex * 2 + 1;
- if (leftChildIndex > this.index)
- {
- break;
- }
- else if (rightChildIndex > this.index)
- {
- minChild = leftChildIndex;
- }
- else
- {
- if (this.heap[leftChildIndex].CompareTo(this.heap[rightChildIndex]) < 0)
- {
- minChild = leftChildIndex;
- }
- else
- {
- minChild = rightChildIndex;
- }
- }
- if (this.heap[minChild].CompareTo(this.heap[rootIndex]) < 0)
- {
- T swapValue = this.heap[rootIndex];
- this.heap[rootIndex] = this.heap[minChild];
- this.heap[minChild] = swapValue;
- rootIndex = minChild;
- }
- else
- {
- break;
- }
- }
- return result;
- }
- public T Peek()
- {
- return this.heap[1];
- }
- private void IncreaseArray()
- {
- T[] copiedHeap = new T[this.heap.Length * 2];
- for (int i = 0; i < this.heap.Length; i++)
- {
- copiedHeap[i] = this.heap[i];
- }
- this.heap = copiedHeap;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement