Untitled
unknown
plain_text
5 months ago
1.0 kB
4
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) { 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("\n Enter the cost of Matrix:\n "); for(i=0;i<n;i++) { printf("\nEnetr the elements of rows:%d\n",i+1); for(j=0;j<n;j++) scanf("%d",&tsp_g[i][j]); 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[i][j]); } printf("\n Shortest Path: "); travellingsalesman(0,n); printf("\nMinimum Cost: "); printf("%d\n",cost); return 0; }
Editor is loading...
Leave a Comment