Advertisement
yesh666

Dijkstra using c

Aug 17th, 2022 (edited)
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
  5. printf("enter the cost matrix\n");
  6. for(i=1;i<=5;i++)
  7. for(j=1;j<=5;j++)
  8. scanf("%d",&a[i][j]);
  9. printf("enter the paths\n");
  10. scanf("%d",&p);
  11. printf("enter possible paths\n");
  12. for(i=1;i<=p;i++)
  13. for(j=1;j<=5;j++)
  14. scanf("%d",&path[i][j]);
  15. for(i=1;i<=p;i++)
  16. {
  17. t[i]=0;
  18. stp=st;
  19. for(j=1;j<=5;j++)
  20. {
  21. edp=path[i][j+1];
  22. t[i]=t[i]+a[stp][edp];
  23. if(edp==ed)
  24. break;
  25. else
  26. stp=edp;
  27. }
  28. }
  29. min=t[st];index=st;
  30. for(i=1;i<=p;i++)
  31. {
  32. if(min>t[i])
  33. {
  34. min=t[i];
  35. index=i;
  36. }
  37. }
  38. printf("minimum cost %d",min);
  39. printf("\n minimum cost path ");
  40. for(i=1;i<=5;i++)
  41. {
  42. printf("--> %d",path[index][i]);
  43. if(path[index][i]==ed)
  44. break;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement