Pagini recente » Cod sursa (job #3187720) | Cod sursa (job #794388) | Cod sursa (job #1770561) | tema | Cod sursa (job #683027)
Cod sursa(job #683027)
#include<fstream.h>
#include<iostream.h>
ifstream f("apm.in");ofstream go("apm.out");
int n,m;
struct graf {int x,y,c;} g[2000],aj;
void citire()
{
int i=1;f>>n>>m;for(i=1;i<=m;i++) f>>g[i].x>>g[i].y>>g[i].c;
}
void sortare()
{
int i,j;
for(i=1;i<m;i++)
{for(j=i+1;j<=m;j++)
{if(g[i].c>g[j].c)
{aj=g[i];g[i]=g[j];g[j]=aj;}}}
}
void pro()
{
int L[2000],i,j,k=0,s=0;
for(i=1;i<=n;i++) L[i]=i;
i=1;
while(k<n-1)
{
if(L[g[i].x]!=L[g[i].y])
{
L[g[i].x]=L[g[i].y];k++;s+g[i].c;
for(j=1;j<=m;j++) { if(L[j]==g[i].x) L[i]=g[i].y;}
}
i++;
}
i=1;
while(k<n-1)
{
if(L[g[i].x]!=L[g[i].y]) {L[g[i].x]=L[g[i].y];k++;s+g[i].c;go<<g[i].x<<' '<<g[i].y<<endl;
for(j=1;j<=m;j++) { if(L[j]==g[i].x) L[i]=g[i].y;}}
i++;
}
}
int main()
{
citire();sortare();pro();return 0;
}