Advertisement
Guest User

Untitled

a guest
May 20th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct list
  5. {
  6.     list *next;
  7.     list *prev;
  8.     int data;
  9. };
  10.  
  11. list *new_list()
  12. {
  13.     list *q = new list;
  14.     q->data = 0;
  15.     q->next = q;
  16.     q->prev = q;
  17.     return q;
  18. }
  19.  
  20. int pop(list *q, int *d)
  21. {
  22.     if (q->prev == q)
  23.     {
  24.         printf("Queue was empty\n");
  25.         return 0;
  26.     }
  27.     float min = d[q->prev->data];
  28.     list *victim = q->prev;
  29.     list *current = q;
  30.     while (q != (current = current->next))
  31.     {
  32.         if (d[current->data] < min)
  33.         {
  34.             min = d[current->data];
  35.             victim = current;
  36.         }
  37.     }
  38.  
  39.     victim->prev->next = victim->next;
  40.     victim->next->prev = victim->prev;
  41.     int data = victim->data;
  42.     delete victim;
  43.     return data;
  44. }
  45.  
  46. void push(list *q, int data)
  47. {
  48.     list *tmp = q->next;
  49.     q->next = new list;
  50.     q->next->prev = q;
  51.     q->next->next = tmp;
  52.     q->next->data = data;
  53.     tmp->prev = q->next;
  54. }
  55.  
  56. void _printPath(int from, int to, char city[100][100], int Pr[100])
  57. {
  58.     if (from == to)
  59.         printf("%s", city[to]);
  60.     else
  61.         if (Pr[to] == -1)
  62.         {
  63.             printf("No way\n");
  64.         }
  65.         else {
  66.             _printPath(from, Pr[to], city, Pr);
  67.             printf(" -> %s", city[to]);
  68.         }
  69. }
  70.  
  71. void printMinimalPathway(list *G[100], int N, int weights[100][100], char city[100][100], int A, int B)
  72. {
  73.     int d[100], Pr[100];
  74.     for (int i = 0; i < N; i++)
  75.     {
  76.         d[i] = 100000;
  77.         Pr[i] = -1;
  78.     }
  79.     d[A] = 0;
  80.     list *Q = new_list();
  81.     for (int i = 0; i < N; i++)
  82.     {
  83.         push(Q, i);
  84.     }
  85.     while (Q != Q->next)
  86.     {
  87.         int u = pop(Q, d);
  88.         list *cur = G[u];
  89.         while (G[u] != (cur = cur->next))
  90.         {
  91.             int v = cur->data;
  92.             if (d[v] > d[u] + weights[u][v])
  93.             {
  94.                 d[v] = d[u] + weights[u][v];
  95.                 Pr[v] = u;
  96.             }
  97.         }
  98.     }
  99.     _printPath(A, B, city, Pr);
  100. }
  101.  
  102. int getCity(char city[100][100], int *cityN, char cityName[100])
  103. {
  104.     for (int i = 0; i < *cityN; i++)
  105.     {
  106.         if (!strcmp(city[i], cityName))
  107.         {
  108.             return i;
  109.         }
  110.     }
  111.     strcpy(city[*cityN], cityName);
  112.     int ret = *cityN;
  113.     (*cityN)++;
  114.     return ret;
  115. }
  116.  
  117. int main()
  118. {
  119.     char filename[16];
  120.     printf("Filename: ");
  121.     gets_s(filename);
  122.     FILE *f = fopen(filename, "r");
  123.     if (!f)
  124.     {
  125.         perror("Error opening file");
  126.         return 1;
  127.     }
  128.  
  129.     // Создание пустого графа
  130.     list *G[100];
  131.     int cityN = 0;
  132.     char city[100][100];
  133.     int weights[100][100];
  134.     int N; fscanf(f, "%d", &N);
  135.     if (N == 0)
  136.     {
  137.         printf("No way\n");
  138.         return 0;
  139.     }
  140.  
  141.     int M; fscanf(f, "%d", &M);
  142.     for (int i = 0; i < N; i++)
  143.     {
  144.         G[i] = new_list();
  145.         for (int j = 0; j < N; j++)
  146.         {
  147.             weights[i][j] = 0;
  148.         }
  149.     }
  150.  
  151.     // Ввод системы дорог
  152.     for (int i = 0; i < M; i++)
  153.     {
  154.         char temp[100];
  155.         fscanf(f, "%s", &temp);
  156.         int city1 = getCity(city, &cityN, temp);
  157.         fscanf(f, "%s", &temp);
  158.         int city2 = getCity(city, &cityN, temp);
  159.         int s, p;
  160.         fscanf(f, "%d %d", &s, &p);
  161.         push(G[city1], city2);
  162.         push(G[city2], city1);
  163.         if (!weights[city1][city2] || weights[city1][city2] > s + p)
  164.         {
  165.             weights[city1][city2] = weights[city2][city1] = s + p;
  166.         }
  167.     }
  168.  
  169.     // Остальные данные
  170.     char _A[100], _B[100];
  171.     fscanf(f, "%s %s", &_A, &_B);
  172.     int A = getCity(city, &cityN, _A);
  173.     int B = getCity(city, &cityN, _B);
  174.  
  175.     // Вычисление и вывод результата
  176.     printMinimalPathway(G, N, weights, city, A, B);
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement