Question: #include #include #include #include #include #include #include #include using namespace std; struct Place { int index; string name; double x , y; } ; struct
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Place
int index;
string name;
double x y;
;
struct Edge
int from, to;
double weight;
bool operatorconst Edge& other const
return weight other.weight;
;
double calculateDistanceconst Place& a const Place& b
return sqrtpowax bx poway by;
vector splitconst string &s char delimiter
vector tokens;
string token;
istringstream tokenStreams;
while getlinetokenStream token, delimiter
tokens.pushbacktoken;
return tokens;
vector readPlacesFromFileconst string& filename
ifstream filefilename;
vector places;
string line;
int index ;
while getlinefile line
vector tokens splitline;
if tokenssize
Place place;
place.index index;
place.name tokens;
place.x stodtokens;
place.y stodtokens;
places.pushbackplace;
return places;
void printPlacesconst vector& places
for const auto& place : places
cout "Index: place.index Name: place.name X: place.x Y: place.y endl;
vector tspBruteForceconst vector& places
Your implementation goes here
vector tspPrimApproximationconst vector& places
your implementation goes here
int main
string filename;
vector places;
ifstream file;
cout "Enter the filename: ;
cin filename;
file.openfilename;
if file
cout "File not found. Please try again." endl;
return ;
string line;
int index ;
while getlinefile line
vector tokens splitline;
if tokenssize
Place place;
place.index index;
place.name tokens;
place.x stodtokens;
place.y stodtokens;
places.pushbackplace;
file.close;
printPlacesplaces;
cin.ignorenumericlimits::max
;
int startIndex;
cout "Enter the index of the starting city: ;
cin startIndex;
if startIndex startIndex places.size
cout "Invalid index for starting city." endl;
return ;
cin.ignorenumericlimits::max
;
string indicesList;
cout "Enter the indices of the cities you wish to visit, separated by commas do not include the starting city: ;
getlinecin indicesList;
vector indicesToVisit splitindicesList;
unorderedset selectedIndices;
vector selectedPlaces;
selectedIndices.insertstartIndex;
cout "Starting City: placesstartIndexname endl;
for const auto& indexStr : indicesToVisit
int index stoiindexStr;
if index startIndex
cout "Note: The starting city Index: startIndex was included in the visit list and will be ignored." endl;
continue;
if index index places.size
cout "Invalid index: index This city will be ignored." endl;
continue;
if selectedIndicescountindex
cout "Duplicate index: index This city will be ignored." endl;
continue;
selectedIndices.insertindex;
selectedPlaces.pushbackplacesindex;
cout "Cities to Visit:" endl;
for const auto& place : selectedPlaces
cout "Index: place.index Name: place.name X: place.x Y: place.y endl;
Modify main below this point to print out the paths traveled for both TSP brute and approx
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
