Pagini recente » Cod sursa (job #3234861) | Cod sursa (job #1241416) | Cod sursa (job #391810) | Cod sursa (job #568142) | Cod sursa (job #995573)
Cod sursa(job #995573)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
vector <pair<int,int> > a[200010];
pair <int,int> per;
int s=10000,rez[200010],i,j,k,v[200010],viz[200010],h[200010],n,poz[200010],m,x,y,b,z,c;
void verif(int nod)
{
int aux,nr;
nod=poz[nod];
while(v[h[nod]]<v[h[nod/2]])
{
aux=h[nod];
h[nod]=h[nod/2];
h[nod/2]=aux;
poz[h[nod]]=nod;
poz[h[nod/2]]=nod/2;
nod/=2;
}
while(v[h[nod]]>v[h[nod*2]]||v[h[nod]]>v[h[nod*2+1]])
{
if(v[h[nod*2]]<v[h[nod*2+1]])
nr=nod*2;
else
nr=nod*2+1;
aux=h[nod];
h[nod]=h[nr];
h[nr]=aux;
poz[h[nod]]=nod;
poz[h[nr]]=nr;
nod=nr;
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y>>z;
a[x].push_back(pair<int,int>(y,z));
a[y].push_back(pair<int,int>(x,z));
}
h[1]=1;
v[1]=-10000;
for(i=2;i<=n;i++)
{
h[i]=i;
poz[i]=i;
v[i]=10000;
}
v[0]=100000;
v[200001]=-100000;
h[0]=200001;
poz[1]=1;
k=n;
z=0;
while(k)
{
x=h[1];
viz[x]=1;s+=v[x];
rez[x]=z;
z=x;
h[1]=h[k];
h[k]=0;
poz[h[1]]=1;
verif(h[1]);
k--;
for(vector<pair<int,int> >::iterator it=a[x].begin();it!=a[x].end();it++)
{
per=*it;
c=per.first;
b=per.second;
if(!viz[c]&&b<v[c])
{
v[c]=b;
verif(c);
}
}
}
g<<s<<'\n'<<n-1<<'\n';
while(rez[z])
{
g<<z<<" "<<rez[z]<<'\n';
z=rez[z];
}
return 0;
}