Pagini recente » Borderou de evaluare (job #1957824) | Borderou de evaluare (job #2385521) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3336588)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct edge{
int i, j, w;
};
struct cmp{
bool operator() (edge x, edge y)
{
return x.w > y.w;
}
};
int n, m, total;
vector <edge> vecini[200001], mst;
priority_queue <edge, vector <edge>, cmp> pq;
bitset <200001> viz;
int main()
{
f>>n>>m;
for (int i=1;i<=m;i++)
{
int x, y, w;
f>>x>>y>>w;
vecini[x].push_back({x, y, w});
vecini[y].push_back({y, x, w});
}
viz[1] = 1;
for (auto e : vecini[1])
pq.push(e);
while (!pq.empty() and mst.size() < n)
{
edge e = pq.top();
pq.pop();
if (viz[e.j] == 0)
{
int i = e.j;
viz[i] = 1;
total += e.w;
mst.push_back(e);
for (auto e : vecini[i])
pq.push(e);
}
}
g<<total;
}