using System; using System.Collections.Generic; using System.Linq; using Graph; namespace TSP { public class Tour where T : IComparable { public List> Cities { get; set; } = new List>(); public T Weight { get { dynamic _weight = default(T); Cities.Where(city => city.Parent != -1).ToList() .ForEach(city => _weight += city.Edges .Where(edge => edge.V == city.Parent) .First().Weight); return _weight; } } // indexer: get vertex where vertex.Id == index public Vertex this[int index] { get => Cities.Find(vertex => vertex.Id == index); } public Tour(List> cities) => Cities.AddRange(cities.Select(city => new Vertex(city))); public Tour() => Cities = new List>(); public Tour(Tour tour) : this(tour.Cities) { } public override string ToString() => $"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" + $"distance: {Weight}"; } }