Pagini recente » Cod sursa (job #1879916) | Cod sursa (job #742774) | Cod sursa (job #2263390) | Cod sursa (job #937384) | Cod sursa (job #3168189)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int Infinit = 10000000;
const int NMAX = 200000;
priority_queue<pair<int, int>> pq;
vector<pair<int, int>>G[NMAX + 1];
int d[NMAX + 1], tata[NMAX + 1], viz[NMAX + 1];
ifstream f("apm.in");
ofstream g("apm.out");
int main() {
int n, m;
f >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, c;
f >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
for (int i = 1; i <= n; i++) {
d[i] = Infinit;
viz[i] = 0;
}
d[1] = 0;
pq.push({d[1], 1});
int s = 0;
while(!pq.empty()){
int x = pq.top().second;
pq.pop();
if(viz[x]) {
continue;
}
viz[x] = 1;
s = s + d[x];
for(auto next : G[x]){
if(viz[next.first] == 0 && d[next.first] > next.second){
d[next.first] = next.second;
tata[next.first] = x;
pq.push({d[next.first], next.first});
}
}
}
g << s << endl;
for(int i = 2; i <= n; i++) {
g << tata[i] << " " << i << endl;
}
}