Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create travelling_salesman.cpp #7094

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions travelling_salesman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Solving issue #7095
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

struct Point {
int x, y;
};

double distance(Point a, Point b) {
int dx = a.x - b.x;
int dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}

double totalDistance(const std::vector<int>& path, const std::vector<Point>& cities) {
double dist = 0.0;
for (size_t i = 0; i < path.size() - 1; ++i) {
dist += distance(cities[path[i]], cities[path[i + 1]]);
}
dist += distance(cities[path.back()], cities[path[0]]);
return dist;
}

void printPath(const std::vector<int>& path) {
for (int city : path) {
std::cout << city << " -> ";
}
std::cout << path[0] << std::endl;
}

int main() {
int n; // Number of cities
std::cout << "Enter the number of cities: ";
std::cin >> n;

std::vector<Point> cities(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter the coordinates of city " << i << " (x y): ";
std::cin >> cities[i].x >> cities[i].y;
}

std::vector<int> path(n);
for (int i = 0; i < n; ++i) {
path[i] = i;
}

double minDistance = std::numeric_limits<double>::max();
std::vector<int> bestPath;

do {
double dist = totalDistance(path, cities);
if (dist < minDistance) {
minDistance = dist;
bestPath = path;
}
} while (std::next_permutation(path.begin() + 1, path.end()));

std::cout << "Shortest Path: ";
printPath(bestPath);
std::cout << "Shortest Distance: " << minDistance << std::endl;

return 0;
}