Pagini recente » Cod sursa (job #2802925) | Cod sursa (job #1678476) | Cod sursa (job #2650016) | Cod sursa (job #1519638) | Cod sursa (job #3186824)
#include <bits/stdc++.h>
#define N 200007
using namespace std;
int cost;
bitset<N> membru;
vector<pair<int,int>>a[N];
int n,m;
vector<pair<int,int>> ans;
void Adaugare(int nod,priority_queue< pair<int,pair<int,int>> >&pq);
int main()
{
ifstream fin("apm.in");ofstream fout("apm.out");
fin >> n >> m;
for(int i=1;i<=m;i++)
{
int x,y,c;
fin >> x >> y >> c;
a[x].push_back({y,c});
a[y].push_back({x,c});
}
fin.close();
membru[1]=1;
priority_queue< pair<int,pair<int,int>> >pq;
for(auto w:a[1])
pq.push( {-w.second,{1,w.first}} );
while( !pq.empty() )
{
int c=-pq.top().first;
int x=pq.top().second.first;
int y=pq.top().second.second;
pq.pop();
if( !membru[x] or !membru[y] )
{
cost+=c;
ans.push_back( {x,y} );
if( membru[x] )
Adaugare( y,pq );
else
Adaugare( x,pq );
}
}
fout << cost << "\n" << n-1 << "\n";
for(int i=0;i<ans.size();i++)
fout << ans[i].first << " " << ans[i].second << "\n";
return 0;
}
void Adaugare(int nod,priority_queue< pair<int,pair<int,int>> >&pq)
{
membru[nod]=1;
for( auto w: a[nod] )
if( !membru[ w.first ] )
{
pq.push( {-w.second,{w.first,nod}} );
}
}