Pagini recente » test3452 | Cod sursa (job #2853562) | Cod sursa (job #592446) | Cod sursa (job #1913038) | Cod sursa (job #2752332)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int N = 200001;
const INF = 1e9;
int n, m, d[N], pred[N], cost;
vector <pair<int, int>> a[N];
bitset <N> selectat;
priority_queue <pair<int, int>> pq;
void apm()
{
pq.push({0, 1});
for (int i = 2; i <= n; ++i)
d[i] = INF;
int x;
while (!pq.empty())
{
x = pq.top().second;
pq.pop();
if (selectat[x])
continue;
selectat[x] = true;
cost += d[x];
for (auto p: a[x])
{
int y = p.first;
int c = p.second;
if (!selectat[y] && c < d[y])
{
pred[y] = x;
d[y] = c;
pq.push({-c, y});
}
}
}
}
int main()
{
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 << cost << '\n' << n - 1 << '\n';
for (int i = 2; i <= n; ++i)
{
out << i << ' ' << pred[i] << '\n';
}
return 0;
}