Posted By

ldominov on 01/16/13


Tagged


Versions (?)

floydov


 / Published in: C++
 

implementation of floyd's algorithm with the dynamic programming in the c++;das

  1. #include <iostream>
  2. #define N 4
  3. using namespace std;
  4.  
  5. int min(int a,int b){
  6. if(a < b) return a;
  7. return b;
  8. }
  9. int main(){
  10. int D[N][N] ={
  11. {0,999,3,999},
  12. {2,0,999,999},
  13. {999,7,0,1},
  14. {6,999,999,0}
  15. };
  16. for(int k = 0; k<N; k++)
  17. for(int i = 0; i<N; i++)
  18. for(int j = 0; j<N; j++)
  19. D[i][j] = min(D[i][j], D[i][k]+D[k][j]);
  20.  
  21. cout<<"\nMatrica udaljenosti:\n";
  22. for(int i=0;i<N;i++){
  23. for(int j=0;j<N;j++)
  24. cout<<D[i][j]<<" ";
  25. cout<<"\n";
  26. }
  27. int n; cin >> n;
  28. return 0;
  29. }

Report this snippet  

You need to login to post a comment.