jaOjaa

datstruct final

Jun 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #define SIZE 100
  6. #define INFINITY 99999
  7. #define NOT_DEFINED 0
  8.  
  9. int readFile(char *);
  10. int djikstra();
  11. void CLR();
  12.  
  13. int graph[SIZE][SIZE];
  14. int graph_dimension;
  15. char cities[SIZE][SIZE];
  16.  
  17. int from;
  18. int dest;
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. readFile("kota.txt");
  23.  
  24. int ch;
  25. do
  26. {
  27. CLR();
  28. printf("Welcome to Map Application\n\n");
  29. printf("Menus :\n");
  30. printf("1. Cities list\n");
  31. printf("2. Set start and finish\n");
  32. printf("3. Exit program\n\n\n");
  33.  
  34. do
  35. {
  36. printf("> Please select your choice : ");
  37. scanf("%d", &ch);
  38. } while (ch < 1 || ch > 3);
  39. switch (ch)
  40. {
  41. case 1:
  42. {
  43. CLR();
  44. printf("Available cities :\n\n");
  45. for (int i = 0; i < sizeof(cities) && i < graph_dimension; i++)
  46. {
  47. printf("%d. %s\n", i + 1, cities[i]);
  48. }
  49. printf("\n");
  50. getch();
  51. break;
  52. }
  53. case 2:
  54. {
  55. CLR();
  56. printf("Available cities :\n\n");
  57. for (int i = 0; i < sizeof(cities) && i < graph_dimension; i++)
  58. {
  59. printf("[ %d ] %s\n", i + 1, cities[i]);
  60. }
  61. printf("\n");
  62. printf("Insert start : ");
  63. scanf("%d", &from);
  64. printf("Insert finish : ");
  65. scanf("%d", &dest);
  66. from -= 1;
  67. dest -= 1;
  68.  
  69. djikstra();
  70. getch();
  71. break;
  72. }
  73. }
  74. } while (ch != 3);
  75.  
  76. printf("\n\n");
  77. return 0;
  78. }
  79.  
  80. void CLR()
  81. {
  82. system("@cls||clear");
  83. }
  84.  
  85. int isHeapEmpty(int heap[SIZE])
  86. {
  87. int i, res = 0;
  88. for (i = 0; i < SIZE && i < graph_dimension; i++)
  89. {
  90. if (heap[i] == 1)
  91. {
  92. res = 1;
  93. break;
  94. }
  95. }
  96. return res;
  97. }
  98.  
  99. int extractMin(int heap[SIZE], int dist[SIZE], int current)
  100. {
  101. int i, min = INFINITY, res = -1;
  102. for (i = 0; i < SIZE && i < graph_dimension; i++)
  103. {
  104. if (min > dist[i] && heap[i] != 0)
  105. {
  106. min = dist[i];
  107. res = i;
  108. }
  109. }
  110. return res;
  111. }
  112.  
  113. int djikstra()
  114. {
  115. int i, j, start, near, current;
  116. int dist[SIZE], heap[SIZE], parent[SIZE];
  117.  
  118. start = current = start;
  119. for (i = 0; i < SIZE && i < graph_dimension; i++)
  120. {
  121. dist[i] = INFINITY;
  122. heap[i] = 1;
  123. parent[i] = NOT_DEFINED;
  124. }
  125. dist[0] = 0;
  126.  
  127. while (isHeapEmpty(heap) == 1)
  128. {
  129. near = extractMin(heap, dist, current);
  130. if (near < 0)
  131. break;
  132. heap[near] = 0;
  133. for (i = 0; i < SIZE && i < graph_dimension; i++)
  134. {
  135. if (heap[i] == 1 && (dist[i] > dist[near] + graph[near][i]))
  136. {
  137. dist[i] = dist[near] + graph[near][i];
  138. parent[i] = near;
  139. }
  140. }
  141. current = near;
  142. }
  143.  
  144. for (i = 0; i < SIZE && i < graph_dimension; i++)
  145. {
  146. if (i == start)
  147. continue;
  148. current = i;
  149. if (i == dest)
  150. {
  151. if (dist[i] >= INFINITY)
  152. {
  153. printf("\n\n (S%d,S%d): Couldn't find path'!\n", start, i);
  154. continue;
  155. }
  156. printf("\nTotal distance from city %s to city %s : %d km\nTotal cost : Rp %d", cities[start], cities[i], dist[i], dist[i]*1500);
  157. printf("\n");
  158. }
  159. }
  160. return 0;
  161. }
  162.  
  163. int readFile(char *filename)
  164. {
  165. FILE *stream = fopen(filename, "r");
  166.  
  167. char line[1024];
  168. int maxi;
  169. int maxj;
  170.  
  171. int i = 0;
  172. while (fgets(line, 1024, stream))
  173. {
  174. char *tmp = strdup(line);
  175.  
  176. const char *tok;
  177. int j;
  178. for (tok = strtok(line, ",");
  179. tok && *tok;
  180. tok = strtok(NULL, ",\n"))
  181. {
  182. if (j == 0)
  183. {
  184. strcpy(cities[i], tok);
  185. }
  186. else
  187. {
  188. int weight = atoi(tok);
  189. if (weight == NOT_DEFINED)
  190. {
  191. graph[i][j - 1] = INFINITY;
  192. }
  193. else
  194. {
  195. graph[i][j - 1] = weight;
  196. }
  197. }
  198. j++;
  199. }
  200. free(tmp);
  201. graph_dimension = j - 1;
  202. j = 0;
  203. i++;
  204. }
  205.  
  206. fclose(stream);
  207. return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment