Advertisement
sylviapsh

Graphs (from Friends in need) - fast

Jun 24th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. namespace Graph
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Collections.Generic;
  6.     using Wintellect.PowerCollections;
  7.  
  8.     public struct Node
  9.     {
  10.         public int To { get; set; }
  11.  
  12.         public int Distance { get; set; }
  13.  
  14.         public Node(int vertex, int distance)
  15.             : this()
  16.         {
  17.             this.To = vertex;
  18.             this.Distance = distance;
  19.         }
  20.     }
  21.  
  22.     public class Program
  23.     {
  24.         private static IList<IList<Node>> graph = null;
  25.  
  26.         private static IList<int> Dijkstra(int start)
  27.         {
  28.             var distances = Enumerable.Repeat(int.MaxValue, graph.Count).ToArray();
  29.  
  30.             var queue = new OrderedBag<Node>((node1, node2) => node1.Distance.CompareTo(node2.Distance));
  31.  
  32.             distances[start] = 0;
  33.             queue.Add(new Node(start, 0));
  34.  
  35.             while (queue.Count != 0)
  36.             {
  37.                 var currentNode = queue.RemoveFirst();
  38.  
  39.                 foreach (var neighborNode in graph[currentNode.To])
  40.                 {
  41.                     int currentDistance = distances[currentNode.To] + neighborNode.Distance;
  42.  
  43.                     if (currentDistance < distances[neighborNode.To])
  44.                     {
  45.                         distances[neighborNode.To] = currentDistance;
  46.                         queue.Add(new Node(neighborNode.To, currentDistance));
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             return distances;
  52.         }
  53.  
  54.         public static void Main()
  55.         {
  56.             int[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  57.  
  58.             var hospitals = new HashSet<int>(
  59.                 Console.ReadLine().Split().Select(line => int.Parse(line) - 1));
  60.  
  61.             var edges = Enumerable.Range(0, numbers[1])
  62.                                   .Select(i => Console.ReadLine()
  63.                                                       .Split()
  64.                                                       .Select(int.Parse)
  65.                                                       .ToArray())
  66.                                   .ToArray();
  67.  
  68.             graph = Enumerable.Range(0, numbers[0])
  69.                               .Select(i => new List<Node>())
  70.                               .ToArray();
  71.  
  72.             foreach (var edge in edges)
  73.             {
  74.                 graph[edge[0] - 1].Add(new Node(edge[1] - 1, edge[2]));
  75.                 graph[edge[1] - 1].Add(new Node(edge[0] - 1, edge[2]));
  76.             }
  77.  
  78.             var results = hospitals.Select(Dijkstra);
  79.  
  80.             int min = results.Select(distances => distances.Where((distance, i) => !hospitals.Contains(i)).Sum()).Min();
  81.  
  82.             Console.WriteLine(min);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement