Pagini recente » Cod sursa (job #1185727) | Cod sursa (job #2868766) | Cod sursa (job #1597928) | Cod sursa (job #2642201) | Cod sursa (job #342346)
Cod sursa(job #342346)
#include<fstream>
#define dmax 200003
#define mmax 400003
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
int n,m,c[dmax],apm[dmax],nrm=1,cm;
struct m
{ int p1;
int p2;
int c;
} g[mmax],x;
void sortare(int st,int dr)
{ int i,j;
if(st<dr)
{ x=g[st];
i=st;
j=dr;
while(i<j)
{ while( (i<j)&&(g[j].c>=x.c) )
j--;
g[i]=g[j];
while( (i<j)&&(g[i].c<=x.c) )
i++;
g[j]=g[i];
}
g[i]=x;
sortare(st,i-1);
sortare(i+1,dr);
}
}
int main()
{ int i,bun,rau,j;
in>>n>>m;
for(i=0;i<m;i++)
in>>g[i].p1>>g[i].p2>>g[i].c;
in.close();
sortare(1,m);
for(i=1;i<=n;i++)
c[i]=i;
for(i=0;nrm<=n-1;i++)
{ if(c[g[i].p1]!=c[g[i].p2])
{ cm+=g[i].c;
apm[nrm]=i;
nrm++;
bun=min(c[g[i].p1],c[g[i].p2]);
rau=max(c[g[i].p1],c[g[i].p2]);
for(j=1;j<=n;j++)
if(c[j]==rau)c[j]=bun;
}
}
out<<cm<<'\n'<<nrm-1<<'\n';
for(i=1;i<=nrm-1;i++)
out<<g[apm[i]].p1<<" "<<g[apm[i]].p2<<'\n';
out.close();
return 0;
}