Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: bradyawn
- PROG: cf
- LANG: C++11
- */
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <map>
- #include <utility>
- #include <algorithm>
- #include <set>
- #include <ctime>
- #include <queue>
- #include <stack>
- #define ll long long
- //#define inf cin
- //#define outf cout
- using namespace std;
- int main()
- {
- //ifstream inf("");
- //fstream outf("");
- //cout << setprecision(10);
- ll n, m;
- cin >> n >> m;
- vector<vector<ll>> graph(n, vector<ll>());
- vector<vector<ll>> prim(n, vector<ll>(n));
- for (int i = 0; i < m; i++)
- {
- int a, b, c;
- cin >> a >> b >> c;
- a--; b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- prim[a][b] = c;
- prim[b][a] = c;
- }
- vector<ll> nodes;
- vector<bool> vis(n);
- nodes.push_back(0);
- vis[0] = true;
- ll ret = 0;
- while (nodes.size() < n) //when amnt nodes reaches total nodes, mst found
- {
- ll toVis = -1;
- ll len = 1000001;
- for (auto e : nodes) //loop possible start nodes
- {
- for (auto u : graph[e]) //vis nodes adj to start
- {
- if (!vis[u]) //if not vis
- {
- if (prim[e][u] < len)
- {
- len = prim[e][u];
- toVis = u;
- }
- }
- }
- }
- ret += len;
- vis[toVis] = true;
- nodes.push_back(toVis);
- }
- cout << ret << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement