Advertisement
Guest User

Untitled

a guest
Feb 21st, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <float.h>
  5.  
  6. double count_weight(int* path, double** adjacency_matrix, int* size, int v_start) {
  7.     double weight = adjacency_matrix[v_start][path[0]];
  8.     for (int i = 0; i < *size - 1; i++) {
  9.         weight += adjacency_matrix[path[i]][path[i + 1]];
  10.     }
  11.     return weight;
  12. }
  13.  
  14. void dfs(int start, int end, int* path, int** shortest_path, double** adjacency_matrix,
  15.          bool* visited, int n, int* size, int v_start, double* minimum_weight, int* final_size) {
  16.     if (start == end) {
  17.         double current_weight = count_weight(path, adjacency_matrix, size, v_start);
  18.         if (current_weight < *minimum_weight) {
  19.             *shortest_path = malloc((*size) * sizeof(int));
  20.             for (int i = 0; i < *size; i++) {
  21.                 (*shortest_path)[i] = path[i];
  22.             }
  23.             *minimum_weight = current_weight;
  24.             *final_size = *size;
  25.         }
  26.         return;
  27.     }
  28.     visited[start] = true;
  29.     for (int i = 0; i < n; i++) {
  30.         if (adjacency_matrix[start][i] > 0 && visited[i] == false) {
  31.             path = realloc(path, ((*size) + 1) * sizeof(int));
  32.             path[(*size)++] = i;
  33.             dfs(i, end, path, shortest_path, adjacency_matrix, visited, n, size, v_start, minimum_weight, final_size);
  34.             path = realloc(path, ((*size) - 1) * sizeof(int));
  35.             (*size)--;
  36.         }
  37.     }
  38.     visited[start] = false;
  39. }
  40.  
  41. int main() {
  42.     int n;
  43.     printf("Podaj liczbe wierzcholkow: ");
  44.     scanf("%d", &n);
  45.  
  46.     bool* visited = malloc(n * sizeof(bool));
  47.     double** adjacency_matrix;
  48.     adjacency_matrix = malloc(n * sizeof(double*));
  49.     for (int i = 0; i < n; i++) {
  50.         adjacency_matrix[i] = malloc(n * sizeof(double));
  51.     }
  52.  
  53.     for (int i = 0; i < n; i++) {
  54.         for (int j = 0; j < n; j++) {
  55.             adjacency_matrix[i][j] = 0;
  56.         }
  57.     }
  58.  
  59.     int v_s, v_e;
  60.     printf("\nPodaj wierzcholek poczatkowy oraz koncowy: ");
  61.     scanf("%d %d", &v_s, &v_e);
  62.  
  63.     int k;
  64.     printf("\nPodaj liczbe przejsc: ");
  65.     scanf("%d", &k);
  66.  
  67.     int s, e;
  68.     double w;
  69.     printf("\nPodaj zgodnie ze wzorem: <poczatek> <koniec> <waga>:\n");
  70.     for (int i = 0; i < k; i++) {
  71.         scanf("%d %d %lf", &s, &e, &w);
  72.         adjacency_matrix[s - 1][e - 1] = w;
  73.     }
  74.  
  75.     int* path = NULL;
  76.     int* shortest_path;
  77.     double min_weight = DBL_MAX;
  78.     int size = 0;
  79.     int temp_size = 0;
  80.     dfs(v_s - 1, v_e - 1, path, &shortest_path, adjacency_matrix, visited, n, &temp_size, v_s - 1, &min_weight, &size);
  81.  
  82.  
  83.     printf("Najkrtosza droga:\n");
  84.     printf("%d->", v_s);
  85.     for (int i = 0; i < size - 1; i++) {
  86.         printf("%d->", shortest_path[i] + 1);
  87.     }
  88.     printf("%d", shortest_path[size - 1] + 1);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement