-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.c
68 lines (54 loc) · 2.02 KB
/
ts.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <limits.h>
#define MAX 20 // Maximum number of cities
int n; // Number of cities
int dist[MAX][MAX]; // Distance matrix
int dp[MAX][(1 << MAX)]; // DP table for memoization
// A function to find the minimum of two integers
int min(int a, int b) {
return (a < b) ? a : b;
}
// Function to solve TSP using dynamic programming and bitmasking
int tsp(int city, int mask) {
if (mask == (1 << n) - 1) { // If all cities have been visited
return dist[city][0]; // Return the distance to the starting city
}
// If this state has been already computed, return the stored result
if (dp[city][mask] != -1) {
return dp[city][mask];
}
int ans = INT_MAX; // Initialize the result to a large value
// Try to visit each city that has not been visited yet
for (int nextCity = 0; nextCity < n; nextCity++) {
if ((mask & (1 << nextCity)) == 0) { // If the city has not been visited
int newAns = dist[city][nextCity] + tsp(nextCity, mask | (1 << nextCity));
ans = min(ans, newAns); // Take the minimum result
}
}
// Store the result in the DP table and return it
dp[city][mask] = ans;
return ans;
}
int main() {
// Input the number of cities
printf("Enter the number of cities: ");
scanf("%d", &n);
// Input the distance matrix
printf("Enter the distance matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &dist[i][j]);
}
}
// Initialize the DP table with -1 (meaning unvisited states)
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << n); j++) {
dp[i][j] = -1;
}
}
// Start the TSP from city 0 with an initial mask of 1 (city 0 is visited)
int result = tsp(0, 1);
// Output the result (minimum cost of visiting all cities)
printf("The minimum cost of travelling all cities is: %d\n", result);
return 0;
}