Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- namespace FriendsOfPesho
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- private static List<Node>[] graph;
- private static int n;
- private static HashSet<int> hospitals;
- public static void Main()
- {
- ReadInput();
- int bestDistance = int.MaxValue;
- foreach (var hospital in hospitals)
- {
- var distances = FindBestPath(hospital);
- int totalDistance = 0;
- for (int j = 1; j <= n; j++) // j n
- {
- if (!hospitals.Contains(j))
- {
- totalDistance += distances[j];
- }
- }
- if (totalDistance < bestDistance)
- {
- bestDistance = totalDistance;
- }
- }
- Console.WriteLine(bestDistance);
- }
- private static void ReadInput()
- {
- var numbers = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- n = numbers[0];
- var m = numbers[1];
- var hospitalsInput = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- hospitals = new HashSet<int>(hospitalsInput);
- graph = new List<Node>[n + 1];
- for (int i = 0; i < m; i++)
- {
- var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- var from = edge[0];
- var to = edge[1];
- var weight = edge[2];
- if (graph[from] == null)
- {
- graph[from] = new List<Node>();
- }
- if (graph[to] == null)
- {
- 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);
- }
- }
- //dijkstra
- private static int[] FindBestPath(int from)
- {
- int firstNodeLabel = from;
- int[] distances = new int[n + 1].Select(d => int.MaxValue).ToArray();
- var queue = new PriorityQueue<Node>(); //new SortedSet<Node>();
- var visited = new HashSet<int>();
- distances[firstNodeLabel] = 0;
- queue.Enqueue(new Node(firstNodeLabel, 0));
- while (queue.Count > 0)
- {
- var current = queue.Dequeue();
- //queue.Remove(current);
- visited.Add(current.Label);
- // calculate distance
- foreach (var neighbour in graph[current.Label])
- {
- var currentDistance = distances[neighbour.Label];
- var newDistance = distances[current.Label] + neighbour.Weight;
- if (currentDistance > newDistance)
- {
- // new solution better than the previous
- distances[neighbour.Label] = newDistance;
- queue.Enqueue(new Node(neighbour.Label, newDistance));
- }
- }
- // remove top visited from queue //current?
- while (queue.Count > 0 && visited.Contains(queue.Peek().Label))
- {
- queue.Dequeue();
- }
- }
- return distances;
- }
- }
- 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