Pagini recente » Cod sursa (job #2536934) | Cod sursa (job #3279477) | Cod sursa (job #2653786) | Cod sursa (job #287709) | Cod sursa (job #3133453)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
const int N = 2e5;
const int INF = 1e9;
struct vecin
{
int vf, cost;
};
int n, total;
vector <vecin> a[N+1];
vector <int> pred;
void prim()
{
vector <int> d(n+1);
pred.resize(n+1);
bitset <N+1> selectat;
d[1] = 0;
for (int i = 2; i <= n; i++)
{
d[i] = INF;
}
priority_queue < pair<int, int>> h;
h.push({0, 1});
while (!h.empty())
{
int x = h.top().second;
h.pop();
if (selectat[x])
{
continue;
}
selectat[x] = 1;
total += d[x];
for (auto v: a[x])
{
int y = v.vf, c = v.cost;
if (selectat[y])
{
continue;
}
if (c < d[y])
{
d[y] = c;
pred[y] = x;
h.push({-d[y], y});
}
}
}
}
int main()
{
ifstream in("apm.in");
ofstream out("apm.out");
int m;
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y, c;
in >> x >> y >> c;
a[x].push_back({y, c});
a[y].push_back({x, c});
}
prim();
out << total << "\n" << n - 1 << "\n";
for (int i = 2; i <= n; i++)
{
out << pred[i] << " " << i << "\n";
}
in.close();
out.close();
return 0;
}