Cod sursa(job #2259258)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 13 octombrie 2018 10:56:31
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
#include <cstdio>
#include <cstdlib>
#define MaxN 200005
#define MaxM 400005
#define min(x, y) (x<y?x:y)
#define swap(x, y) {int aux; aux=x; x=y; y=aux;}
using namespace std;
int i, N, M, Heap[MaxM], Vf, WorkHeap[MaxM];
bool Find[MaxN];
struct Pair{
    int first;
    int second;
}P;
#define PSwap(x, y) {Pair aux; aux=x; x=y; y=aux;}
Pair CList[MaxM];
Pair WorKList[MaxM];
void Read_Sort();
void HeapSort();
void Sift(int i);
void Percolate(int i);
int main()
{
    freopen("apm.in", "r", stdin);
    freopen("apm.out", "w", stdout);
    Read_Sort();

    return 0;
}
void Read_Sort(){
    scanf("%d%d", &N, &M);
    Vf=M;
    for(i=1; i<=M; ++i){
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        CList[i].first=x;
        CList[i].second=y;
        Heap[i]=z;
        Percolate(i);
    }

    for(i=1; i<M; ++i){
        WorkHeap[i]=Heap[1];
        WorKList[i]=CList[1];
        Heap[1]=Heap[Vf];
        CList[1]=CList[Vf];
        Vf--;
        Sift(1);
    }
    WorkHeap[M]=Heap[1];
    WorKList[M]=CList[1];

    return;
}
void Percolate(int i){
    bool k=0;
    while(i>1 && k==0){
        k=1;
        if(Heap[i/2]>Heap[i]){
            swap(Heap[i], Heap[i/2]);
            PSwap(CList[i], CList[i/2]);
            i/=2;
            k=0;
        }
    }
    return;
}
void Sift(int i){
    bool k=0;
    while(i*2<=Vf && k==0){
        k=1;
        if(i*2==Vf && Heap[i]>Heap[i*2]){
            swap(Heap[i], Heap[i*2]);
            PSwap(CList[i], CList[i*2]);
            i*=2;
            k=0;
        }
        else if(Heap[i]>min(Heap[i*2], Heap[i*2+1])){
            int a=(Heap[i*2]<Heap[i*2+1]?i*2:i*2+1);
            swap(Heap[i], Heap[a]);
            PSwap(CList[i], CList[a]);
            i=a;
            k=0;
        }
    }
}