Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : Prim
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 30 July 2022 [09:03]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- int v,w;
- bool operator < (const A&o) const{
- return w>o.w;
- }
- };
- priority_queue<A > heap;
- vector<A > g[100010];
- int mark[100010];
- int main(){
- cin.tie(0)->sync_with_stdio(0);
- cin.exceptions(cin.failbit);
- int n,m,u,v,w;
- cin >> n >> m;
- for(int i=1;i<=m;i++){
- cin >> u >> v >> w;
- g[u].push_back({v,w});
- g[v].push_back({u,w});
- }
- mark[1] = 1;
- for(auto x:g[1])
- heap.push(x);
- int sum = 0;
- while(!heap.empty()){
- A now = heap.top();
- heap.pop();
- if(mark[now.v]) continue;
- sum+=now.w;
- mark[now.v] = 1;
- for(auto x:g[now.v]){
- if(mark[x.v]) continue;
- heap.push(x);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment