Advertisement
myname0

graph_III_1

May 19th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace graph_1
  9. {
  10.     public class Graph
  11.     {
  12.         private class Node
  13.         {
  14.             private int[,] array;
  15.             private bool[] nov;
  16.             private string[] names;
  17.  
  18.             public int this[int i, int j]
  19.             {
  20.                 get { return array[i, j]; }
  21.                 set { array[i, j] = value; }
  22.             }
  23.  
  24.             public string this[int i]
  25.             {
  26.                 get { return names[i]; }
  27.             }
  28.  
  29.             public int Size
  30.             {
  31.                 get { return array.GetLength(0); }
  32.             }
  33.  
  34.             public void NovSet()
  35.             {
  36.                 for (int i = 0; i < Size; i++)
  37.                     nov[i] = true;
  38.             }
  39.  
  40.             public Node(int[,] tmp, string[] names)
  41.             {
  42.                 array = tmp;
  43.                 nov = new bool[tmp.GetLength(0)];
  44.                 this.names = names;
  45.             }
  46.  
  47.             public int[,] SearchShortestWayByCities(string A, string B, string[] cities)
  48.             {
  49.                 int indexA = 0;
  50.                 int indexB = 0;
  51.                 List<int> indexofcities = new List<int>();
  52.                 for (int i = 0; i < names.Length; i++)
  53.                 {
  54.                     if (names[i] == A)
  55.                     {
  56.                         indexA = i;
  57.                     }
  58.                     else if (names[i] == B)
  59.                     {
  60.                         indexB = i;
  61.                     }
  62.                     else for (int j = 0; j < cities.Length; j++)
  63.                             if (names[i] == cities[j])
  64.                                 indexofcities.Add(i);
  65.                 }
  66.  
  67.                 int[,] newarray = new int[Size, Size];
  68.                 bool flag = true;
  69.                 for (int i = 0; i < Size; i++)
  70.                     for (int j = 0; j < Size; j++)
  71.                     {
  72.                         flag = true;
  73.                         for (int k = 0; k < indexofcities.Count; k++)
  74.                             if (i == indexofcities[k] || j == indexofcities[k])
  75.                             {
  76.                                 newarray[i, j] = array[i, j];
  77.                                 flag = false;
  78.                             }
  79.                         if (flag) newarray[i, j] = 0;
  80.                     }            
  81.                 return newarray;
  82.             }
  83.  
  84.             //реализация алгоритма Флойда
  85.             public long[,] Floyd(out int[,] p, int[,] tmp)
  86.             {
  87.                 int i, j, k;
  88.                 long[,] a = new long[Size, Size];
  89.                 p = new int[Size, Size];
  90.  
  91.                 for (i = 0; i < Size; i++)
  92.                     for (j = 0; j < Size; j++)
  93.                     {
  94.                         if (i == j) a[i, j] = 0;
  95.                         else
  96.                         {
  97.                             if (tmp[i, j] == 0)
  98.                                 a[i, j] = int.MaxValue;
  99.                             else a[i, j] = tmp[i, j];
  100.                         }
  101.                         p[i, j] = -1;
  102.                     }
  103.  
  104.                 for (k = 0; k < Size; k++)
  105.                     for (i = 0; i < Size; i++)
  106.                         for (j = 0; j < Size; j++)
  107.                         {
  108.                             long distance = a[i, k] + a[k, j];
  109.                             if (a[i, j] > distance)
  110.                             {
  111.                                 a[i, j] = distance;
  112.                                 p[i, j] = k;
  113.                             }
  114.                         }
  115.  
  116.                 return a;//в качестве результата возвращается массив кратчайших путей между
  117.             }
  118.            
  119.             public void WayFloyd(int a, int b, int[,] p, ref Queue<int> items)
  120.             {
  121.                 int k = p[a, b];
  122.                 if (k != -1)
  123.                 {
  124.                    
  125.                     WayFloyd(a, k, p, ref items);
  126.                     items.Enqueue(k);                
  127.                     WayFloyd(k, b, p, ref items);
  128.                 }
  129.             }
  130.         }
  131.         private Node graph;
  132.  
  133.         public Graph(string nameOfFile)
  134.         {
  135.             StreamReader fin = new StreamReader(nameOfFile);
  136.             int n = int.Parse(fin.ReadLine());
  137.             string[] names = fin.ReadLine().Split(' ');
  138.             int[,] tmp = new int[n, n];
  139.             for (int i = 0; i < n; i++)
  140.             {
  141.                 string[] arr = fin.ReadLine().Split(' ');
  142.                 for (int j = 0; j < n; j++)
  143.                     tmp[i, j] = int.Parse(arr[j]);
  144.             }
  145.             graph = new Node(tmp, names);
  146.         }
  147.  
  148.         public void SearchShortestWayByCities()
  149.         {
  150.             int [,]p;
  151.             Console.WriteLine("Введите название города, из кторого необходимо найти кратчайший путь: ");
  152.             string a = Console.ReadLine();
  153.             Console.WriteLine("Введите название города, в который необходимо найти кратчайший путь: ");
  154.             string b = Console.ReadLine();
  155.            
  156.             Console.WriteLine("Введите названия городов, через которые необходимо проложить путь: ");
  157.             string [] cities = Console.ReadLine().Split(' ');
  158.             int[,] newarray = graph.SearchShortestWayByCities(a, b, cities);
  159.             long [,] tmp = graph.Floyd(out p, newarray);
  160.          
  161.             int indexA = 0;
  162.             int indexB = 0;
  163.             for (int i = 0; i < graph.Size; i++)
  164.             {
  165.                 if (graph[i] == a)
  166.                     indexA = i;
  167.                 else if (graph[i] == b)
  168.                     indexB = i;
  169.             }
  170.  
  171.             if (tmp[indexA,indexB]==int.MaxValue)
  172.                 Console.WriteLine("Пути из вершины {0} в вершину {1} не существует", graph[indexA], graph[indexB]);
  173.             else
  174.             {
  175.                 Console.WriteLine("Кратчайший путь от вершины {0} до вершины {1} равен {2}, ", graph[indexA], graph[indexB], tmp[indexA, indexB]);
  176.                
  177.                 Console.Write("Путь: ");
  178.                 Queue<int> items = new Queue<int>();
  179.                 items.Enqueue(indexA);
  180.                 graph.WayFloyd(indexA, indexB, p, ref items);
  181.                 items.Enqueue(indexB);
  182.                 while (items.Count != 0)
  183.                     Console.Write("{0} ", graph[items.Dequeue()]);
  184.                 Console.WriteLine ();
  185.             }
  186.         }        
  187.     }
  188.    
  189.  
  190.  
  191.     class Program
  192.     {
  193.         static void Main(string[] args)
  194.         {
  195.             Graph graph1 = new Graph("input.txt");
  196.             graph1.SearchShortestWayByCities();
  197.         }
  198.     }
  199. }
  200.  
  201. //input
  202. 5
  203. Березовка Еремеевка Октябрьское Рузаевка Сосновка
  204. 0 70 0 0 20
  205. 70 0 75 25 15
  206. 0 75 0 40 60
  207. 0 25 40 0 0
  208. 20 15 60 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement