Pagini recente » Cod sursa (job #1736054) | Cod sursa (job #594092) | Cod sursa (job #1674124) | Cod sursa (job #1479993) | Cod sursa (job #1389545)
#include <stdio.h>
#include <vector>
#include <utility>
#include <queue>
#include <bitset>
#include <algorithm>
#define NMax 200000
using namespace std;
typedef struct
{
int x,y,cost;
}NOD;
struct CMP
{
bool operator()(const NOD A,const NOD B)
{
return (A.cost<B.cost)?false:true;
}
};
bool Compare(const NOD A,const NOD B)
{
return (A.cost>B.cost)?false:true;
}
std::vector< std::pair<int,int> > SUB;
std::vector<NOD> coada;
std::bitset<NMax> viz;
int n,m,x,y,cost;
void APM(int x0)
{
viz[x0] = true;
while(!coada.empty())
{
NOD aux;
for(int i=0;i<coada.size();++i)
{
aux = coada[i];
if((!viz[aux.x]&&viz[aux.y])||(viz[aux.x]&&!viz[aux.y]))
{
coada.erase(coada.begin()+i);
viz[aux.x] = true;
viz[aux.y] = true;
cost += aux.cost;
SUB.push_back( std::make_pair(aux.x,aux.y) );
break;
}
}
if(SUB.size()==n-1)break;
}
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=m;++i)
{
scanf("%d %d %d",&x,&y,&cost);
NOD aux;
aux.x = x;
aux.y = y;
aux.cost = cost;
coada.push_back( aux );
}
sort(coada.begin(),coada.end(),Compare);
cost = 0;
APM(1);
printf("%d\n%d\n",cost,SUB.size());
for(int i=0;i<SUB.size();++i)
{
printf("%d %d\n",SUB[i].first,SUB[i].second);
}
return 0;
}