Pagini recente » Cod sursa (job #423620) | Cod sursa (job #149909) | Cod sursa (job #2545262) | Cod sursa (job #2314211) | Cod sursa (job #2352700)
#include <fstream>
#include <vector>
#include <queue>
#define INF 2000000001
using namespace std;
ifstream fi("apm.in");
ofstream fo("apm.out");
int n,m;
vector <pair <int, int> > A[200001];
vector <pair <int,int> > :: iterator it;
int KEY[200001];
int P[200001];
priority_queue <pair <int, int> > Q;
int nrm;
int VIZ[200001];
int main()
{
fi>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y,c;
fi>>x>>y>>c;
A[x].push_back({y,c});
A[y].push_back({x,c});
}
KEY[1]=0;
P[1]=0;
for(int i=2; i<=n; i++)
{
KEY[i]=INF;
P[i]=0;
}
for (int i=1;i<=n;i++)
Q.push({-KEY[i],i});
nrm=0;
while (1)
{
if (nrm==n-1)
break;
if (Q.empty())
break;
pair <int, int> curent;
curent=Q.top();
Q.pop();
int v;
v=curent.second;
if(VIZ[v]==1)
continue;
VIZ[v]=1;
nrm++;
for (it=A[v].begin();it!=A[v].end();it++)
{
pair <int, int> pereche;
pereche=(*it);
int varf=pereche.first;
int pondere=pereche.second;
if (pondere<KEY[varf] && VIZ[varf]==0)
{
KEY[varf]=pondere;
P[varf]=v;
Q.push({-pondere,varf});
}
}
}
int suma;
suma=0;
for (int i=1;i<=n;i++)
suma=suma+KEY[i];
fo<<suma<<"\n";
fo<<n-1<<"\n";
for (int i=2;i<=n;i++)
fo<<P[i]<<" "<<i<<"\n";
fi.close();
fo.close();
return 0;
}