Pagini recente » Cod sursa (job #3206954) | Cod sursa (job #2345899) | Cod sursa (job #1613146) | Cod sursa (job #9996) | Cod sursa (job #3123541)
#include<fstream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
const int N=2e5;
struct edges
{
int x;
int y;
int c;
bool operator<(const edges &other) const
{
return c > other.c;
}
};
priority_queue<edges> pq;
vector<pair<int, int>> v[N+1];
pair<int, int> result[N+1];
bool viz[N+1];
int main()
{
int n, m;
long long costuri =0;
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int from, to, cost;
cin >> from >> to >> cost;
v[from].push_back(make_pair(to, cost));
v[to].push_back(make_pair(from, cost));
}
viz[1]=true;
for(auto x: v[1])
pq.push({1, x.first, x.second});
int parcurgem=1;
while(parcurgem < n)
{
edges e = pq.top();
pq.pop();
if(!viz[e.y])
{
viz[e.y]=true;
result[parcurgem]=make_pair(e.x, e.y);
parcurgem++;
costuri+=e.c;
for(auto x: v[e.y])
{
if(!viz[x.first])
pq.push({e.y, x.first, x.second});
}
}
}
cout << costuri << '\n';
cout << parcurgem-1 << '\n';
for(int i=1; i<=parcurgem-1; i++)
{
cout << result[i].first << " " << result[i].second << '\n';
}
return 0;
}