Untitled
unknown
plain_text
6 months ago
956 B
3
Indexable
#include <stdio.h> int tsp_g[20][20], visited[10], n, cost = 0; void travellingsalesman(int c, int n) int k, adj_vertex = 999; int min = 999; visited[c] = 1; printf("%d-> ", , c+1); for(k=0; k<n; k++) if((tsp_g[c][k] != 0) && (visited[k] == 0)) if(tsp_g[c][k] < min) { min = tsp_g[c][k]; } adj_vertex = k; } } if(min != 999) 1 cost = cost + min; } if(adj_vertex == 999) adj_vertex = 0; printf("%d" ', adj_vertex + 1); cost = cost + tsp_g[c][adj_vertex]; return; } travellingsalesman(adj_vertex,n); } int main) int i, j,n; printf("Enter the number of villages: "); scanf("%d",&n); printf("|nEnter the Cost Matrix\n"); fori=0;i < n;i++) printf("|nEnter Elements of Row: %d\n",i+1); for(j=0;] < n;j++) scanf("%d". visited[i]=0; } printf("|n\nThe cost list is:"); for i=0;i < n;i++) printf("\n"); for(j=0;j < n:j++) printf("\t%d",tsp_g[ilb]); } printf("\nShortest Path: "); travellingsalesman(O,n); printf("|nMinimum Cost: "); printf("%d\n", cost); return 0; }
Editor is loading...
Leave a Comment