ParanoidPanda

Untitled

Nov 10th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <locale>
  4.  
  5. int dots;
  6. int **graph;
  7.  
  8. struct root{
  9. int start;
  10. int end;
  11. int v;
  12. };
  13.  
  14. root *roots;
  15. root *ostgraph;
  16.  
  17. int main(){
  18. int counter = 0;
  19. setlocale(LC_ALL,"RUSSIAN");
  20. printf("Введите количество вершин в графе :");
  21. scanf("%d", &dots);
  22. graph = (int **)malloc(dots * sizeof(int));
  23. for (int i = 0; i < dots; i++)
  24. graph[i] = (int *)malloc(dots * sizeof(int));
  25. FILE *input = fopen("graph.txt", "r");
  26. if (input == NULL)
  27. printf("Невозможно открыть файл с графом \n");
  28. else{
  29. for (int i = 0; i < dots; i++){
  30. for (int j = 0; j < dots; j++){
  31. fscanf(input, "%d", &graph[i][j]);
  32. if (graph[i][j]>0)
  33. counter++;
  34. }
  35. }
  36. counter /= 2;
  37. printf("counter : %d \n", counter);
  38. roots = (root*)malloc(counter*sizeof(root)); // храним ребра
  39. ostgraph = (root*)malloc((dots - 1)*sizeof(root));
  40. int step = 0;
  41. bool target = true;
  42. for (int i = 0; i < dots; i++){
  43. for (int j = 0; j < dots; j++){
  44. if (graph[i][j]>0){
  45. for (int s = 0; s < counter; s++){
  46. if (roots[s].start == j && roots[s].end == i)
  47. target = false;
  48. }
  49. if (target == true){
  50. roots[step].start = i;
  51. roots[step].end = j;
  52. roots[step].v = graph[i][j];
  53. step++;
  54. }
  55. }
  56. target = true;
  57. }
  58. }
  59. //сортировка
  60. root tmp;
  61. for (int i = 0; i < counter - 1; i++){
  62. for (int j = 0; j < counter - i - 1; j++){
  63. if (roots[j].v > roots[j + 1].v){
  64. tmp.v = roots[j].v;
  65. tmp.start = roots[j].start;
  66. tmp.end = roots[j].end;
  67. roots[j].v = roots[j + 1].v;
  68. roots[j].start = roots[j + 1].start;
  69. roots[j].end = roots[j + 1].end;
  70. roots[j + 1].v = tmp.v;
  71. roots[j + 1].start = tmp.start;
  72. roots[j + 1].end = tmp.end;
  73. }
  74. }
  75. }
  76. for (int i = 0; i < counter; i++){
  77. printf("начало : %d конец: %d вес: %d \n", roots[i].start, roots[i].end, roots[i].v);
  78. }
  79. //сам корявый алгоритм сделанный на коленке
  80. target = true;
  81. step = 0;
  82. for (int i = 0; i < counter; i++){
  83. for (int j = 0; j < dots - 1; j++){
  84. if (roots[i].end == ostgraph[j].end){
  85. target = false;
  86. }
  87. }
  88. if (target == true){
  89. ostgraph[step].start = roots[i].start;
  90. ostgraph[step].end = roots[i].end;
  91. ostgraph[step].v = roots[i].v;
  92. step++;
  93. }
  94. target = true;
  95. }
  96. printf("\n\nНаш остовый граф: \n\n");
  97. for (int i = 0; i < dots-1; i++){
  98. printf("начало : %d конец: %d вес: %d \n", ostgraph[i].start, ostgraph[i].end, ostgraph[i].v);
  99. }
  100. //сохраняем в ебаный графиз мать его в кино водил
  101. FILE *inspect = fopen("graphviz.txt", "w+");
  102. fprintf(inspect, "graph G{\n");
  103. for (int i = 0; i < dots; i++){
  104. fprintf(inspect, " %d;\n", i);
  105. }
  106. target = false;
  107. for (int i = 0; i < dots; i++){
  108. for (int j = 0; j < dots; j++){
  109. if (graph[i][j]>0){
  110. for (int s = 0; s < dots - 1; s++){
  111. if (ostgraph[s].start == i && ostgraph[s].end == j)
  112. target = true;
  113. }
  114. if (target == true)
  115. fprintf(inspect, " %d -- %d [ label = \"%d\" color = \"RED\"];\n", i, j, graph[i][j]);
  116. else
  117. fprintf(inspect, " %d -- %d [ label = \"%d\" color = \"BLACK\"];\n", i, j, graph[i][j]);
  118. target = false;
  119. }
  120. }
  121. }
  122. fprintf(inspect, "}");
  123. fclose(inspect);
  124. }
  125. fclose(input);
  126. free(graph);
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment