Guest User

Untitled

a guest
Dec 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int linhas;
  7. int colunas;
  8. int n;
  9. int entrada;
  10.  
  11. int matriz[500][500];
  12. int marcado[500][500];
  13. int matrizCusto[500][500];
  14.  
  15. int dx[] = {0, 0, -1 , 1};
  16. int dy[] = {1, -1 , 0, 0};
  17.  
  18. typedef struct no No;
  19.  
  20. struct no{
  21.     int i;
  22.     int j;
  23.     int custo;
  24. };
  25.  
  26. No heap[250000];
  27.  
  28. void insercao(int i, int j, int custo){
  29.     heap[entrada].i = i;
  30.     heap[entrada].j = j;
  31.     heap[entrada].custo = custo;
  32.     entrada++;
  33. }
  34.  
  35. void heapify(int n, int i)
  36. {
  37.     int maior;
  38.     No aux;
  39.     while(i <= n/2)
  40.     {
  41.         if(n%2 == 0)
  42.         {
  43.             if(i < n/2)
  44.             {
  45.                 if(heap[2*i].custo > heap[2*i+1].custo)
  46.                 {
  47.                     maior = 2*i;
  48.                 }
  49.                 else
  50.                 {
  51.                     maior = 2*i+1;
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 maior = 2*i;
  57.             }
  58.         }
  59.         else
  60.         {
  61.             if(i <= n/2)
  62.             {
  63.                 if(heap[2*i].custo > heap[2*i+1].custo)
  64.                 {
  65.                     maior = 2*i;
  66.                 }
  67.                 else
  68.                 {
  69.                     maior = 2*i+1;
  70.                 }
  71.             }
  72.             else
  73.             {
  74.                 maior = 2*i;
  75.             }
  76.         }
  77.  
  78.         if(heap[i].custo < heap[maior].custo)
  79.         {
  80.             aux = heap[i];
  81.             heap[i] = heap[maior];
  82.             heap[maior] = aux;
  83.             i = maior;
  84.         }
  85.         else
  86.             i = n;
  87.     }
  88. }
  89.  
  90. void constroiHeap(int n)
  91. {
  92.     int i;
  93.     for(i = n/2; i >= 1; i--)
  94.         heapify(n, i);
  95. }
  96.  
  97. void heapS(int n)
  98. {
  99.     int i, j, k;
  100.     int tam = n;
  101.     No aux;
  102.     for(i = n, j = 1; i >= 1; i--)
  103.     {
  104.         constroiHeap(tam);
  105.         aux = heap[i];
  106.         heap[i] = heap[j];
  107.         heap[j] = aux;
  108.         tam--;
  109.     }
  110. }
  111.  
  112. void formiga(){
  113.     entrada = 1;
  114.     for (int i = 0; i < linhas; ++i)
  115.     {
  116.         for (int j = 0; j < colunas; ++j)
  117.         {
  118.             marcado[i][j] = false;
  119.             matrizCusto[i][j] = 1<<30;
  120.         }
  121.     }
  122.  
  123.     insercao(0, 0, matriz[0][0]);
  124.  
  125.     int i;
  126.     int j;
  127.     int custo=0;
  128.     int custoTotal = 0;
  129.     int k;
  130.  
  131.     i = heap[1].i;
  132.     j = heap[1].j;
  133.     custo = heap[1].custo;
  134.     entrada--;
  135.  
  136.     while(1){
  137.         if(entrada > 2) heapS(entrada-1);
  138.  
  139.         i = heap[1].i;
  140.         j = heap[1].j;
  141.         custo = heap[1].custo;
  142.        
  143.  
  144.         if(entrada > 2){ // tira do heap
  145.             heap[1].i = heap[entrada-1].i;
  146.             heap[1].j = heap[entrada-1].j;
  147.             heap[1].custo = heap[entrada-1].custo;
  148.             entrada--;
  149.         }
  150.  
  151.  
  152.         if(marcado[i][j])
  153.             continue;
  154.        
  155.         marcado[i][j] = true;
  156.        
  157.         if(i == linhas - 1 && j == colunas - 1) break;
  158.        
  159.  
  160.         //erro aqui!
  161.         for (int cont = 0; cont < 4; ++cont)
  162.         {
  163.             if((j+dx[cont] >= 0 && j+dx[cont] < colunas) && (i+dy[cont] >= 0 && i+dy[cont] < linhas)){
  164.                 if(!marcado[i+dy[cont]][j+dx[cont]] && custo+matriz[i+dy[cont]][j+dx[cont]] < matrizCusto[i+dy[cont]][j+dx[cont]]){
  165.                     insercao(i+dy[cont], j+dx[cont], custo+matriz[i+dy[cont]][j+dx[cont]]);
  166.                 }
  167.             }
  168.         }
  169.  
  170.     }
  171.  
  172.     /*for (int i = 0; i < linhas; ++i)
  173.     {
  174.         for (int j = 0; j < colunas; ++j)
  175.         {
  176.             printf("%d ", matrizCusto[i][j]);
  177.         }
  178.         printf("\n");
  179.     }*/
  180.  
  181.     printf("%d\n", custo);
  182.  
  183.  
  184. }
  185.  
  186. int main()
  187. {
  188.     freopen("input", "r", stdin);
  189.  
  190.     scanf("%d", &n);
  191.     while(n--){
  192.         scanf("%d %d", &linhas, &colunas);
  193.         for (int i = 0; i < linhas; ++i)
  194.         {
  195.             for (int j = 0; j < colunas; ++j)
  196.             {
  197.                 scanf("%d", &matriz[i][j]);
  198.             }
  199.         }
  200.  
  201.  
  202.  
  203.         formiga();
  204.     }
  205.     return 0;
  206. }
Add Comment
Please, Sign In to add comment