Advertisement
svetlai

Energo-Prim

Dec 1st, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.08 KB | None | 0 0
  1. using System.Diagnostics.CodeAnalysis;
  2.  
  3. namespace Energy
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Linq;
  8.     using System.Text;
  9.     using System.Threading.Tasks;
  10.  
  11.     public class Program
  12.     {
  13.         private static Dictionary<int, List<Node>> graph = new Dictionary<int, List<Node>>();
  14.         private static HashSet<int> visited = new HashSet<int>();
  15.         //private static long sum = 0;
  16.         private static long minSum = int.MaxValue;
  17.  
  18.         public static void Main()
  19.         {
  20.             ReadInput();
  21.             foreach (var node in graph.Keys)
  22.             {
  23.                 //sum = 0;
  24.                 var currentSum = FindMinimumSpanningTree(node);
  25.                 if (currentSum < minSum)
  26.                 {
  27.                     minSum = currentSum;
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine(minSum);
  32.         }
  33.  
  34.         private static void ReadInput()
  35.         {
  36.             var n = int.Parse(Console.ReadLine());
  37.  
  38.             for (int i = 0; i < n; i++)
  39.             {
  40.                 var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  41.  
  42.                 var from = edge[0];
  43.                 var to = edge[1];
  44.                 var weight = edge[2];
  45.  
  46.                 if (!graph.ContainsKey(from))
  47.                 {
  48.                     graph[from] = new List<Node>();
  49.                 }
  50.  
  51.                 if (!graph.ContainsKey(to))
  52.                 {
  53.                     graph[to] = new List<Node>();
  54.                 }
  55.  
  56.                 var fromNode = new Node(from, weight);
  57.                 var toNode = new Node(to, weight);
  58.  
  59.                 // two way
  60.                 graph[from].Add(toNode);
  61.                 graph[to].Add(fromNode);
  62.             }
  63.         }
  64.  
  65.         //Prim
  66.         private static long FindMinimumSpanningTree(int startLabel)
  67.         {
  68.             var queue = new PriorityQueue<Edge>();
  69.             //var used = new bool[numberOfNodes + 1];
  70.             var visited = new HashSet<int>();
  71.             var tree = new HashSet<Edge>();
  72.  
  73.             queue.Enqueue(new Edge(-1, startLabel, 0));
  74.  
  75.             while (queue.Count > 0)
  76.             {
  77.                 Edge current = queue.Dequeue(); //.First(); //.Min;
  78.                 //queue.Remove(current);
  79.  
  80.                 //!used[current.To]
  81.  
  82.                 if (visited.Contains(current.To))
  83.                 {
  84.                     continue;
  85.                 }
  86.  
  87.                 visited.Add(current.To); //used[current.To] = true; // we "visit" this node
  88.                 tree.Add(current);
  89.  
  90.                 foreach (var node in graph[current.To])
  91.                 {
  92.                     //if (visited.Contains(node.Label))
  93.                     //{
  94.                     //    return;
  95.                     //}
  96.  
  97.                     queue.Enqueue(new Edge(current.To, node.Label, node.Weight));
  98.                 }
  99.  
  100.  
  101.                 //// add neighbours
  102.                 //foreach (var edg in graph)
  103.                 //{
  104.                 //    if (!tree.Contains(edg))
  105.                 //    {
  106.                 //        if (current.To == edg.From && !visited.Contains(edg.To)) //!used[edg.To]
  107.                 //        {
  108.                 //            queue.Add(edg);
  109.                 //        }
  110.                 //    }
  111.                 //}
  112.  
  113.             }
  114.  
  115.             long sum = 0;
  116.             foreach (var treeEdge in tree)
  117.             {
  118.                 sum += treeEdge.Weight;
  119.                 //Console.WriteLine("From: " + treeEdge.From + " To: " + treeEdge.To + " Weight: " + treeEdge.Weight);
  120.             }
  121.  
  122.             return sum;
  123.         }
  124.     }
  125.  
  126.     public class Edge : IComparable<Edge>, IComparable
  127.     {
  128.         public Edge(int startNode, int endNode, int weight)
  129.         {
  130.             this.From = startNode;
  131.             this.To = endNode;
  132.             this.Weight = weight;
  133.         }
  134.  
  135.         public int From { get; set; }
  136.  
  137.         public int To { get; set; }
  138.  
  139.         public int Weight { get; set; }
  140.  
  141.         public int CompareTo(Edge other)
  142.         {
  143.             int weightCompared = this.Weight.CompareTo(other.Weight);
  144.  
  145.             if (weightCompared == 0)
  146.             {
  147.                 return this.From.CompareTo(other.From);
  148.             }
  149.             return weightCompared;
  150.         }
  151.  
  152.         public override string ToString()
  153.         {
  154.             return string.Format("({0} {1}) -> {2}", this.From, this.To, this.Weight);
  155.         }
  156.  
  157.         public int CompareTo(object obj)
  158.         {
  159.             var other = obj as Edge;
  160.             int weightCompared = this.Weight.CompareTo(other.Weight);
  161.  
  162.             if (weightCompared == 0)
  163.             {
  164.                 return this.From.CompareTo(other.From);
  165.             }
  166.             return weightCompared;
  167.         }
  168.     }
  169.  
  170.  
  171.     public class Node : IComparable<Node>, IComparable
  172.     {
  173.         public Node(int label, int weight)
  174.         {
  175.             this.Label = label;
  176.             this.Weight = weight;
  177.         }
  178.  
  179.         public int Label { get; set; }
  180.  
  181.         public int Weight { get; set; }
  182.  
  183.         public int CompareTo(Node other)
  184.         {
  185.             if (this.Weight.CompareTo(other.Weight) == 0)
  186.             {
  187.                 return this.Label.CompareTo(other.Label);
  188.             }
  189.  
  190.             return this.Weight.CompareTo(other.Weight);
  191.         }
  192.  
  193.         public int CompareTo(object obj)
  194.         {
  195.             Node other = obj as Node;
  196.             return this.CompareTo(other);
  197.         }
  198.     }
  199.  
  200.     public class PriorityQueue<T> where T : IComparable
  201.     {
  202.         private T[] heap;
  203.         private int index;
  204.  
  205.         public int Count
  206.         {
  207.             get
  208.             {
  209.                 return this.index - 1;
  210.             }
  211.         }
  212.  
  213.         public PriorityQueue()
  214.         {
  215.             this.heap = new T[16];
  216.             this.index = 1;
  217.         }
  218.  
  219.         public void Enqueue(T element)
  220.         {
  221.             if (this.index >= this.heap.Length)
  222.             {
  223.                 IncreaseArray();
  224.             }
  225.  
  226.             this.heap[this.index] = element;
  227.  
  228.             int childIndex = this.index;
  229.             int parentIndex = childIndex / 2;
  230.             this.index++;
  231.  
  232.             while (parentIndex >= 1 && this.heap[childIndex].CompareTo(this.heap[parentIndex]) < 0)
  233.             {
  234.                 T swapValue = this.heap[parentIndex];
  235.                 this.heap[parentIndex] = this.heap[childIndex];
  236.                 this.heap[childIndex] = swapValue;
  237.  
  238.                 childIndex = parentIndex;
  239.                 parentIndex = childIndex / 2;
  240.             }
  241.         }
  242.  
  243.         public T Dequeue()
  244.         {
  245.             T result = this.heap[1];
  246.  
  247.             this.heap[1] = this.heap[this.Count];
  248.             this.index--;
  249.  
  250.             int rootIndex = 1;
  251.  
  252.             int minChild;
  253.  
  254.             while (true)
  255.             {
  256.                 int leftChildIndex = rootIndex * 2;
  257.                 int rightChildIndex = rootIndex * 2 + 1;
  258.  
  259.                 if (leftChildIndex > this.index)
  260.                 {
  261.                     break;
  262.                 }
  263.                 else if (rightChildIndex > this.index)
  264.                 {
  265.                     minChild = leftChildIndex;
  266.                 }
  267.                 else
  268.                 {
  269.                     if (this.heap[leftChildIndex].CompareTo(this.heap[rightChildIndex]) < 0)
  270.                     {
  271.                         minChild = leftChildIndex;
  272.                     }
  273.                     else
  274.                     {
  275.                         minChild = rightChildIndex;
  276.                     }
  277.                 }
  278.  
  279.                 if (this.heap[minChild].CompareTo(this.heap[rootIndex]) < 0)
  280.                 {
  281.                     T swapValue = this.heap[rootIndex];
  282.                     this.heap[rootIndex] = this.heap[minChild];
  283.                     this.heap[minChild] = swapValue;
  284.  
  285.                     rootIndex = minChild;
  286.                 }
  287.                 else
  288.                 {
  289.                     break;
  290.                 }
  291.             }
  292.  
  293.             return result;
  294.         }
  295.  
  296.         public T Peek()
  297.         {
  298.             return this.heap[1];
  299.         }
  300.  
  301.         private void IncreaseArray()
  302.         {
  303.             T[] copiedHeap = new T[this.heap.Length * 2];
  304.  
  305.             for (int i = 0; i < this.heap.Length; i++)
  306.             {
  307.                 copiedHeap[i] = this.heap[i];
  308.             }
  309.  
  310.             this.heap = copiedHeap;
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement