Pagini recente » Cod sursa (job #549807) | Cod sursa (job #3177413) | Cod sursa (job #2431217) | Cod sursa (job #2983031) | Cod sursa (job #2688712)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int cost[200005],mcost[200005],f[200005];
int n,m,i,j,sum;
struct muchie
{
int nod;
int c;
};
muchie aux;
vector<muchie> G[200005];
int x,y,c;
int main()
{
fin>>n>>m;
while(m--)
{
fin>>x>>y>>c;
aux.c=c;
aux.nod=y;
G[x].push_back(aux);
aux.nod=x;
G[y].push_back(aux);
}
f[1]=1;
for(i=1;i<=n;i++)
cost[i]=1005;
for(i=0;i<G[1].size();i++)
{
if(G[1][i].c<cost[G[1][i].nod])
{
mcost[G[1][i].nod]=1;
cost[G[1][i].nod]=G[1][i].c;
}
}
for(j=1;j<n;j++)
{
int minn=1005;
for(i=1;i<=n;i++)
{
if(f[i]==0)
if(cost[i]<minn)
minn=cost[i];
}
for(i=1;i<=n;i++)
if(f[i]==0)
if(cost[i]==minn)
{
f[i]=1;
sum+=cost[i];
for(int l=0;l<G[i].size();l++)
if(f[G[i][l].nod]==0)
if(cost[G[i][l].nod]>G[i][l].c)
{
mcost[G[i][l].nod]=i;
cost[G[i][l].nod]=G[i][l].c;
}
break;
}
}
fout<<sum<<"\n";
fout<<n-1<<"\n";
for(i=2;i<=n;i++)
fout<<i<<" "<<mcost[i]<<"\n";
fin.close();
fout.close();
return 0;
}