Pagini recente » Cod sursa (job #2469251) | Cod sursa (job #920195) | Cod sursa (job #399757) | Cod sursa (job #1562610) | Cod sursa (job #2999670)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
vector <pair <int,int> > v[250005];
set <pair <int,int > > st;
int dist[200005],viz[200005],totdist,tata[250005];
int n,m;
const int oo=2000000001;
void citire()
{
fin>>n>>m;
int x,y,cost;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>cost;
v[x].push_back({y,cost});
v[y].push_back({x,cost});
}
}
void init()
{
for(int i=1; i<=n; i++)
dist[i]=oo;
dist[1]=0;
st.insert({0,1});
}
void apm()
{
while(st.empty()==0)
{
int nod=st.begin()->second;
int di=st.begin()->first;
st.erase(st.begin());
if(viz[nod]==0)
{
viz[nod]=1;
totdist+=di;
for(auto k:v[nod])
{
if(dist[k.first]>k.second && viz[k.first]==0)
{
//st.erase({dist[k.first],k.first});
dist[k.first]=k.second;
st.insert({dist[k.first],k.first});
tata[k.first]=nod;
}
}
}
}
}
void afisare()
{
fout<<totdist<<'\n';
fout<<n-1<<'\n';
for(int i=2; i<=n; i++)
{
fout<<i<<" "<<tata[i]<<'\n';
}
}
int main()
{
citire();
init();
apm();
afisare();
return 0;
}