Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <chrono>
  7. using namespace std;
  8. using namespace chrono;
  9.  
  10. struct edge
  11. {
  12. int i;
  13. int j;
  14. int weight;
  15. };
  16. int findMin(vector<edge> graph)
  17. {
  18. int indexMin = 0;
  19. for (int i = 0; i < graph.size(); i++)
  20. {
  21. if (graph[i].weight < graph[indexMin].weight)
  22. {
  23. indexMin = i;
  24. }
  25. }
  26. return indexMin;
  27. }
  28. int main()
  29. {
  30. int size = 0;
  31. cout << "Please, enter size: ";
  32. cin >> size;
  33. if (!cin)
  34. exit(1);
  35. vector<edge> graph;
  36. vector<edge> tree;
  37. for (int i = 0; i < size; i++)
  38. {
  39. edge buffer;
  40. cout << "Please, enter edge(i j weight): ";
  41. cin >> buffer.i >> buffer.j >> buffer.weight;
  42. graph.push_back(buffer);
  43. }
  44. if (!cin)
  45. exit(1);
  46. high_resolution_clock::time_point cl1 = high_resolution_clock::now();
  47. while (graph[findMin(graph)].weight != INT_MAX)
  48. {
  49. int start = 0;
  50. do
  51. {
  52. int index = findMin(graph);
  53. bool i = 0, j = 0;
  54. for (auto x : tree)
  55. {
  56. if (x.j == graph[index].i || x.i == graph[index].i)
  57. {
  58. i = true;
  59. }
  60. if (x.j == graph[index].j || x.i == graph[index].j)
  61. {
  62. j = true;
  63. }
  64. }
  65. if (i && j)
  66. {
  67. graph[index].weight = INT_MAX;
  68. break;
  69. }
  70. tree.push_back(graph[index]);
  71. graph[index].weight = INT_MAX;
  72. break;
  73. } while (true);
  74. }
  75. high_resolution_clock::time_point cl2 = high_resolution_clock::now();
  76. cout << endl;
  77. for (auto x : tree)
  78. {
  79. cout << x.i << ' ' << x.j << ' ' << x.weight << endl;
  80. }
  81. int weight = 0;
  82. for (auto x : tree)
  83. {
  84. weight += x.weight;
  85. }
  86. cout << "Weight: " << weight << endl;
  87. std::cout << "Time: " << (duration_cast<duration<double>>(cl2 - cl1)).count() << " sec." << endl;
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement