Advertisement
StreetKatya

Пожарная охрана

Apr 11th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Пожарная_охрана
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Graph g;
  15.             using (var sr = new StreamReader("input.txt"))
  16.             {
  17.                 int countBunkers, countExits, countTunnels;
  18.                 List<int> exits = new List<int>();
  19.                 List<List<int>> tunnels = new List<List<int>>();
  20.  
  21.                 countBunkers = int.Parse(sr.ReadLine());
  22.                 for (int i = 0; i < countBunkers; i++)
  23.                 {
  24.                     tunnels.Add(new List<int>());
  25.                 }
  26.  
  27.                 countExits = int.Parse(sr.ReadLine());
  28.                 string[] str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  29.                 for (int i = 0; i < countExits; i++)
  30.                 {
  31.                     exits.Add(int.Parse(str[i])-1);
  32.                 }
  33.  
  34.                 countTunnels = int.Parse(sr.ReadLine());
  35.                 for (int i = 0; i < countTunnels; i++)
  36.                 {
  37.                     str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  38.                     tunnels[int.Parse(str[0]) - 1].Add(int.Parse(str[1]) - 1);
  39.                     tunnels[int.Parse(str[1]) - 1].Add(int.Parse(str[0]) - 1);
  40.                 }
  41.  
  42.                 g = new Graph(countBunkers, countTunnels, exits, tunnels);
  43.             }
  44.  
  45.             int[] minDistance = SearchPath(g);
  46.             //for (int i = 0; i < g.countPoints; i++)
  47.             //{
  48.             //    for (int j = 0; j < g.countPoints; j++)
  49.             //    {
  50.             //        Console.Write(g.matrAdjacency[i, j] + " ");
  51.             //    }
  52.             //    Console.WriteLine();
  53.             //}
  54.             //for (int i = 0; i < g.countPoints; i++)
  55.             //{
  56.             //    minDistance[i] = int.MaxValue;
  57.             //}
  58.             //foreach (int exits in g.specificPoints)
  59.             //{
  60.             //    int[] temp = SearchPath(g, exits);
  61.             //    for (int i = 0; i < g.countPoints; i++)
  62.             //    {
  63.             //        if (temp[i] < minDistance[i])
  64.             //        {
  65.             //            minDistance[i] = temp[i];
  66.             //        }
  67.             //    }
  68.             //}
  69.             using (var sw = new StreamWriter("output.txt"))
  70.             {
  71.                 foreach (int dist in minDistance)
  72.                 {
  73.                     sw.Write(dist + " ");
  74.                 }
  75.             }
  76.         }
  77.         public static int[] SearchPath(Graph g)
  78.         {
  79.             int[] minDistToPoint = new int[g.countPoints];
  80.             for (int i = 0; i < g.countPoints; i++)
  81.             {
  82.                 minDistToPoint[i] = -1;
  83.             }
  84.  
  85.             Queue<int> pathToPoint = new Queue<int>();
  86.             for (int j = 0; j < g.specificPoints.Count; j++)
  87.             {
  88.                 pathToPoint.Enqueue(g.specificPoints[j]);
  89.                 minDistToPoint[g.specificPoints[j]] = 0;
  90.             }
  91.  
  92.             while (pathToPoint.Count != 0)
  93.             {
  94.                 int currentPoint = pathToPoint.Peek();
  95.                 pathToPoint.Dequeue();
  96.  
  97.                 for (int k = 0; k < g.listPoints[currentPoint].Count; k++)
  98.                 {
  99.                     if (minDistToPoint[g.listPoints[currentPoint][k]] == -1)
  100.                     {
  101.                         pathToPoint.Enqueue(g.listPoints[currentPoint][k]);
  102.                         minDistToPoint[g.listPoints[currentPoint][k]] = minDistToPoint[currentPoint] + 1;
  103.                     }
  104.                 }
  105.             }
  106.             return minDistToPoint;
  107.         }
  108.         //public static int[] SearchPath(Graph g, int exitsPoint)
  109.         //{
  110.         //    int[] minPath = new int[g.countPoints];
  111.         //    int[] distToPoints = new int[g.countPoints];
  112.  
  113.         //    int[,] newMatrixAdjacency = new int[g.countPoints, g.countPoints];
  114.         //    for (int i = 0; i < g.countPoints; i++)
  115.         //    {
  116.         //        for (int j = 0; j < g.countPoints; j++)
  117.         //        {
  118.         //            newMatrixAdjacency[i, j] = g.matrAdjacency[i, j];
  119.         //            if (g.matrAdjacency[i, j] == 0)
  120.         //            {
  121.         //                newMatrixAdjacency[i, j] = int.MaxValue;
  122.         //            }
  123.         //        }
  124.         //    }
  125.  
  126.         //    HashSet<int> setOfPoints = new HashSet<int>();
  127.         //    for (int i = 0; i < g.countPoints; i++)
  128.         //    {
  129.         //        setOfPoints.Add(i);
  130.         //        distToPoints[i] = newMatrixAdjacency[exitsPoint - 1, i];
  131.  
  132.         //        if (int.MaxValue > distToPoints[i])
  133.         //        {
  134.         //            minPath[i] = exitsPoint - 1;
  135.         //        }
  136.         //    }
  137.  
  138.  
  139.         //    distToPoints[exitsPoint - 1] = 0;
  140.         //    minPath[exitsPoint - 1] = -1;
  141.         //    setOfPoints.Remove(exitsPoint - 1);
  142.         //    while (setOfPoints.Count != 0)
  143.         //    {
  144.         //        int minDistance = int.MaxValue;
  145.         //        int minPoints = -1;
  146.         //        foreach (int point in setOfPoints)
  147.         //        {
  148.         //            if (distToPoints[point] < minDistance)
  149.         //            {
  150.         //                minDistance = distToPoints[point];
  151.         //                minPoints = point;
  152.         //            }
  153.         //        }
  154.         //        if (minPoints != -1)
  155.         //        {
  156.         //            setOfPoints.Remove(minPoints);
  157.         //        }
  158.         //        foreach (int point in setOfPoints)
  159.         //        {
  160.         //            if (newMatrixAdjacency[minPoints, point] < int.MaxValue)
  161.         //            {
  162.         //                distToPoints[point] = Math.Min(distToPoints[minPoints] + newMatrixAdjacency[minPoints, point], distToPoints[point]);
  163.         //                if (distToPoints[minPoints] + newMatrixAdjacency[minPoints, point] == distToPoints[point])
  164.         //                {
  165.         //                    minPath[point] = minPoints;
  166.         //                }
  167.         //            }
  168.         //        }
  169.         //    }
  170.         //    return distToPoints;
  171.         //}
  172.     }
  173.     public class Graph
  174.     {
  175.         private int __countPoints;
  176.         public int countPoints
  177.         {
  178.             get { return __countPoints; }
  179.             set
  180.             {
  181.                 if (value < 1)
  182.                 {
  183.                     throw new ArgumentException("Неверный ввод данных");
  184.                 }
  185.                 else __countPoints = value;
  186.             }
  187.         }
  188.         private int __countEdges;
  189.         public int countEdges
  190.         {
  191.             get { return __countEdges; }
  192.             set
  193.             {
  194.                 if (value < 0)
  195.                 {
  196.                     throw new ArgumentException("Неверный ввод данных");
  197.                 }
  198.                 else __countEdges = value;
  199.             }
  200.         }
  201.         public List<List<int>> listPoints = new List<List<int>>();
  202.         public List<int> specificPoints = new List<int>();
  203.         //public int[,] matrAdjacency;
  204.         public Graph(int countPoints, int countEdges, List<int> specificPoints, List<List<int>> listPoints)
  205.         {
  206.             this.countPoints = countPoints;
  207.             this.countEdges = countEdges;
  208.             this.specificPoints = specificPoints;
  209.             this.listPoints = listPoints;
  210.             //matrAdjacency = new int[countPoints, countPoints];
  211.             //for (int i = 0; i < countPoints; i++)
  212.             //{
  213.             //    for (int k = 0; k < listPoints[i].Count; k++)
  214.             //    {
  215.             //        matrAdjacency[i, listPoints[i][k]] = 1;
  216.             //    }
  217.             //}
  218.         }
  219.     }
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement