Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <functional>
- #include <map>
- using namespace std;
- struct edge
- {
- int from;
- int to;
- int cost;
- edge(int a, int b, int c){from = a, to = b, cost = c;}
- };
- int number_vertces;
- bool *visited;
- int arr[100][100];
- int main()
- {
- //freopen("inp.txt", "r", stdin);
- map<int, int> abo;
- int e, cc, F,T;
- //cout << "Enter number of v :\n";
- cin >> number_vertces;
- //cout << "Enter number of edges :\n";
- cin >> e;
- for (int i = 0; i < e; i++)
- {
- cin >> F >> T >> cc;
- arr[F][T] = arr[T][F]= cc;
- }
- visited = new bool[number_vertces];
- vector<edge> MST;
- edge E(0,0,0);
- priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int> > > Q;
- Q.push(make_pair(0, 0));
- visited[0] = true;
- int v, CUR;
- pair<int, int> C;
- int cost = 0;
- for (int i = 0; i < number_vertces; i++)
- visited[i] = false;
- while (!Q.empty())
- {
- C = Q.top();
- Q.pop();
- if (!visited[C.second])
- {
- cost += C.first;
- edge k(C.second, abo[C.second], C.first);
- MST.push_back(k);
- }
- visited[C.second] = true;
- //cout << "Cost = " << cost << endl;
- for (int i = 0; i < number_vertces; i++)
- if (!visited[i] && arr[C.second][i] > 0)
- {
- Q.push(make_pair(arr[C.second][i], i));
- abo[i] = C.second;
- }
- }
- cout << cost << endl;
- for (int i = 0; i < MST.size(); i++)
- {
- cout << MST[i].from << " " << MST[i].to << " " << MST[i].cost << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement