Guest User

Untitled

a guest
Jun 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. void Dijkstra(Cidade **lista,int qtdCidade, int qtdMoeda)
  2. {
  3. int **Distancias,**Marcados,tamanho=1,j,k,verticeAtual,moedaAtual,CustoTotal=0;
  4. Heap *heap;
  5. Cidade *TEMP;
  6.  
  7. Distancias = matriz_create (qtdCidade+1,qtdMoeda+1);
  8. Marcados = matriz_create (qtdCidade+1,qtdMoeda+1);
  9. heap = (Heap*)calloc(15000,sizeof(Heap));
  10.  
  11. for(j=1;j<=qtdCidade;j++)
  12. {
  13. for(k=1;k<=qtdMoeda;k++)
  14. {
  15. Distancias[j][k] = 0x3f3f3f3f;
  16. }
  17. }
  18.  
  19. Distancias[1][qtdMoeda] = 0;
  20.  
  21. heap[1].vertice = 1;
  22. heap[1].distancia = 0;
  23. heap[1].pedagio = qtdMoeda;
  24. Marcados[1][qtdMoeda] = 1;
  25.  
  26. while(tamanho >= 1)
  27. {
  28. ControiHeap(heap,tamanho);
  29. verticeAtual = heap[1].vertice;
  30. moedaAtual = heap[1].pedagio;
  31. ///remoção no heap
  32. heap[1] = heap[tamanho];
  33. tamanho--;
  34. heapify(heap,tamanho,1);
  35.  
  36. Marcados[verticeAtual][moedaAtual] = 1;
  37.  
  38. TEMP = lista[verticeAtual];
  39. while(TEMP->NEXT != NULL)
  40. {
  41. TEMP = TEMP->NEXT;
  42. if((TEMP->pedagio + Distancias[verticeAtual][moedaAtual]) < Distancias[TEMP->cidade][TEMP->pedagio] && Marcados[TEMP->cidade][TEMP->pedagio] == 0 )
  43. {
  44. Distancias[TEMP->cidade][TEMP->pedagio] = TEMP->pedagio + Distancias[verticeAtual][moedaAtual];
  45. tamanho++;
  46. heap[tamanho].vertice = TEMP->cidade;
  47. heap[tamanho].distancia = TEMP->comprimento;
  48. heap[tamanho].pedagio = TEMP->pedagio;
  49. ControiHeap(heap,tamanho);
  50. }
  51. }
  52. }
  53.  
  54. Quicksort(Distancias[qtdCidade],1,qtdMoeda);
  55. if(Distancias[qtdCidade][1] != 0x3f3f3f3f )
  56. {
  57. fprintf(Saida,"%d\n",Distancias[qtdCidade][1]);
  58. }
  59. else
  60. {
  61. fprintf(Saida,"-1\n");
  62. }
  63. liberarMatriz (Marcados,qtdCidade);
  64. liberarMatriz (Distancias,qtdCidade);
  65. }
Add Comment
Please, Sign In to add comment