karlangadas

ALGORITMIA EX2 2015-1 Pregunta 3

Dec 2nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <float.h>
  4. typedef int TElemento;
  5. typedef struct nodo{
  6.     TElemento elem;
  7.     double peso;
  8.     struct nodo *sig;
  9. }TNodo;
  10.  
  11. typedef struct lista{
  12.     TNodo *inicio;
  13.     TNodo *fin;
  14.     int numElem;
  15. }TLista;
  16.  
  17. void Lista_Crear(TLista *L){
  18.     L->inicio = NULL;
  19.     L->fin = NULL;
  20.     L->numElem = 0;
  21. }
  22.  
  23. int Lista_EstaVacia(TLista L){
  24.     return L.numElem == 0;
  25. }
  26.  
  27. void Lista_Insertar(TLista *L, TElemento e, double peso){
  28.     TNodo *ptrNuevo = malloc(sizeof(TNodo));
  29.     if (ptrNuevo != NULL){
  30.         ptrNuevo->elem = e;
  31.         ptrNuevo->peso = peso;
  32.         ptrNuevo->sig = NULL;
  33.         // Insertar al final de la lista
  34.         if (Lista_EstaVacia(*L))
  35.             L->inicio = ptrNuevo;
  36.         else
  37.             L->fin->sig = ptrNuevo;
  38.         L->fin = ptrNuevo;
  39.         (L->numElem)++;
  40.     }
  41. }
  42. int Lista_EstaEnLista(TLista L, TElemento e){
  43.     TNodo *ptrRec = L.inicio;
  44.     while (ptrRec!=NULL && ptrRec->elem!=e)
  45.         ptrRec = ptrRec->sig;
  46.     return ptrRec!=NULL;
  47. }
  48.  
  49. void Lista_Imprimir(TLista L){
  50.     TNodo *ptrRec = L.inicio;
  51.     while(ptrRec != NULL){
  52.         printf("%d ", ptrRec->elem);
  53.         ptrRec = ptrRec->sig;
  54.     }
  55.     printf("\n");
  56. }
  57. typedef struct vertice{
  58.         TElemento info;
  59.         TLista lstAdyacentes;
  60.     struct vertice *sig;
  61. }TVertice;
  62.    
  63.    
  64. typedef struct grafo{
  65.     int numVertices;
  66.     TVertice *inicio;
  67.     TVertice *fin;
  68. }TGrafo;
  69.  
  70. void Grafo_init(TGrafo *grafo){
  71.     grafo->numVertices = 0;
  72.     grafo->inicio = NULL;
  73.     grafo->fin = NULL;
  74. }
  75.  
  76. int Grafo_existeVertice(TGrafo *grafo, TElemento elem){
  77.     TVertice *ptrRec = grafo->inicio;
  78.     while(ptrRec){
  79.         if (ptrRec->info == elem) return 1;
  80.         ptrRec = ptrRec->sig;
  81.     }
  82.     return 0;
  83. }
  84.  
  85. void Grafo_insertarVertice(TGrafo *grafo, TElemento elem){
  86.     if (!Grafo_existeVertice(grafo, elem)){
  87.         TVertice *ptrNuevoVert = malloc(sizeof(TVertice));
  88.         if (ptrNuevoVert){
  89.             ptrNuevoVert->info = elem;
  90.             Lista_Crear(&ptrNuevoVert->lstAdyacentes);
  91.             ptrNuevoVert->sig = grafo->inicio;
  92.  
  93.             grafo->inicio= ptrNuevoVert;
  94.             if (grafo->numVertices == 0)
  95.                 grafo->fin = ptrNuevoVert;
  96.             grafo->numVertices++;
  97.         }
  98.     }
  99. }
  100.  
  101. void Grafo_insertarArista(TGrafo *grafo, TElemento elem1, TElemento elem2, double peso){
  102.     if (Grafo_existeVertice(grafo, elem1) && Grafo_existeVertice(grafo, elem2)){
  103.         TVertice *ptrRec = grafo->inicio;
  104.         while(ptrRec){
  105.             if (ptrRec->info ==  elem1){
  106.                 if (!Lista_EstaEnLista(ptrRec->lstAdyacentes, elem2))
  107.                     Lista_Insertar(&ptrRec->lstAdyacentes, elem2, peso);
  108.                 break;
  109.             }
  110.             ptrRec = ptrRec->sig;
  111.         }
  112.     }
  113. }
  114.  
  115. void Grafo_imprimirGrafo(TGrafo *grafo){
  116.     TVertice *ptrRec = grafo->inicio;
  117.     while (ptrRec){
  118.         printf("%d: ", ptrRec->info);
  119.         Lista_Imprimir(ptrRec->lstAdyacentes);
  120.         ptrRec = ptrRec->sig;
  121.     }
  122. }
  123.  
  124. TLista *ObtenerAdyacentes(TGrafo *grafo, TElemento elem){
  125.     TVertice *ptrRec = grafo->inicio;
  126.     while(ptrRec){
  127.         if (ptrRec->info == elem){
  128.             return &(ptrRec->lstAdyacentes);
  129.         }
  130.         ptrRec = ptrRec->sig;
  131.     }
  132. }
  133.  
  134. int ObtenerMinimoNoVisitado(TGrafo *grafo,int *arrVisitados, double *arrDistMin){
  135.     int i, indMin; double minDist = DBL_MAX;
  136.     for (i = 0; i < grafo->numVertices; i++){
  137.         if (arrVisitados[i] == 0 && arrDistMin[i] < minDist){
  138.             minDist = arrDistMin[i];
  139.             indMin = i;
  140.         }
  141.     }
  142.     return indMin;
  143. }
  144.  
  145. void Grafo_imprimirCaminosMasCortos(TGrafo *grafo, TElemento vertInicial,TElemento vertFinal){
  146.     int *arrPadres = malloc(grafo->numVertices*sizeof(int));
  147.     int *arrVisitados = malloc(grafo->numVertices*sizeof(int));
  148.     double *arrDistMin = malloc(grafo->numVertices*sizeof(double));
  149.    
  150.     int i;
  151.     for (i = 0; i < grafo->numVertices; i++){
  152.         arrPadres[i] = -1;
  153.         arrVisitados[i] = 0;
  154.         arrDistMin[i] = DBL_MAX;
  155.     }
  156.    arrDistMin[vertInicial-1] = 0;
  157.    
  158.     int indMin; TLista *lstAdyacentes;
  159.     for (i=0; i < grafo->numVertices; i++){
  160.         //Encontrar el mínimo de los no visitados
  161.         indMin = ObtenerMinimoNoVisitado(grafo, arrVisitados, arrDistMin);
  162.         arrVisitados[indMin] = 1;
  163.         lstAdyacentes = ObtenerAdyacentes(grafo, indMin + 1);
  164.         TNodo *ptrNodo = lstAdyacentes->inicio;
  165.         while (ptrNodo){
  166.             if (arrVisitados[ptrNodo->elem - 1] == 0)
  167.                 if ( arrDistMin[ptrNodo->elem - 1] > arrDistMin[indMin] + ptrNodo->peso){
  168.                     arrDistMin[ptrNodo->elem - 1] = arrDistMin[indMin] + ptrNodo->peso;
  169.                     arrPadres[ptrNodo->elem - 1] = indMin;
  170.                 }
  171.             ptrNodo = ptrNodo->sig;
  172.         }
  173.     }
  174.  
  175.     if (arrDistMin[vertFinal-1]!=DBL_MAX)
  176.         printf("%.0lf\n", arrDistMin[vertFinal-1]);
  177.     else
  178.         printf("unreachable\n");
  179.    
  180. }
  181.  
  182. int main(){
  183.     int c,m,n,s,t,ar1,ar2;
  184.     float p;
  185.     TGrafo G;
  186.     scanf("%d",&c);
  187.     int i,j;
  188.     for (i=1;i<=c;i++){
  189.         Grafo_init(&G);
  190.         scanf("%d%d%d%d",&m,&n,&s,&t);
  191.         for (j=0;j<m;j++)
  192.             Grafo_insertarVertice(&G,j+1);
  193.         for (j=0;j<n;j++){
  194.             scanf("%d%d%f",&ar1,&ar2,&p);
  195.             Grafo_insertarArista(&G,ar1+1,ar2+1,p);
  196.             Grafo_insertarArista(&G,ar2+1,ar1+1,p);
  197.         }
  198.         printf("Case #%d: ",i);
  199.         Grafo_imprimirCaminosMasCortos(&G,s+1,t+1);
  200.     }
  201. }
Add Comment
Please, Sign In to add comment