Pagini recente » Cod sursa (job #2178210) | Cod sursa (job #3039810) | Cod sursa (job #1476782) | Cod sursa (job #1000911) | Cod sursa (job #3214137)
#include <fstream>
#include <vector>
#define MAXN 200000
#define INF 1000000000
using namespace std;
ifstream cin ("apm.in");
ofstream cout ("apm.out");
int dist[MAXN + 10], parent[MAXN + 10];
vector <pair <int, int>> graph[MAXN + 10];
struct ura
{
int x, y;
};
ura ans[MAXN + 10];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
cin >> x >> y >> c;
graph[x].push_back({c, y});
graph[y].push_back({c, x});
}
for (int i = 2; i <= n; i++)
dist[i] = INF;
dist[1] = -INF;
int node = 1, costMin = 0 ;
for (int i = 2; i <= n; i++)
{
for (const auto &it : graph[node])
if (it.first < dist[it.second])
{
dist[it.second] = it.first;
parent[it.second] = node;
}
int minDist = INF, nextNode;
for (int j = 1; j <= n; j++)
if (dist[j] != -INF && dist[j] < minDist)
{
minDist = dist[j];
nextNode = j;
}
costMin = costMin + minDist;
ans[i - 1] = {parent[nextNode], nextNode};
dist[nextNode] = -INF;
node = nextNode;
}
cout << costMin << '\n' << n - 1 << '\n';
for (int i = 1; i < n; i++)
cout << ans[i].x << ' ' << ans[i].y << '\n';
return 0;
}