Pagini recente » Cod sursa (job #907816) | Cod sursa (job #842436) | Cod sursa (job #2932768) | Cod sursa (job #631014) | Cod sursa (job #2928641)
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <bitset>
//#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define NMAX 200001
#define KMAX 105
#define LIM 1000
#define INF 1e9
#define LOG 17
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
struct triplet
{
int x, y, c;
bool operator < (triplet other) const
{
return other.c < c;
}
};
int n, m;
vector<pair<int, int>> G[NMAX];
vector<pair<int, int>> APM;
vector<int> T(NMAX, 0);
int TotalCost;
bool Visited[NMAX];
void Prim(int src)
{
priority_queue<triplet> pq;
// Initialize
for (auto Edge : G[src])
pq.push({ src, Edge.first, Edge.second });
Visited[src] = 1;
while (!pq.empty() && APM.size() != n - 1)
{
int NodeCur = pq.top().y;
if (!Visited[NodeCur])
{
APM.push_back({ pq.top().x, pq.top().y });
TotalCost += pq.top().c;
Visited[NodeCur] = 1;
for (auto Edge : G[NodeCur])
if (!Visited[Edge.first])
pq.push({ NodeCur, Edge.first, Edge.second });
}
pq.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
cin >> x >> y >> c;
G[x].push_back({ y, c });
G[y].push_back({ x, c });
}
Prim(1);
cout << TotalCost << '\n';
cout << APM.size() << '\n';
for (auto x : APM)
cout << x.first << ' ' << x.second << '\n';
return 0;
}