Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Пожарная_охрана
- {
- class Program
- {
- static void Main(string[] args)
- {
- Graph g;
- using (var sr = new StreamReader("input.txt"))
- {
- int countBunkers, countExits, countTunnels;
- List<int> exits = new List<int>();
- List<List<int>> tunnels = new List<List<int>>();
- countBunkers = int.Parse(sr.ReadLine());
- for (int i = 0; i < countBunkers; i++)
- {
- tunnels.Add(new List<int>());
- }
- countExits = int.Parse(sr.ReadLine());
- string[] str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < countExits; i++)
- {
- exits.Add(int.Parse(str[i])-1);
- }
- countTunnels = int.Parse(sr.ReadLine());
- for (int i = 0; i < countTunnels; i++)
- {
- str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- tunnels[int.Parse(str[0]) - 1].Add(int.Parse(str[1]) - 1);
- tunnels[int.Parse(str[1]) - 1].Add(int.Parse(str[0]) - 1);
- }
- g = new Graph(countBunkers, countTunnels, exits, tunnels);
- }
- int[] minDistance = SearchPath(g);
- //for (int i = 0; i < g.countPoints; i++)
- //{
- // for (int j = 0; j < g.countPoints; j++)
- // {
- // Console.Write(g.matrAdjacency[i, j] + " ");
- // }
- // Console.WriteLine();
- //}
- //for (int i = 0; i < g.countPoints; i++)
- //{
- // minDistance[i] = int.MaxValue;
- //}
- //foreach (int exits in g.specificPoints)
- //{
- // int[] temp = SearchPath(g, exits);
- // for (int i = 0; i < g.countPoints; i++)
- // {
- // if (temp[i] < minDistance[i])
- // {
- // minDistance[i] = temp[i];
- // }
- // }
- //}
- using (var sw = new StreamWriter("output.txt"))
- {
- foreach (int dist in minDistance)
- {
- sw.Write(dist + " ");
- }
- }
- }
- public static int[] SearchPath(Graph g)
- {
- int[] minDistToPoint = new int[g.countPoints];
- for (int i = 0; i < g.countPoints; i++)
- {
- minDistToPoint[i] = -1;
- }
- Queue<int> pathToPoint = new Queue<int>();
- for (int j = 0; j < g.specificPoints.Count; j++)
- {
- pathToPoint.Enqueue(g.specificPoints[j]);
- minDistToPoint[g.specificPoints[j]] = 0;
- }
- while (pathToPoint.Count != 0)
- {
- int currentPoint = pathToPoint.Peek();
- pathToPoint.Dequeue();
- for (int k = 0; k < g.listPoints[currentPoint].Count; k++)
- {
- if (minDistToPoint[g.listPoints[currentPoint][k]] == -1)
- {
- pathToPoint.Enqueue(g.listPoints[currentPoint][k]);
- minDistToPoint[g.listPoints[currentPoint][k]] = minDistToPoint[currentPoint] + 1;
- }
- }
- }
- return minDistToPoint;
- }
- //public static int[] SearchPath(Graph g, int exitsPoint)
- //{
- // int[] minPath = new int[g.countPoints];
- // int[] distToPoints = new int[g.countPoints];
- // int[,] newMatrixAdjacency = new int[g.countPoints, g.countPoints];
- // for (int i = 0; i < g.countPoints; i++)
- // {
- // for (int j = 0; j < g.countPoints; j++)
- // {
- // newMatrixAdjacency[i, j] = g.matrAdjacency[i, j];
- // if (g.matrAdjacency[i, j] == 0)
- // {
- // newMatrixAdjacency[i, j] = int.MaxValue;
- // }
- // }
- // }
- // HashSet<int> setOfPoints = new HashSet<int>();
- // for (int i = 0; i < g.countPoints; i++)
- // {
- // setOfPoints.Add(i);
- // distToPoints[i] = newMatrixAdjacency[exitsPoint - 1, i];
- // if (int.MaxValue > distToPoints[i])
- // {
- // minPath[i] = exitsPoint - 1;
- // }
- // }
- // distToPoints[exitsPoint - 1] = 0;
- // minPath[exitsPoint - 1] = -1;
- // setOfPoints.Remove(exitsPoint - 1);
- // while (setOfPoints.Count != 0)
- // {
- // int minDistance = int.MaxValue;
- // int minPoints = -1;
- // foreach (int point in setOfPoints)
- // {
- // if (distToPoints[point] < minDistance)
- // {
- // minDistance = distToPoints[point];
- // minPoints = point;
- // }
- // }
- // if (minPoints != -1)
- // {
- // setOfPoints.Remove(minPoints);
- // }
- // foreach (int point in setOfPoints)
- // {
- // if (newMatrixAdjacency[minPoints, point] < int.MaxValue)
- // {
- // distToPoints[point] = Math.Min(distToPoints[minPoints] + newMatrixAdjacency[minPoints, point], distToPoints[point]);
- // if (distToPoints[minPoints] + newMatrixAdjacency[minPoints, point] == distToPoints[point])
- // {
- // minPath[point] = minPoints;
- // }
- // }
- // }
- // }
- // return distToPoints;
- //}
- }
- public class Graph
- {
- private int __countPoints;
- public int countPoints
- {
- get { return __countPoints; }
- set
- {
- if (value < 1)
- {
- throw new ArgumentException("Неверный ввод данных");
- }
- else __countPoints = value;
- }
- }
- private int __countEdges;
- public int countEdges
- {
- get { return __countEdges; }
- set
- {
- if (value < 0)
- {
- throw new ArgumentException("Неверный ввод данных");
- }
- else __countEdges = value;
- }
- }
- public List<List<int>> listPoints = new List<List<int>>();
- public List<int> specificPoints = new List<int>();
- //public int[,] matrAdjacency;
- public Graph(int countPoints, int countEdges, List<int> specificPoints, List<List<int>> listPoints)
- {
- this.countPoints = countPoints;
- this.countEdges = countEdges;
- this.specificPoints = specificPoints;
- this.listPoints = listPoints;
- //matrAdjacency = new int[countPoints, countPoints];
- //for (int i = 0; i < countPoints; i++)
- //{
- // for (int k = 0; k < listPoints[i].Count; k++)
- // {
- // matrAdjacency[i, listPoints[i][k]] = 1;
- // }
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement