Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int number_of_vertices = 0;
  7. int number_of_faces = 0;
  8. int infinity = 10000;
  9. scanf("%d", &number_of_vertices);
  10. scanf("%d", &number_of_faces);
  11. int x, y, length;
  12. int way[number_of_vertices][number_of_vertices];
  13. int attendance[number_of_vertices];
  14. int the_shortest_way[number_of_vertices];
  15.  
  16. for (int i = 1; i <= number_of_vertices; i++)
  17. {
  18. for (int j = 1; j <= number_of_vertices; j++)
  19. {
  20. way[i][j] = 0;
  21. }
  22. }
  23. for (int i = 1; i <= number_of_faces; i++)
  24. {
  25. scanf("%d%d%d", &x, &y, &length);
  26. way[x][y] = length;
  27. way[y][x] = length;
  28. }
  29. for (int i = 1; i <= number_of_vertices; i++)
  30. {
  31. for (int j = 1; j <= number_of_vertices; j++)
  32. {
  33. printf("%3d ",way[i][j]);
  34. }
  35. printf("\n");
  36. }
  37.  
  38. for (int i = 1; i <= number_of_vertices; i++)
  39. {
  40. attendance[i] = 0;
  41. }
  42.  
  43. for (int i = 1; i <= number_of_vertices; i++)
  44. {
  45. the_shortest_way[i] = infinity;
  46. }
  47.  
  48. int element;
  49. scanf("%d", &element);
  50.  
  51. the_shortest_way[element] = 0;
  52.  
  53. for (int i = 1; i <= number_of_vertices; i++)
  54. {
  55. printf("%d ",the_shortest_way[i]);
  56. }
  57.  
  58. int temp;
  59. int minindex;
  60. int min;
  61. do {
  62. minindex = infinity;
  63. min = infinity;
  64. for (int i = 1; i <= number_of_vertices; i++)
  65. {
  66. if ((attendance[i] == 0) && (the_shortest_way[i] < min))
  67. {
  68. min = the_shortest_way[i];
  69. minindex = i;
  70. }
  71. }
  72. if (minindex != infinity)
  73. {
  74. for (int i = 1; i <= number_of_vertices; i++)
  75. {
  76. if (way[minindex][i] > 0)
  77. {
  78. temp = min + way[minindex][i];
  79. if (temp < the_shortest_way[i])
  80. {
  81. the_shortest_way[i] = temp;
  82. }
  83. }
  84. }
  85. attendance[minindex] = 1;
  86. }
  87. } while (minindex < 10000);
  88. printf("\nКратчайшие расстояния до вершин: \n");
  89. for (int i = 1; i <= number_of_vertices; i++)
  90. printf("%d ", the_shortest_way[i]);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement