Advertisement
svetlai

Friends

Nov 21st, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. namespace Friends
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.         private static int start;
  10.         private static int end;
  11.         private static int middle1;
  12.         private static int middle2;
  13.         private static int n;
  14.         private static int m;
  15.         private static List<Node>[] graph;
  16.  
  17.         public static void Main()
  18.         {
  19.             ReadInput();
  20.  
  21.             var best = Solve();
  22.  
  23.             Console.WriteLine(best);
  24.         }
  25.  
  26.         private static int Solve()
  27.         {
  28.             var middle1Distances = FindPathDijkstra(middle1, new int[] { start, end, middle2 });
  29.             var middle2Distances = FindPathDijkstra(middle2, new int[] { start, end, middle1 });
  30.  
  31.             int middle1ToStartDistance = middle1Distances[start];
  32.             int middle2ToStartDistance = middle2Distances[start];
  33.  
  34.             int middle1ToEndDistance = middle1Distances[end];
  35.             int middle2ToEndDistance = middle2Distances[end];
  36.  
  37.             int middle1ToMiddle2Distance = middle1Distances[middle2];
  38.             int middle2ToMiddle1Distance = middle2Distances[middle1];
  39.  
  40.             var firstPossiblePath = middle1ToStartDistance + middle1ToMiddle2Distance + middle2ToEndDistance;
  41.             var secondPossiblePath = middle2ToStartDistance + middle2ToMiddle1Distance + middle1ToEndDistance;
  42.  
  43.             var best = Math.Min(firstPossiblePath, secondPossiblePath);
  44.  
  45.             return best;
  46.         }
  47.  
  48.         private static int[] FindPathDijkstra(int from, int[] skip)
  49.         {
  50.             // dijkstra
  51.             int firstNodeLabel = from;
  52.             int[] distances = new int[n].Select(d => int.MaxValue).ToArray();
  53.             var queue = new SortedSet<Node>();
  54.             var visited = new HashSet<int>();
  55.  
  56.  
  57.             foreach (var nodeLabel in skip)
  58.             {
  59.                 visited.Add(nodeLabel);
  60.             }
  61.  
  62.             distances[firstNodeLabel] = 0;
  63.             queue.Add(new Node(firstNodeLabel, 0));
  64.  
  65.             while (queue.Count > 0)
  66.             {
  67.                 var current = queue.First();
  68.                 queue.Remove(current);
  69.                 visited.Add(current.Label);
  70.  
  71.                 // calculate distance
  72.                 foreach (var neighbour in graph[current.Label])
  73.                 {
  74.                     var currentDistance = distances[neighbour.Label];
  75.                     var newDistance = distances[current.Label] + neighbour.Weight;
  76.                     if (currentDistance > newDistance)
  77.                     {
  78.                         // new solution better than the previous
  79.                         distances[neighbour.Label] = newDistance;
  80.                         queue.Add(new Node(neighbour.Label, newDistance));
  81.                     }
  82.                 }
  83.  
  84.                 // remove top visited from queue //current?
  85.                 while (queue.Count > 0 && visited.Contains(queue.First().Label))
  86.                 {
  87.                     queue.Remove(queue.First());
  88.                 }
  89.  
  90.             }
  91.  
  92.             return distances;
  93.         }
  94.  
  95.         private static void ReadInput()
  96.         {
  97.             var counts = GetConsoleLine();
  98.             n = counts[0];
  99.             m = counts[1];
  100.  
  101.             var endPoints = GetConsoleLine();
  102.             start = endPoints[0] - 1;
  103.             end = endPoints[1] - 1;
  104.  
  105.             var middlePoints = GetConsoleLine();
  106.             middle1 = middlePoints[0] - 1;
  107.             middle2 = middlePoints[1] - 1;
  108.  
  109.             graph = new List<Node>[n];
  110.             for (int i = 0; i < m; i++)
  111.             {
  112.                 var edge = GetConsoleLine();
  113.  
  114.                 var from = edge[0] - 1;
  115.                 var to = edge[1] - 1;
  116.                 var weight = edge[2];
  117.  
  118.                 if (graph[from] == null)
  119.                 {
  120.                     graph[from] = new List<Node>();
  121.                 }
  122.  
  123.                 if (graph[to] == null)
  124.                 {
  125.                     graph[to] = new List<Node>();
  126.                 }
  127.  
  128.                 graph[from].Add(new Node(to, weight));
  129.                 graph[to].Add(new Node(from, weight));
  130.             }
  131.         }
  132.  
  133.         private static int[] GetConsoleLine()
  134.         {
  135.             return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  136.         }
  137.     }
  138.  
  139.     public class Node : IComparable<Node>
  140.     {
  141.         public Node(int label, int weight)
  142.         {
  143.             this.Label = label;
  144.             this.Weight = weight;
  145.         }
  146.  
  147.         public int Label { get; set; }
  148.  
  149.         public int Weight { get; set; }
  150.  
  151.         public int CompareTo(Node other)
  152.         {
  153.             if (this.Weight.CompareTo(other.Weight) == 0)
  154.             {
  155.                 return this.Label.CompareTo(other.Label);
  156.             }
  157.  
  158.             return this.Weight.CompareTo(other.Weight);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement