Pagini recente » Cod sursa (job #556337) | Cod sursa (job #342583) | Cod sursa (job #2072245) | Cod sursa (job #800890) | Cod sursa (job #2408226)
#include <bits/stdc++.h>
#define NM 200005
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
void read();
struct edge
{
int x, y, c;
};
struct cmp
{
bool operator()(const edge& a, const edge& b)
{
return a.c > b.c;
}
};
void read();
int n, m, s;
bitset<NM> viz;
vector<pair<int,int>> v[NM], sol;
priority_queue<edge, vector<edge>, cmp> q;
int main()
{
read();
for(auto it : v[1])
q.push({1, it.first, it.second});
viz[1] = 1;
while(!q.empty())
{
edge cur = q.top();
q.pop();
if(viz[cur.y])
continue;
else
{
sol.push_back({cur.x, cur.y});
s+=cur.c;
viz[cur.y] = 1;
for(auto it : v[cur.y])
if(!viz[it.first])
q.push({cur.y, it.first, it.second});
}
}
fout << s << '\n' << n-1 << '\n';
for(auto it : sol)
fout << it.first << ' ' << it.second << '\n';
return 0;
}
void read()
{
fin >> n >> m;
for(int i=1; i<=m; i++)
{
int x, y, c;
fin >> x >> y >> c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
}