Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <functional>
  4. #include <map>
  5. using namespace std;
  6.  
  7. struct edge
  8. {
  9. int from;
  10. int to;
  11. int cost;
  12. edge(int a, int b, int c){from = a, to = b, cost = c;}
  13. };
  14.  
  15. int number_vertces;
  16. bool *visited;
  17. int arr[100][100];
  18. int main()
  19. {
  20. //freopen("inp.txt", "r", stdin);
  21.  
  22. map<int, int> abo;
  23.  
  24. int e, cc, F,T;
  25. //cout << "Enter number of v :\n";
  26. cin >> number_vertces;
  27. //cout << "Enter number of edges :\n";
  28. cin >> e;
  29. for (int i = 0; i < e; i++)
  30. {
  31. cin >> F >> T >> cc;
  32. arr[F][T] = arr[T][F]= cc;
  33. }
  34.  
  35. visited = new bool[number_vertces];
  36. vector<edge> MST;
  37. edge E(0,0,0);
  38. priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int> > > Q;
  39. Q.push(make_pair(0, 0));
  40. visited[0] = true;
  41. int v, CUR;
  42. pair<int, int> C;
  43. int cost = 0;
  44. for (int i = 0; i < number_vertces; i++)
  45. visited[i] = false;
  46.  
  47. while (!Q.empty())
  48. {
  49. C = Q.top();
  50.  
  51. Q.pop();
  52. if (!visited[C.second])
  53. {
  54. cost += C.first;
  55. edge k(C.second, abo[C.second], C.first);
  56. MST.push_back(k);
  57.  
  58. }
  59. visited[C.second] = true;
  60. //cout << "Cost = " << cost << endl;
  61.  
  62. for (int i = 0; i < number_vertces; i++)
  63. if (!visited[i] && arr[C.second][i] > 0)
  64. {
  65. Q.push(make_pair(arr[C.second][i], i));
  66. abo[i] = C.second;
  67. }
  68. }
  69.  
  70.  
  71. cout << cost << endl;
  72.  
  73. for (int i = 0; i < MST.size(); i++)
  74. {
  75. cout << MST[i].from << " " << MST[i].to << " " << MST[i].cost << endl;
  76. }
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement