Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. int minDistance(int D[], int S[])
  2. {
  3. // Initialize min value
  4. int min = 150, min_index;
  5.  
  6. for (int i = 0; i < nnod; i++){
  7. //cout << min_index;
  8. if (S[i] == 0 && D[i] <= min){
  9. min = D[i];
  10. min_index = i;
  11. }
  12. }
  13.  
  14. return min_index;
  15. }
  16.  
  17. int* Dijkstra (int mat[nnod][nnod],int sursa){
  18. int i, j, k;
  19. int lungmax = 150;
  20. int *S = (int*)malloc(nnod * sizeof(int));
  21. int *D = (int*)malloc(nnod * sizeof(int));
  22. int *V = (int*)malloc(nnod * sizeof(int));
  23. for (i = 0; i < nnod; i++){
  24. S[i] = 0;
  25. // nod neselectat
  26. D[i] = lungmax;
  27. // distantele minime de la sursa
  28. /*if (D[i] < lungmax)
  29. V[i] = i;
  30. // initializeaza vecinii
  31. else
  32. V[i] = 0;
  33. */}
  34. //S[sursa] = 1;
  35. // selecteaza nodul sursa
  36. D[sursa] = 0;
  37. //cout << mat[sursa][1] << '\n';
  38. for ( i = 0; i < nnod; i++){
  39.  
  40.  
  41. S[sursa] = 1;
  42. for (j = 0; j < nnod; j++) {
  43. if(mat[sursa][j] != 0 && mat[sursa][j] != 150) {
  44. // recalculeaza distantele
  45. if(sursa == 0)
  46. cout << j << "kl" << endl;
  47. //cout << "dada\n";
  48. if ((D[sursa] + mat[sursa][j] < D[j])){
  49. D[j] = D[sursa] + mat[sursa][j];
  50. V[j] = V[sursa];
  51. // modifica tabela de dirijare
  52. }
  53. if(sursa == 0)
  54. cout << j << "k\n";
  55. }
  56.  
  57. sursa = minDistance(D,S);
  58. }
  59. }
  60. return D;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement