Pagini recente » Cod sursa (job #3193872) | Cod sursa (job #363192) | Cod sursa (job #245390) | Cod sursa (job #1000967) | Cod sursa (job #3040606)
/// prim
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("apm.in");
ofstream out ("apm.out");
const int max_size = 2e5 + 1, INF = 1e9 + 1;
struct str{
int x, cost;
bool operator < (const str & aux) const
{
return cost > aux.cost;
}
};
int d[max_size], uz[max_size], rez, n, t[max_size];
vector <pair <int, int>> mc[max_size], ans;
priority_queue <str> pq;
void djk ()
{
for (int i = 1; i <= n; i++)
{
d[i] = INF;
}
d[1] = 0;
pq.push({1, 0});
while (!pq.empty())
{
int nod = pq.top().x, val = pq.top().cost;
pq.pop();
if (uz[nod])
{
continue;
}
uz[nod] = 1;
rez += val;
if (nod != 1)
{
ans.push_back({t[nod], nod});
}
for (auto f : mc[nod])
{
if (f.second < d[f.first] && !uz[f.first])
{
t[f.first] = nod;
pq.push({f.first, f.second});
}
}
}
}
int main ()
{
int m;
in >> n >> m;
while (m--)
{
int x, y, c;
in >> x >> y >> c;
mc[x].push_back({y, c});
mc[y].push_back({x, c});
}
djk();
out << rez << '\n' << ans.size() << '\n';
for (auto f : ans)
{
out << f.first << " " << f.second << '\n';
}
in.close();
out.close();
return 0;
}