Advertisement
IvetValcheva

Примерно решение №3

Oct 11th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Wintellect.PowerCollections;
  5.  
  6. namespace EmergencyPlan
  7. {
  8.     class Edge
  9.     {
  10.         public Edge(int first, int second, int time)
  11.         {
  12.             this.first = first;
  13.             this.second = second;
  14.             this.time = time;
  15.         }
  16.         public int first { set; get; }
  17.         public int second { set; get; }
  18.         public int time { set; get; }
  19.     }
  20.  
  21.     class Program
  22.     {
  23.         private static List<Edge>[] graph;
  24.         static void Main(string[] args)
  25.         {
  26.             int n = int.Parse(Console.ReadLine());
  27.             var excapeRooms = Console.ReadLine()
  28.                 .Split()
  29.                 .Select(int.Parse)
  30.                 .ToList();
  31.  
  32.             int e = int.Parse(Console.ReadLine());
  33.             graph = readGraph(n, e);
  34.  
  35.             int maxT = extractTime(Console.ReadLine());
  36.  
  37.             int[] timesToEscape = new int[n];
  38.             for (int i = 0; i < timesToEscape.Length; i++)
  39.             {
  40.                 timesToEscape[i] = int.MaxValue;
  41.             }
  42.  
  43.             for (int i = 0; i < excapeRooms.Count; i++)
  44.             {
  45.                 int exit = excapeRooms[i];
  46.                 int[] currentTimes = calculateAllTimes(exit, excapeRooms);
  47.                
  48.                 for (int t = 0; t < currentTimes.Length; t++)
  49.                 {
  50.                     if (currentTimes[t] < timesToEscape[t])
  51.                     {
  52.                         timesToEscape[t] = currentTimes[t];
  53.                     }
  54.                 }
  55.             }
  56.  
  57.             prepareOutput(timesToEscape, maxT);
  58.         }
  59.  
  60.         private static int[] calculateAllTimes(int exit, List<int> exits)
  61.         {
  62.             int[] times = new int[graph.Length];
  63.             for (int i = 0; i < times.Length; i++)
  64.             {
  65.                 times[i] = int.MaxValue;
  66.             }
  67.  
  68.             for (int room = 0; room < graph.Length; room++)
  69.             {
  70.                 if (exits.Contains(room))
  71.                 {
  72.                     times[room] = -1;
  73.                     continue;
  74.                 }
  75.  
  76.                 times[room] = calculateTime(room, exit);
  77.                 ;
  78.             }
  79.  
  80.             return times;
  81.         }
  82.  
  83.         private static int calculateTime(int start, int end)
  84.         {
  85.             int[] times = new int[graph.Length];
  86.             for (int i = 0; i < times.Length; i++)
  87.             {
  88.                 times[i] = int.MaxValue;
  89.             }
  90.  
  91.             OrderedBag<int> queue = new OrderedBag<int>(
  92.                 Comparer<int>.Create((f, s) => times[f] - times[s]));
  93.  
  94.             times[start] = 0;
  95.             queue.Add(start);
  96.  
  97.             while (queue.Count > 0)
  98.             {
  99.                 int current = queue.RemoveFirst();
  100.  
  101.                 if (current == end)
  102.                 {
  103.                     break;
  104.                 }
  105.  
  106.                 foreach (Edge edge in graph[current])
  107.                 {
  108.                     int otherNode = edge.first == current ? edge.second : edge.first;
  109.                     if (times[otherNode] == int.MaxValue)
  110.                     {
  111.                         queue.Add(otherNode);
  112.                     }
  113.  
  114.                     int newTime = times[current] + edge.time;
  115.                     if (newTime < times[otherNode])
  116.                     {
  117.                         times[otherNode] = newTime;
  118.                         queue = new OrderedBag<int>(queue,
  119.                             Comparer<int>.Create((f, s) => times[f] - times[s]));
  120.                     }
  121.                 }
  122.             }
  123.  
  124.             return times[end];
  125.         }
  126.  
  127.         private static List<Edge>[] readGraph(int n, int e)
  128.         {
  129.             List<Edge>[] result = new List<Edge>[n];
  130.             for (int i = 0; i < n; i++)
  131.             {
  132.                 result[i] = new List<Edge>();
  133.             }
  134.  
  135.             for (int i = 0; i < e; i++)
  136.             {
  137.                 string[] data = Console.ReadLine()
  138.                     .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  139.                     .ToArray();
  140.  
  141.                 int room1 = int.Parse(data[0]);
  142.                 int room2 = int.Parse(data[1]);
  143.                 int time = extractTime(data[2]);
  144.  
  145.                 Edge edge = new Edge(room1, room2, time);
  146.                 result[room1].Add(edge);
  147.                 result[room2].Add(edge);
  148.             }
  149.  
  150.             return result;
  151.         }
  152.  
  153.         private static string convertTime(int time)
  154.         {
  155.             int hour = time / 3600;
  156.             time -= (hour * 3600);
  157.  
  158.             int min = time / 60;
  159.             time -= (min * 60);
  160.  
  161.             int sec = time;
  162.  
  163.             string h = String.Format("{0:0#}", hour);
  164.             string m = String.Format("{0:0#}", min);
  165.             string s = String.Format("{0:0#}", sec);
  166.             return h + ":" + m + ":" + s;
  167.         }
  168.  
  169.         private static int extractTime(string timeString)
  170.         {
  171.             int[] data = timeString
  172.                 .Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)
  173.                 .Select(int.Parse)
  174.                 .ToArray();
  175.             // Time is calculated in seconds
  176.             return data[0] * 60 + data[1];
  177.         }
  178.  
  179.         private static void prepareOutput(int[] timesToEscape, int maxT)
  180.         {
  181.             for (int room = 0; room < timesToEscape.Length; room++)
  182.             {
  183.                 int time = timesToEscape[room];
  184.  
  185.                 if (time == -1)
  186.                 {
  187.                     continue;
  188.                 }
  189.  
  190.                 if (time == int.MaxValue)
  191.                 {
  192.                     Console.WriteLine($"Unreachable {room} (N/A)");
  193.                     continue;
  194.                 }
  195.  
  196.                 if (time <= maxT)
  197.                 {
  198.                     Console.WriteLine($"Safe {room} ({convertTime(time)})");
  199.                 }
  200.                 else
  201.                 {
  202.                     Console.WriteLine($"Unsafe {room} ({convertTime(time)})");
  203.                 }
  204.             }
  205.         }
  206.     }
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement