nguyentruong98

Untitled

Jan 2nd, 2019
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define INFINITY 9999
  5. #define MAX 201
  6. void dijkstra(int adjacencymatrix[MAX][MAX], int n, int startnode, int endnode)
  7. {
  8.       int cost[MAX][MAX], distance[MAX], pred[MAX];
  9.       int visited[MAX], count, mindistance, nextnode, i, j;
  10.       for (i = 1; i <= n; i++)
  11.             for (j = 1; j <= n; j++)
  12.                   if (adjacencymatrix[i][j] == 0)
  13.                         cost[i][j] = INFINITY;
  14.                   else
  15.                         cost[i][j] = adjacencymatrix[i][j];
  16.       for (i = 1; i <= n; i++)
  17.       {
  18.             distance[i] = cost[startnode][i];
  19.             pred[i] = startnode;
  20.             visited[i] = 0;
  21.       }
  22.       distance[startnode] = 0;
  23.       visited[startnode] = 1;
  24.       count = 1;
  25.       while (count < n - 1)
  26.       {
  27.             mindistance = INFINITY;
  28.             for (i = 1; i <= n; i++)
  29.                   if (distance[i] < mindistance && !visited[i])
  30.                   {
  31.                         mindistance = distance[i];
  32.                         nextnode = i;
  33.                   }
  34.             visited[nextnode] = 1;
  35.             for (i = 1; i <= n; i++)
  36.                   if (!visited[i])
  37.                         if (mindistance + cost[nextnode][i] < distance[i])
  38.                         {
  39.                               distance[i] = mindistance + cost[nextnode][i];
  40.                               pred[i] = nextnode;
  41.                         }
  42.             count++;
  43.       }
  44.       for (i = 1; i <= n; i++)
  45.       {
  46.             if (i == endnode)
  47.             {
  48.                   printf("\nThe shortest distance between two nodes %d=%d", i, distance[i]);
  49.                   printf("\nPath=%d", i);
  50.                   j = i;
  51.                   do
  52.                   {
  53.                         j = pred[j];
  54.                         printf("<-%d", j);
  55.                   } while (j != startnode);
  56.             }
  57.       }
  58. }
  59. int mapinput[201][201];
  60. int adjacencymatrix[201][201];
  61. int vertices;
  62. int edge;
  63. int u, y;
  64. int main()
  65. {
  66.       int i, j;
  67.       FILE *f = fopen("diamtest.txt", "r");
  68.       if (f == NULL)
  69.       {
  70.             printf("FILE ERR\n");
  71.             return 0;
  72.       }
  73.       else
  74.       {
  75.             fscanf(f, "%d %d", &vertices, &edge);
  76.             for (i = 1; i <= vertices; i++)
  77.             {
  78.                   for (j = 1; j <= 2; j++)
  79.                   {
  80.                         fscanf(f, "%d", &mapinput[i][j]);
  81.                   }
  82.             }
  83.       }
  84.       for (i = 1; i <= vertices; i++)
  85.             for (j = 1; j <= vertices; j++)
  86.             {
  87.                   if (mapinput[i][j] != i && j <= 2)
  88.                   {
  89.                         adjacencymatrix[i][mapinput[i][j]] = 1;
  90.                         adjacencymatrix[mapinput[i][j]][i] = 1;
  91.                   }
  92.                   if (mapinput[i][j] == i && j <= 2)
  93.                         adjacencymatrix[i][mapinput[i][j]] = 0;
  94.             }
  95.       printf("\nEnter the starting node:");
  96.       scanf("%d", &u);
  97.       printf("\nEnter the ending node:");
  98.       scanf("%d", &y);
  99.       dijkstra(adjacencymatrix, vertices, u, y);
  100.       return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment