Pagini recente » Cod sursa (job #1801396) | Cod sursa (job #1855201) | Cod sursa (job #1494774) | Cod sursa (job #2801236) | Cod sursa (job #3337140)
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,m,i,j,x,y,c;
int d[200005],t[200005],v[200005];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>q;
vector<pair<int,int>>G[200005];
void bfs(int nod)
{
d[nod]=0;
t[nod]=0;
q.push({0,nod});
while(!q.empty())
{
int j=q.top().second;
int cost=q.top().first;
q.pop();
if(v[j]==1)
continue;
v[j]=1;
for(i=0;i<G[j].size();i++)
{
auto x=G[j][i];
if(d[x.first]>x.second && v[x.first]==0)
{
d[x.first]=x.second;
t[x.first]=j;
q.push({d[x.first],x.first});
}
}
}
}
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y>>c;
G[x].push_back({y,c});
G[y].push_back({x,c});
}
for(i=1;i<=n;i++)
{
d[i]=inf;
}
bfs(1);
int s=0;
for(i=2;i<=n;i++)
{
if(d[i]==inf)
d[i]=0;
s=s+d[i];
}
fout<<s<<"\n"<<n-1<<"\n";
for(i=2;i<=n;i++)
{
fout<<t[i]<<" "<<i<<"\n";
}
return 0;
}