#include #include #include #include #include #include #include #include #include #include #include using namespace std; // - a v definicii 'SearchData' je pouzite 'City' struct RoadTo; struct City; // Udaje pouzite v algoritmoch hladania struct SearchData { // Mozete si doplnit dalsie atributy a metody, ktore pouzijete v algoritmoch hladania bool discovered; unsigned distance; const City *previous; bool foundShortestPath; void clear() { // nastavi na implicitne hodnoty (tuto metodu mozete upravit (ale nie jej deklaracnu cast - nedoplnajte parametre)) discovered = false; distance = numeric_limits::max(); previous = nullptr; foundShortestPath = false; } }; struct City { string name; // nazov obce list roads; // zoznam "priamych" jednosmernych cestnych spojeni do dalsich obci (zoznam vystupnych hran z tohto uzla grafu) SearchData searchData; // udaje pouzite v algoritmoch hladania // Mozete doplnit dalsi konstruktor alebo metody, ale tento konstruktor nemente explicit City(string name) : name(move(name)) { searchData.clear(); }; }; // Jednosmerne "priame" cestne spojenie do dalsej obce (orientovana, ohodnotena hrana grafu) struct RoadTo { City *city; // obec, do ktorej je toto "priame" cestne spojenie unsigned length; // dlzka tohto "priameho" spojenia // Mozete doplnit dalsi konstruktor alebo metody, ale tento konstruktor nemente RoadTo(City *city, unsigned length) : city(city) , length(length) { } }; // Cestna mapa planety (orientovany, ohodnoteny graf) struct Planet { list cities; // zoznam obci na planete (zoznam vrcholov grafu) void clearSearchData() { // inicializuje atributy pouzite v algoritme hladania for(City &c : cities) { c.searchData.clear(); } } }; // Vyminka v pripade neexistencie obce so zadanym nazvom class CityNotExistsException : exception { string cityName; // nazov obce public: explicit CityNotExistsException(string name) : cityName(move(name)) { } const char * what() const noexcept override { // vrati nazov neexistujucej obce return cityName.c_str(); } }; //------------------------------------------------------------------------------------------------- // 10. ULOHA (0.4 bodu) //------------------------------------------------------------------------------------------------- /* Funkcia najte dlzky najkratsich ciest z obce 'startCity' do vsetkych dosiahnutelnych obci. Pouzite Dijkstrov algoritmus. VSTUPNE HODNOTY: [in] planet - cestna siet na planete [in] startCity - nazov obce NAVRATOVA HODNOTA: Pre kazdu obec dosiahnutelnu zo 'startCity' (na planete 'planet') obsahuje dlzku najkratsej cesty zo 'startCity'. VYNIMKA: CityNotExistsException - ak na planete 'planet' neexistuje obec s nazvom 'startCity'. Metoda 'CityNotExistsException::what()' vrati nazov neexistujucej obce (hodnota 'startCity'). VYSTUPNA PODMIENKA: Navratova hodnota obsahuje nazvy len tych miest, ktore su dosiahnutelne zo 'startCity'. Navratova hodnota obsahuje aj vzdialenost do 'startCity' (nula). PRIKLAD: obrazok na webe POZNAMKA: Ak v implementacii pouziteje 'City::searchData', tak bude pravdepodobne potrebne na zaciatku zavolat 'planet->clearSearchData()', aj aby bolo osetrene viacnasobne volanie algoritmu s tym istym objektom 'planet'. */ City miniDist(Planet *planet) // finding minimum distance { std::cout<<"vstup do funkcie miniDist"<cities.front(); for(auto &city: planet->cities) { std::cout<<"vo funkcii skumane mesto je"<cities.begin(),planet->cities.end(),[&startCity](City& c){ return startCity == c.name; }); std::cout<<"mesto je"<name<< "s distance"<< curr->searchData.distance<cities) { city.searchData.distance; city.searchData.discovered = false; // city.searchData.previous = nullptr; } curr->searchData.distance = 0; std::cout<<"mesto je po zadani distancu 0"<name<< "s distance"<< curr->searchData.distance<cities) { *curr = miniDist(planet); std::cout<<"curent je"<name<searchData.discovered=true; for (auto &RoadTo: curr->roads) { if(curr->searchData.discovered &&curr->searchData.distance!=2147483647 && curr->searchData.distance+RoadTo.length< RoadTo.city->searchData.distance){ RoadTo.city->searchData.distance = curr->searchData.distance+RoadTo.length; std::cout<<"cesta z "<name<<"do RoadTO.city "<name<< "s distance "<< RoadTo.city->searchData.distance<cities = {London, Haron, Pecelon, Nicudon, Bailon, Tigron, Etalon}; dijkstra(pl,"London"); for (auto &what: pl->cities) { std::cout<cities.front().roads.front().city->searchData.distance<