Pagini recente » Cod sursa (job #2589932) | Cod sursa (job #2068347) | Cod sursa (job #2988032) | Cod sursa (job #726805) | Cod sursa (job #2965046)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
set <pair <int,int> > s;
vector <pair <int,int> > v[200005];
int tot_dist,viz[200005],dist[200005],tata[200005];
int n,m,i,x,y,cost;
int oo=2000000009;
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>x>>y>>cost;
v[x].push_back({y,cost});
v[y].push_back({x,cost});
}
for(int i=1; i<=n; i++)
dist[i]=oo;
dist[1]=0;
s.insert({0,1}); ///dist,nod
while(s.empty()==0)
{
int nod=s.begin()->second;
int dist_nod=s.begin()->first;
s.erase(s.begin());
if(viz[nod]==0)
{
viz[nod]=1;
tot_dist+=dist_nod;
for(auto k:v[nod])
{
if(dist[k.first]>k.second && viz[k.first]==0)
{
dist[k.first]=k.second;
s.insert({dist[k.first],k.first});
tata[k.first]=nod;
}
}
}
}
fout<<tot_dist<<'\n';
fout<<n-1<<'\n';
for(int i=2; i<=n; i++)
fout<<i<<" "<<tata[i]<<'\n';
return 0;
}