Pagini recente » Cod sursa (job #1740749) | Cod sursa (job #2258648) | Cod sursa (job #1737893) | Cod sursa (job #1281633) | Cod sursa (job #1499544)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <bitset>
using namespace std;
int H[200250*2], c[200001], P[200001], N, cnt = 0, M, par[200001], cost = 0;
bitset<200001> v;
vector<pair<int,int>> No[200001],apm;
const int inf = (1 << 30);
void per(int k){
while (k / 2 && c[H[k]] < c[H[k / 2]]){
swap(H[k], H[k / 2]);
swap(P[H[k]], P[H[k / 2]]);;
k /= 2;
}
}
void sift(int poz)
{
while ((poz * 2 <= cnt && c[H[poz]] > c[H[poz * 2]]) || (poz * 2 + 1 <= cnt && c[H[poz]] > c[H[poz * 2 + 1]]))
{
if (c[H[poz * 2]] < c[H[poz * 2 + 1]] || poz * 2 + 1 > cnt)
{
swap(H[poz], H[poz * 2]);
swap(P[H[poz]], P[H[poz * 2]]);
poz *= 2;
}
else
{
swap(H[poz], H[poz * 2 + 1]);
swap(P[H[poz]], P[H[poz * 2 + 1]]);
poz = poz * 2 + 1;
}
}
}
void add(int x){
H[++cnt] = x;
P[x] = cnt;
per(cnt);
}
int rad(){
int x = H[1];
swap(H[1], H[cnt]);
swap(P[H[1]], P[H[cnt]]);
--cnt;
sift(1);
P[x] = 0;
return x;
}
int main(){
freopen("apm.in", "r", stdin);
freopen("apm.out", "w", stdout);
scanf("%d%d", &N, &M);
for (int i = 2; i <= N; ++i){
c[i] = inf;
}
c[1] = 0;
for (int i = 1; i <= M; ++i){
int a, b, d;
scanf("%d%d%d", &a, &b, &d);
No[a].push_back(make_pair(b, d));
No[b].push_back(make_pair(a, d));
}
add(1);
while (cnt!=0){
int x = rad();
v[x] = 1;
cost += c[x];
for (auto& i : No[x]){
if (!v[i.first] && i.second < c[i.first]){
c[i.first] = i.second;
par[i.first] = x;
if (!P[i.first])add(i.first);
else per(P[i.first]);
}
}
}
printf("%d\n%d\n",cost,N-1);
for (int i = 2; i <= N; ++i)
printf("%d %d\n", par[i],i);
}