Advertisement
nurzamf

Shortest Path Problem.

Nov 27th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <process.h>
  4.  
  5. int main()
  6. {
  7. int cost[9][9]={
  8. {0,2,4,4,5,0,0,0,3},
  9. {0,0,3,0,9,4,0,10,0},
  10. {0,0,0,0,0,0,2,7,7},
  11. {0,0,0,0,0,0,0,0,5},
  12. {0,0,0,0,0,0,0,0,2},
  13. {0,0,0,0,0,0,0,7,0},
  14. {0,0,0,0,0,0,0,0,0},
  15. {0,0,0,0,0,0,0,0,2},
  16. {0,0,0,0,0,0,0,0,0}};
  17.  
  18. int path[10][9]={
  19. {1,2,3,8,9,0,0,0,0},
  20. {1,2,6,8,9,0,0,0,0},
  21. {1,2,8,9,0,0,0,0,0},
  22. {1,2,5,9,0,0,0,0,0},
  23. {1,3,8,9,0,0,0,0,0},
  24. {1,3,7,9,0,0,0,0,0},
  25. {1,3,9,0,0,0,0,0,0},
  26. {1,4,9,0,0,0,0,0,0},
  27. {1,5,9,0,0,0,0,0,0},
  28. {1,9,0,0,0,0,0,0,0}};
  29.  
  30.  
  31. int n=9;
  32. int distance[10];
  33. int i, j, v, p, min, index=1;
  34. int row, column;
  35.  
  36. printf("\t\t\n\n *********************************");
  37. printf("\n\n SHORTEST PATH PROBLEM");
  38. printf("\n\n *********************************");
  39. printf("\n\n Enter the node that you wish to visit: " );
  40. scanf("%d", &v);
  41. printf("\n\n Enter the number of paths for node %d: ",v);
  42. scanf("%d", &p);
  43.  
  44. for (i=1; i<=p; i++)
  45. {
  46. distance[i]=0;
  47. row=1;
  48. for (j=1; j<=n; j++)
  49. {
  50. if (row!=v)
  51. {
  52. column=path[i][j+1];
  53. distance[i]=distance[i]+cost[row][column];
  54. }
  55. row=column;
  56. }
  57. }
  58.  
  59. min=distance[1];
  60. for (i=1; i<=p; i++)
  61. {
  62. if (distance[i]<=min)
  63. {
  64. min=distance[i];
  65. index=i;
  66. }
  67. }
  68.  
  69. printf("\n\n The minimum distance is %d: ", min);
  70. printf("\n\n The shortest path is: \n");
  71. for (i=1; i<=n; i++)
  72. {
  73. if (path[index][i]!=0)
  74. {
  75. printf(" --> %d", path[index][i]);
  76. }
  77. }
  78. getch();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement