Pagini recente » Cod sursa (job #785784) | Cod sursa (job #2065088) | Cod sursa (job #394470) | Cod sursa (job #1872332) | Cod sursa (job #2861325)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int N=2e5+10,INF=1e7+10;
vector <pair<int,int>> a[N];
priority_queue<pair<int,int>> pq;
int n,cost,pred[N],dist[N];
bool viz[N];
int main()
{
int m;
f>>n>>m;
for(int i=1;i<=n;i++)
{
dist[i]=INF;
}
dist[1]=0;
for(int i=1;i<=m;i++)
{
int x,y,z;
f>>x>>y>>z;
a[x].push_back({y,z});
a[y].push_back({x,z});
}
pq.push({0,1});
while(!pq.empty())
{
int x=pq.top().second;
pq.pop();
if(viz[x])
continue;
viz[x]=1;
for(auto i:a[x])
{
int y=i.first;
int c=i.second;
if(c<dist[y] && !viz[y])
{
dist[y]=c;
pred[y]=x;
pq.push({-c,y});
}
}
}
for(int i=1;i<=n;i++)
{
cost+=dist[i];
}
g<<cost<<"\n"<<n-1<<"\n";
for(int i=2;i<=n;i++)
{
g<<i<<" "<<pred[i]<<"\n";
}
return 0;
}