Pagini recente » Cod sursa (job #2588200) | Cod sursa (job #1647080) | Cod sursa (job #2812242) | Cod sursa (job #358794) | Cod sursa (job #3132951)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
string file = "apm";
ifstream cin (file + ".in");
ofstream cout (file + ".out");
const int N = 2e5;
struct muchie{
int vf;
int cost;
bool operator < (const muchie &y) const
{
return cost > y.cost;
}
};
vector <muchie> a[N+1];
bitset <N+1> in_t;
int n,predecesor[N+1];
int prim()
{
priority_queue <muchie> h;
h.push({1,0});
int cost_total = 0;
while (!h.empty())
{
int x = h.top().vf, y = h.top().cost;
h.pop();
if (in_t[x])
{
continue;
}
cost_total += y;
in_t[x] = 1;
for (auto e : a[x])
{
int c = e.cost, y = e.vf;
if (!in_t[y])
{
predecesor[y] = x;
h.push({y,c});
}
}
}
return cost_total;
}
int main ()
{
int m,x,y,z;
cin >> n >> m;
while (m--)
{
cin >> x >> y >> z;
a[x].push_back({y,z});
a[y].push_back({x,z});
}
cout << prim() << '\n' << n-1;
for (int i=2; i<=n; i++)
{
cout << '\n' << predecesor[i] << ' ' << i;
}
}