Ejemplo n.º 1
0
        //  find meet point
        private void FindMeetPoint(int bestHouse)
        {
            //  create instance of Meet Point
            _meetPoint = new MeetPoint();

            _meetPoint.FromHouse1ToPoint = 0;
            _meetPoint.House1            = bestHouse;
            _meetPoint.House2            = bestHouse;

            //  difference between the biggest and the smalest way
            int delta = 0;

            //  summ of all ways
            int summ = 0;

            //  the biggest and the smallest numbers in the route
            int biggest  = 0;
            int smallest = Infinity;

            for (int i = 0; i < _housesAmount; i++)
            {
                summ += _routes[bestHouse, i];

                if (_routes[bestHouse, i] > biggest)
                {
                    biggest = _routes[bestHouse, i];
                }

                if (smallest > _routes[bestHouse, i])
                {
                    smallest = _routes[bestHouse, i];
                }
            }

            //  claculate delta for this route
            delta = biggest - smallest;


            //  iterates each of roads and put point onto the road
            //  find the smallest routes to all houses
            //  find summ and delta
            //  if summ <= newSumm and delta < newDelta
            //  this route is better to meet
            //  feel the _meetPoint and continue interations

            //  roads array
            Road[] roads = _roads.ToArray();

            //  new recalculated route
            //  from our point to the all houses
            int[] route = new int[_housesAmount];

            //  current point
            MeetPoint point = new MeetPoint();

            for (int i = 0; i < roads.Length; i++)
            {
                Road tmp = new Road();
                tmp = roads[i];

                //  iterates by the road
                for (int j = 1; j < tmp.Length; j++)
                {
                    //  initialize point
                    point.House1            = tmp.House1 - 1;
                    point.House2            = tmp.House2 - 1;
                    point.Length            = tmp.Length;
                    point.FromHouse1ToPoint = point.Length - j;

                    //  distances from current point
                    //  to all houses
                    int[] distances = new int[_housesAmount];

                    //  set all to 0
                    for (int x = 0; x < _housesAmount; x++)
                    {
                        distances[x] = 0;
                    }

                    distances[point.House1] = point.FromHouse1ToPoint;
                    distances[point.House2] = point.Length - point.FromHouse1ToPoint;

                    //  initialize distances
                    for (int k = 0; k < distances.Length; k++)
                    {
                        if (k != point.House1 && k != point.House2)
                        {
                            //  distance from houses to k
                            int viaHouse1 = _routes[point.House1, k];
                            int viaHouse2 = _routes[point.House2, k];

                            //  find wich route is better
                            if (viaHouse1 >= viaHouse2)
                            {
                                distances[k] = point.Length - point.FromHouse1ToPoint + viaHouse2;
                            }
                            else
                            {
                                distances[k] = point.FromHouse1ToPoint + viaHouse1;
                            }
                        }
                    }

                    //  calculate summ and delta
                    int newSumm  = 0;
                    int newDelta = 0;

                    //  the biggest and the smallest numbers in the route
                    int newBiggest  = 0;
                    int newSmallest = Infinity;

                    for (int x = 0; x < _housesAmount; x++)
                    {
                        newSumm += _routes[bestHouse, x];

                        if (distances[x] > newBiggest)
                        {
                            newBiggest = distances[x];
                        }

                        if (newSmallest > distances[x])
                        {
                            newSmallest = distances[x];
                        }
                    }

                    //  claculate delta for this route
                    newDelta = newBiggest - newSmallest;

                    //  if the route we have found is better
                    //  swap
                    if (summ >= newSumm && delta > newDelta)
                    {
                        summ  = newSumm;
                        delta = newDelta;

                        _meetPoint = point;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //  найти точку встречи
        private void FindMeetPoint(int bestHouse)
        {
            _meetPoint = new MeetPoint();

            _meetPoint.FromHouse1ToPoint = 0;
            _meetPoint.House1            = bestHouse;
            _meetPoint.House2            = bestHouse;

            //  разница между самым большим и наименьшим путем
            int delta = 0;

            //  сумма всех путей
            int summ = 0;

            //  самые большие и наименьшие числа на маршруте
            int biggest  = 0;
            int smallest = Infinity;

            for (int i = 0; i < _housesAmount; i++)
            {
                summ += _routes[bestHouse, i];

                if (_routes[bestHouse, i] > biggest)
                {
                    biggest = _routes[bestHouse, i];
                }

                if (smallest > _routes[bestHouse, i])
                {
                    smallest = _routes[bestHouse, i];
                }
            }

            //  вычислить дельту для этого маршрута
            delta = biggest - smallest;


            // Итерирует каждую из дорог и ставит точку на дорогу
            // найти наименьшие маршруты для всех домов
            // находим summ и delta
            // если summ <= newSumm и delta <newDelta
            // Этот маршрут лучшй для встречи
            // заполняем _meetPoint и продолжаем взаимодействие

            //  массив дорог
            Road[] roads = _roads.ToArray();

            // новый пересчитанный маршрут
            // от нашей точки до всех домов
            int[] route = new int[_housesAmount];

            //  текущая точка
            MeetPoint point = new MeetPoint();

            for (int i = 0; i < roads.Length; i++)
            {
                Road tmp = new Road();
                tmp = roads[i];


                for (int j = 1; j < tmp.Length; j++)
                {
                    point.House1            = tmp.House1 - 1;
                    point.House2            = tmp.House2 - 1;
                    point.Length            = tmp.Length;
                    point.FromHouse1ToPoint = point.Length - j;

                    // расстояния от текущей точки ко всем домам
                    int[] distances = new int[_housesAmount];

                    //  устанавливаем все на 0
                    for (int x = 0; x < _housesAmount; x++)
                    {
                        distances[x] = 0;
                    }

                    distances[point.House1] = point.FromHouse1ToPoint;
                    distances[point.House2] = point.Length - point.FromHouse1ToPoint;

                    //  инициализируем расстояния
                    for (int k = 0; k < distances.Length; k++)
                    {
                        if (k != point.House1 && k != point.House2)
                        {
                            //  расстояние от домов до k
                            int viaHouse1 = _routes[point.House1, k];
                            int viaHouse2 = _routes[point.House2, k];

                            //  находим лучший маршрут
                            if (viaHouse1 >= viaHouse2)
                            {
                                distances[k] = point.Length - point.FromHouse1ToPoint + viaHouse2;
                            }
                            else
                            {
                                distances[k] = point.FromHouse1ToPoint + viaHouse1;
                            }
                        }
                    }


                    int newSumm  = 0;
                    int newDelta = 0;

                    int newBiggest  = 0;
                    int newSmallest = Infinity;

                    for (int x = 0; x < _housesAmount; x++)
                    {
                        newSumm += _routes[bestHouse, x];

                        if (distances[x] > newBiggest)
                        {
                            newBiggest = distances[x];
                        }

                        if (newSmallest > distances[x])
                        {
                            newSmallest = distances[x];
                        }
                    }

                    //  вычислить дельту для этого маршрута
                    newDelta = newBiggest - newSmallest;

                    // если найденный маршрут лучше, то заменяем
                    if (summ >= newSumm && delta > newDelta)
                    {
                        summ  = newSumm;
                        delta = newDelta;

                        _meetPoint = point;
                    }
                }
            }
        }