Pagini recente » Cod sursa (job #1949328) | Cod sursa (job #3285052)
#include <fstream>
#include <vector>
#include <queue>
#define nmax (int)(2e5+1)
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int n,m,cost,x,y,c;
bool viz[nmax];
vector<pair<int,int>>apm,v[nmax];
struct muchie{
int x,y,c;
};
struct cmp{
bool operator()(const muchie& a,const muchie& b){
return a.c>b.c;
}
};
priority_queue<muchie,vector<muchie>,cmp>q;
void prim(){
q.push({1,0,0});
while(!q.empty()){
int nod=q.top().x,t=q.top().y,c=q.top().c;
q.pop();
if(viz[nod])
continue;
viz[nod]=1;
if(t!=0)
apm.push_back({nod,t});
cost+=c;
for(auto i:v[nod])
if(!viz[i.first])
q.push({i.first,nod,i.second});
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y>>c;
v[x].push_back({y,c});
v[y].push_back({x,c});
}
prim();
cout<<cost<<'\n'<<n-1<<'\n';
for(auto i:apm)
cout<<i.first<<" "<<i.second<<'\n';
return 0;
}
/// Prim