Pagini recente » Cod sursa (job #2520951) | Cod sursa (job #2356125) | Cod sursa (job #1371797) | Cod sursa (job #2346083) | Cod sursa (job #2861324)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int N=2e5+1;
const int INF=1e5;
int d[N],pred[N],n;
bool in_arb[N];
vector<pair<int,int>> a[N];
int prim()
{
for(int i=2; i<=n; i++)
{
d[i]=INF;
in_arb[i]=false;
}
d[1]=0;
priority_queue<pair<int,int>> h;
h.push({0,1});
int cost=0;
while(!h.empty())
{
int x=h.top().second;
h.pop();
if(in_arb[x])
{
continue;
}
in_arb[x]=true;
cost+=d[x];
for(auto p: a[x])
{
int y=p.first,c=p.second;
if(in_arb[y])
{
continue;
}
if(c<d[y])
{
d[y]=c;
pred[y]=x;
h.push({-c,y});
}
}
}
return cost;
}
int main()
{
int m;
in>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y,c;
in>>x>>y>>c;
a[x].push_back({y,c});
a[y].push_back({x,c});
}
out<<prim()<<"\n";
out<<n-1<<"\n";
for(int i=2; i<=n; i++)
{
out<<i<<" "<<pred[i]<<"\n";
}
return 0;
}