Guest User

Untitled

a guest
Dec 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #define MAX 10000
  6.  
  7. #define fr(a,b,c) for(int a = b ; a < c ; a++ )
  8. #define rp(a,c) fr(a,0,c)
  9.  
  10.  
  11. using namespace std;
  12.  
  13. int pos;
  14. int inicio,fim;
  15. int v,arestas;
  16. int matriz[MAX][2*MAX];
  17. int vet[MAX];
  18. bool vis[MAX];
  19.  
  20.  
  21. struct ligado
  22. {
  23.     int a;
  24.     int pos;
  25.     ligado(int a = 0, int pos = 0): a(a), pos(pos){}
  26.  
  27. }aux2[MAX];
  28.  
  29. struct ponto
  30. {
  31.     int x;
  32.     int y;
  33.     int c;
  34.     int pos;
  35.  
  36.     ponto(int x=0, int y = 0, int c = 0, int pos = 0): x(x), y(y), c(c), pos(pos){}
  37.  
  38. }aux[10000];
  39.  
  40.  
  41. bool minimo(ponto a, ponto b)
  42. {
  43.     if(a.c < b.c) return true;
  44.     else return false;
  45. }
  46.  
  47. void heap_push(ponto novo)
  48. {
  49.     pos++;
  50.     aux[pos] = novo;
  51.     int n = pos;
  52.     bool troca = true;
  53.     ponto aux2;
  54.     int pai = n/2;
  55.  
  56.     while(n>1 && troca)
  57.     {
  58.         troca = false;
  59.         if(aux[n].c <= aux[pai].c)
  60.         {
  61.             aux2 = aux[pai];
  62.             aux[pai] = aux[n];
  63.             aux[n]=aux2;
  64.             troca=true;
  65.         }
  66.         n = pai;
  67.         pai = n/2;
  68.     }
  69.  
  70. }
  71.  
  72. void heap_pop()
  73. {
  74.     aux[1]=aux[pos];
  75.     pos--;
  76.  
  77.     int n = pos;
  78.  
  79.     int i=1;
  80.     ponto aux2;
  81.     int menor;
  82.  
  83.     while(i <= n/2)
  84.     {
  85.         if(i< (n/2 + n%2))
  86.         {
  87.             if(aux[2*i].c < aux[2*i +1].c)
  88.             {
  89.                 menor = 2*i;
  90.             }
  91.             else
  92.             {
  93.                 menor = 2*i +1;
  94.             }
  95.         }
  96.         else
  97.         {
  98.             menor = 2*i;
  99.         }
  100.  
  101.  
  102.         if(aux[i].c > aux[menor].c)
  103.         {
  104.             aux2 = aux[i];
  105.             aux[i] = aux[menor];
  106.             aux[menor] = aux2;
  107.  
  108.             i = menor;
  109.         }
  110.         else
  111.         {
  112.             i = n;
  113.         }
  114.     }
  115.  
  116.  
  117. }
  118.  
  119. int process()
  120. {
  121.     pos=0;
  122.  
  123.     rp(i,vet[inicio])
  124.     {
  125.         int ant = inicio;
  126.         int prox = matriz[inicio][i];
  127.         i++;
  128.         int c = matriz[inicio][i];
  129.         heap_push(ponto(ant,prox,c,i));
  130.     }
  131.  
  132.     vis[inicio] = true;
  133.  
  134.  
  135.     while(pos)
  136.     {
  137.         int ant = aux[1].x;
  138.         int atual = aux[1].y;
  139.         int c = aux[1].c;
  140.         int pos = aux[1].pos;
  141.  
  142.         heap_pop();
  143.  
  144.         if(atual == fim)
  145.         {
  146.              aux2[atual] = ligado(ant,pos);
  147.              return c;
  148.         }
  149.  
  150.         if(!vis[atual])
  151.         {
  152.             vis[atual] = true;
  153.  
  154.             aux2[atual] = ligado(ant,pos);
  155.  
  156.             rp(i,vet[atual])
  157.             {
  158.                 int prox = matriz[atual][i];
  159.                 i++;
  160.                 int custo = matriz[atual][i];
  161.  
  162.                 if(!vis[prox])
  163.                 {
  164.                     heap_push(ponto(atual,prox,c + custo,i));
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.  
  171.     return -1;
  172. }
  173.  
  174. int process2()
  175. {
  176.     pos=0;
  177.  
  178.     rp(i,vet[fim])
  179.     {
  180.         int ant = fim;
  181.         int prox = matriz[fim][i];
  182.         i++;
  183.         int c = matriz[fim][i];
  184.         heap_push(ponto(ant,prox,c,0));
  185.         heap_push(ponto(ant,prox,c/2,1));
  186.     }
  187.  
  188.     vis[fim] = true;
  189.  
  190.     while(pos)
  191.     {
  192.         int atual = aux[1].y;
  193.         int c = aux[1].c;
  194.         int bic = aux[1].pos;
  195.  
  196.         heap_pop();
  197.  
  198.         if(atual == inicio) return c;
  199.  
  200.         if(!vis[atual])
  201.         {
  202.             vis[atual] = true;
  203.  
  204.             rp(i,vet[atual])
  205.             {
  206.                 int prox = matriz[atual][i];
  207.                 i++;
  208.                 int custo = matriz[atual][i];
  209.  
  210.                 if(!vis[prox])
  211.                 {
  212.                     heap_push(ponto(atual,prox,c + custo,0));
  213.  
  214.                     if(!bic)
  215.                     {
  216.                         heap_push(ponto(atual,prox,c + custo/2,1));
  217.                     }
  218.  
  219.                 }
  220.             }
  221.         }
  222.         else
  223.         {
  224.             if(!bic)
  225.             {
  226.                 rp(i,vet[atual])
  227.                 {
  228.                     int prox = matriz[atual][i];
  229.                     i++;
  230.                     int custo = matriz[atual][i];
  231.  
  232.                     if(!vis[prox])
  233.                     {
  234.                         heap_push(ponto(atual,prox,c + custo/2,1));
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.  
  240.  
  241.     }
  242.  
  243.  
  244.     return -1;
  245.  
  246.  
  247. }
  248.  
  249.  
  250. void removeArestas()
  251. {
  252.     int remover = fim;
  253.  
  254.     while(aux2[remover].a != inicio)
  255.     {
  256.         int a = aux2[remover].a;
  257.         int pos = aux2[remover].pos;
  258.  
  259.         vet[a] = vet[a] - 2;
  260.  
  261.         matriz[a][pos] = matriz[a][vet[a]];
  262.         matriz[a][pos+1] = matriz[a][vet[a]+1];
  263.  
  264.         remover = a;
  265.     }
  266.  
  267.  
  268. }
  269.  
  270.  
  271. int main()
  272. {
  273.     freopen("L4Q4.in","r",stdin);
  274.     int caso=1;
  275.     while(scanf("%d %d",&v,&arestas)>0 && (v!=0 || arestas!=0))
  276.     {
  277.  
  278.         memset(vet,0,sizeof(vet));
  279.         memset(vis,false,sizeof(vis));
  280.  
  281.         scanf("%d %d",&inicio,&fim);
  282.  
  283.         rp(i,arestas)
  284.         {
  285.             int x,y,c;
  286.  
  287.             scanf("%d %d %d",&x,&y,&c);
  288.             matriz[x][vet[x]] = y;
  289.             vet[x]++;
  290.             matriz[x][vet[x]] = c;
  291.             vet[x]++;
  292.         }
  293.  
  294.         if(inicio == fim)
  295.         {
  296.              printf("Caso #%d: %d\n",caso++,-1);
  297.              printf("-1\n");
  298.              continue;
  299.         }
  300.  
  301.         int retorno = process();
  302.  
  303.         printf("Caso #%d: %d\n",caso++,retorno);
  304.  
  305.         if(retorno > 0)
  306.         {
  307.             removeArestas();
  308.             memset(vis,false,sizeof(vis));
  309.  
  310.             printf("%d\n",process2());
  311.  
  312.         }
  313.         else
  314.         {
  315.             printf("-1\n");
  316.         }
  317.  
  318.  
  319.     }
  320.     return 0;
  321. }
Add Comment
Please, Sign In to add comment