Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <process.h>
- int main()
- {
- int cost[9][9]={
- {0,2,4,4,5,0,0,0,3},
- {0,0,3,0,9,4,0,10,0},
- {0,0,0,0,0,0,2,7,7},
- {0,0,0,0,0,0,0,0,5},
- {0,0,0,0,0,0,0,0,2},
- {0,0,0,0,0,0,0,7,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,2},
- {0,0,0,0,0,0,0,0,0}};
- int path[10][9]={
- {1,2,3,8,9,0,0,0,0},
- {1,2,6,8,9,0,0,0,0},
- {1,2,8,9,0,0,0,0,0},
- {1,2,5,9,0,0,0,0,0},
- {1,3,8,9,0,0,0,0,0},
- {1,3,7,9,0,0,0,0,0},
- {1,3,9,0,0,0,0,0,0},
- {1,4,9,0,0,0,0,0,0},
- {1,5,9,0,0,0,0,0,0},
- {1,9,0,0,0,0,0,0,0}};
- int n=9;
- int distance[10];
- int i, j, v, p, min, index=1;
- int row, column;
- printf("\t\t\n\n *********************************");
- printf("\n\n SHORTEST PATH PROBLEM");
- printf("\n\n *********************************");
- printf("\n\n Enter the node that you wish to visit: " );
- scanf("%d", &v);
- printf("\n\n Enter the number of paths for node %d: ",v);
- scanf("%d", &p);
- for (i=1; i<=p; i++)
- {
- distance[i]=0;
- row=1;
- for (j=1; j<=n; j++)
- {
- if (row!=v)
- {
- column=path[i][j+1];
- distance[i]=distance[i]+cost[row][column];
- }
- row=column;
- }
- }
- min=distance[1];
- for (i=1; i<=p; i++)
- {
- if (distance[i]<=min)
- {
- min=distance[i];
- index=i;
- }
- }
- printf("\n\n The minimum distance is %d: ", min);
- printf("\n\n The shortest path is: \n");
- for (i=1; i<=n; i++)
- {
- if (path[index][i]!=0)
- {
- printf(" --> %d", path[index][i]);
- }
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement