Advertisement
svetlai

FriendsOfPesho

Nov 27th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.38 KB | None | 0 0
  1. using System.Net;
  2.  
  3. namespace FriendsOfPesho
  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 List<Node>[] graph;
  14.         private static int n;
  15.         private static HashSet<int> hospitals;
  16.  
  17.         public static void Main()
  18.         {
  19.             ReadInput();
  20.  
  21.             int bestDistance = int.MaxValue;
  22.             foreach (var hospital in hospitals)
  23.             {
  24.                 var distances = FindBestPath(hospital);
  25.                 int totalDistance = 0;
  26.                 for (int j = 1; j <= n; j++) // j n
  27.                 {
  28.                     if (!hospitals.Contains(j))
  29.                     {
  30.                         totalDistance += distances[j];
  31.                     }
  32.                 }
  33.  
  34.                 if (totalDistance < bestDistance)
  35.                 {
  36.                     bestDistance = totalDistance;
  37.                 }
  38.             }
  39.  
  40.             Console.WriteLine(bestDistance);
  41.         }
  42.  
  43.         private static void ReadInput()
  44.         {
  45.             var numbers = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  46.             n = numbers[0];
  47.             var m = numbers[1];
  48.             var hospitalsInput = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  49.             hospitals = new HashSet<int>(hospitalsInput);
  50.  
  51.             graph = new List<Node>[n + 1];
  52.             for (int i = 0; i < m; i++)
  53.             {
  54.                 var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  55.  
  56.                 var from = edge[0];
  57.                 var to = edge[1];
  58.                 var weight = edge[2];
  59.  
  60.                 if (graph[from] == null)
  61.                 {
  62.                     graph[from] = new List<Node>();
  63.                 }
  64.  
  65.                 if (graph[to] == null)
  66.                 {
  67.                     graph[to] = new List<Node>();
  68.                 }
  69.  
  70.                 var fromNode = new Node(from, weight);
  71.                 var toNode = new Node(to, weight);
  72.  
  73.                 // two way
  74.                 graph[from].Add(toNode);
  75.                 graph[to].Add(fromNode);
  76.             }
  77.         }
  78.  
  79.         //dijkstra
  80.         private static int[] FindBestPath(int from)
  81.         {
  82.             int firstNodeLabel = from;
  83.             int[] distances = new int[n + 1].Select(d => int.MaxValue).ToArray();
  84.             var queue = new PriorityQueue<Node>(); //new SortedSet<Node>();
  85.             var visited = new HashSet<int>();
  86.  
  87.             distances[firstNodeLabel] = 0;
  88.             queue.Enqueue(new Node(firstNodeLabel, 0));
  89.  
  90.             while (queue.Count > 0)
  91.             {
  92.                 var current = queue.Dequeue();
  93.                 //queue.Remove(current);
  94.                 visited.Add(current.Label);
  95.  
  96.                 // calculate distance
  97.                 foreach (var neighbour in graph[current.Label])
  98.                 {
  99.                     var currentDistance = distances[neighbour.Label];
  100.                     var newDistance = distances[current.Label] + neighbour.Weight;
  101.                     if (currentDistance > newDistance)
  102.                     {
  103.                         // new solution better than the previous
  104.                         distances[neighbour.Label] = newDistance;
  105.                         queue.Enqueue(new Node(neighbour.Label, newDistance));
  106.                     }
  107.                 }
  108.  
  109.                 // remove top visited from queue //current?
  110.                 while (queue.Count > 0 && visited.Contains(queue.Peek().Label))
  111.                 {
  112.                     queue.Dequeue();
  113.                 }
  114.             }
  115.  
  116.             return distances;
  117.         }
  118.     }
  119.  
  120.     public class Node : IComparable<Node>, IComparable
  121.     {
  122.         public Node(int label, int weight)
  123.         {
  124.             this.Label = label;
  125.             this.Weight = weight;
  126.         }
  127.  
  128.         public int Label { get; set; }
  129.  
  130.         public int Weight { get; set; }
  131.  
  132.         public int CompareTo(Node other)
  133.         {
  134.             if (this.Weight.CompareTo(other.Weight) == 0)
  135.             {
  136.                 return this.Label.CompareTo(other.Label);
  137.             }
  138.  
  139.             return this.Weight.CompareTo(other.Weight);
  140.         }
  141.  
  142.         public int CompareTo(object obj)
  143.         {
  144.             Node other = obj as Node;
  145.             return this.CompareTo(other);
  146.         }
  147.     }
  148.  
  149.     public class PriorityQueue<T> where T : IComparable
  150.     {
  151.         private T[] heap;
  152.         private int index;
  153.  
  154.         public int Count
  155.         {
  156.             get
  157.             {
  158.                 return this.index - 1;
  159.             }
  160.         }
  161.  
  162.         public PriorityQueue()
  163.         {
  164.             this.heap = new T[16];
  165.             this.index = 1;
  166.         }
  167.  
  168.         public void Enqueue(T element)
  169.         {
  170.             if (this.index >= this.heap.Length)
  171.             {
  172.                 IncreaseArray();
  173.             }
  174.  
  175.             this.heap[this.index] = element;
  176.  
  177.             int childIndex = this.index;
  178.             int parentIndex = childIndex / 2;
  179.             this.index++;
  180.  
  181.             while (parentIndex >= 1 && this.heap[childIndex].CompareTo(this.heap[parentIndex]) < 0)
  182.             {
  183.                 T swapValue = this.heap[parentIndex];
  184.                 this.heap[parentIndex] = this.heap[childIndex];
  185.                 this.heap[childIndex] = swapValue;
  186.  
  187.                 childIndex = parentIndex;
  188.                 parentIndex = childIndex / 2;
  189.             }
  190.         }
  191.  
  192.         public T Dequeue()
  193.         {
  194.             T result = this.heap[1];
  195.  
  196.             this.heap[1] = this.heap[this.Count];
  197.             this.index--;
  198.  
  199.             int rootIndex = 1;
  200.  
  201.             int minChild;
  202.  
  203.             while (true)
  204.             {
  205.                 int leftChildIndex = rootIndex * 2;
  206.                 int rightChildIndex = rootIndex * 2 + 1;
  207.  
  208.                 if (leftChildIndex > this.index)
  209.                 {
  210.                     break;
  211.                 }
  212.                 else if (rightChildIndex > this.index)
  213.                 {
  214.                     minChild = leftChildIndex;
  215.                 }
  216.                 else
  217.                 {
  218.                     if (this.heap[leftChildIndex].CompareTo(this.heap[rightChildIndex]) < 0)
  219.                     {
  220.                         minChild = leftChildIndex;
  221.                     }
  222.                     else
  223.                     {
  224.                         minChild = rightChildIndex;
  225.                     }
  226.                 }
  227.  
  228.                 if (this.heap[minChild].CompareTo(this.heap[rootIndex]) < 0)
  229.                 {
  230.                     T swapValue = this.heap[rootIndex];
  231.                     this.heap[rootIndex] = this.heap[minChild];
  232.                     this.heap[minChild] = swapValue;
  233.  
  234.                     rootIndex = minChild;
  235.                 }
  236.                 else
  237.                 {
  238.                     break;
  239.                 }
  240.             }
  241.  
  242.             return result;
  243.         }
  244.  
  245.         public T Peek()
  246.         {
  247.             return this.heap[1];
  248.         }
  249.  
  250.         private void IncreaseArray()
  251.         {
  252.             T[] copiedHeap = new T[this.heap.Length * 2];
  253.  
  254.             for (int i = 0; i < this.heap.Length; i++)
  255.             {
  256.                 copiedHeap[i] = this.heap[i];
  257.             }
  258.  
  259.             this.heap = copiedHeap;
  260.         }
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement