-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRani_exp9.cpp
52 lines (42 loc) · 1.16 KB
/
Rani_exp9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int calculateDistance(vector<vector<int>>& graph, vector<int>& path) {
int distance = 0;
int n = graph.size();
for (int i = 0; i < n - 1; ++i) {
int src = path[i];
int dest = path[i + 1];
distance += graph[src][dest];
}
distance += graph[path[n - 1]][path[0]];
return distance;
}
int tspNaive(vector<vector<int>>& graph, int start) {
int n = graph.size();
vector<int> path(n);
for (int i = 0; i < n; ++i)
path[i] = i;
int minDistance = INT_MAX;
do {
if (path[0] == start) {
int distance = calculateDistance(graph, path);
minDistance = min(minDistance, distance);
}
} while (next_permutation(path.begin() + 1, path.end()));
return minDistance;
}
int main() {
vector<vector<int>> graph = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
int startCity = 0;
int minDistance = tspNaive(graph, startCity);
cout << "Minimum distance: " << minDistance << endl;
return 0;
}