KAMEN1973

MostReliablePath

Feb 11th, 2021 (edited)
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Wintellect.PowerCollections;
  5.  
  6. namespace MostReliablePath
  7. {
  8.     class Program
  9.     {
  10.         private class Edge
  11.         {
  12.             public int First { get; set; }
  13.  
  14.             public int Second { get; set; }
  15.  
  16.             public int Weight { get; set; }
  17.  
  18.             public override string ToString()
  19.             {
  20.                 return $"{this.First} - {this.Second}";
  21.             }
  22.         }
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             int nodesCount = int.Parse(Console.ReadLine());
  27.             int edgesCount = int.Parse(Console.ReadLine());
  28.             List<Edge>[] graph = ReadGraph(nodesCount, edgesCount);
  29.             int start = int.Parse(Console.ReadLine());
  30.             int end = int.Parse(Console.ReadLine());
  31.  
  32.             GetMostReliablePath(graph, start, end);
  33.         }
  34.  
  35.         private static void GetMostReliablePath(List<Edge>[] graph, int start, int end)
  36.         {
  37.             double[] distances = new double[graph.Length];
  38.             for (int i = 0; i < distances.Length; i++)
  39.             {
  40.                 distances[i] = double.NegativeInfinity;
  41.             }
  42.             //Array.Fill(distances, double.NegativeInfinity);
  43.             distances[start] = 100;
  44.  
  45.             int[] previous = new int[graph.Length];
  46.             previous[start] = -1;
  47.  
  48.             bool[] visited = new bool[graph.Length];
  49.  
  50.             OrderedBag<int> queue = new OrderedBag<int>(
  51.                 Comparer<int>.Create((a, b) => distances[b].CompareTo(distances[a])));
  52.  
  53.             queue.Add(start);
  54.             visited[start] = true;
  55.  
  56.             while (queue.Count > 0)
  57.             {
  58.                 int nearestNode = queue.RemoveFirst();
  59.  
  60.                 if (nearestNode == end)
  61.                 {
  62.                     Stack<int> path = ReconstructPath(previous, end);
  63.  
  64.                     Console.WriteLine($"Most reliable path reliability: {distances[nearestNode]:f2}%");
  65.                     Console.WriteLine(string.Join(" -> ", path));
  66.  
  67.                     break;
  68.                 }
  69.  
  70.                 foreach (Edge edge in graph[nearestNode])
  71.                 {
  72.                     int childNode = edge.Second;
  73.  
  74.                     if (!visited[childNode])
  75.                     {
  76.                         queue.Add(childNode);
  77.                         visited[childNode] = true;
  78.                     }
  79.  
  80.                     double newDistance = (edge.Weight * distances[nearestNode]) / 100;
  81.                     if (newDistance > distances[childNode])
  82.                     {
  83.                         distances[childNode] = newDistance;
  84.                         previous[childNode] = nearestNode;
  85.  
  86.                         queue = new OrderedBag<int>(
  87.                             queue,
  88.                             Comparer<int>.Create((a, b) => distances[b].CompareTo(distances[a])));
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private static Stack<int> ReconstructPath(int[] previous, int node)
  95.         {
  96.             Stack<int> path = new Stack<int>();
  97.  
  98.             while (node != -1)
  99.             {
  100.                 path.Push(node);
  101.                 node = previous[node];
  102.             }
  103.  
  104.             return path;
  105.         }
  106.  
  107.         private static List<Edge>[] ReadGraph(int nodesCount, int edgesCount)
  108.         {
  109.             List<Edge>[] graph = new List<Edge>[nodesCount];
  110.  
  111.             for (int i = 0; i < graph.Length; i++)
  112.             {
  113.                 graph[i] = new List<Edge>();
  114.             }
  115.  
  116.             for (int i = 0; i < edgesCount; i++)
  117.             {
  118.                 int[] data = Console.ReadLine()
  119.                     .Split()
  120.                     .Select(int.Parse)
  121.                     .ToArray();
  122.                 int first = data[0];
  123.                 int second = data[1];
  124.                 int weight = data[2];
  125.  
  126.                 graph[first].Add(new Edge
  127.                 {
  128.                     First = first,
  129.                     Second = second,
  130.                     Weight = weight,
  131.                 });
  132.  
  133.                 graph[second].Add(new Edge
  134.                 {
  135.                     First = second,
  136.                     Second = first,
  137.                     Weight = weight,
  138.                 });
  139.             }
  140.  
  141.             return graph;
  142.         }
  143.     }
  144. }
Add Comment
Please, Sign In to add comment