Cod sursa(job #2215113)

Utilizator ShutterflyFilip A Shutterfly Data 21 iunie 2018 02:48:57
Problema Algoritmul lui Dijkstra Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 3.72 kb
#include <stdio.h>
#include <string.h>

#define MASK 131071

///implementare interactiva 2.0.0

struct NodeData {
    int Nod;
    int Path;
} heap[50001];

struct Muchie {
    int Vec;
    int Cost;
} muchii[250001];

int nod[50001], prevy[250001];

int inHeap[50001];

char buff[MASK];
int pos;
FILE *fin;
FILE *fout;

int poz[50001];
int heaplevel;
int heapsize;

void add(struct NodeData val);
void rmv(int nod);
void coborare(int nod);
void urcare(int nod);
void swap_nodes(int nod1,int nod2);

void add(struct NodeData val) {
    heap[++heapsize].Nod = val.Nod;
    heap[heapsize].Path = val.Path;
    inHeap[val.Nod] = heapsize;
    urcare(heapsize);
}

void rmv(int nod) {
    swap_nodes(nod, heapsize);
    heapsize--;
    coborare(nod);
    urcare(nod);
}

void coborare(int nod) {
    while ((heap[nod].Path > heap[nod * 2].Path || heap[nod].Path > heap[nod * 2 + 1].Path) && nod * 2 <= heapsize) {
        if (heap[nod * 2].Path < heap[nod * 2 + 1].Path) {
            swap_nodes(nod * 2, nod);
            nod = nod * 2;
        } else {
            swap_nodes(nod * 2 + 1, nod);
            nod = nod * 2 + 1;
        }
    }
}

void urcare(int nod) {
    while (heap[nod].Path < heap[nod / 2].Path && nod / 2 >= 1) {
        swap_nodes(nod / 2, nod);
        nod = nod / 2;
    }
}

void swap_nodes(int nod1,int nod2) {
    int temp = heap[nod1].Path;
    int tempNod = heap[nod1].Nod;
    int tmp = inHeap[nod1];
    inHeap[nod1] = inHeap[nod2];
    inHeap[nod2] = tmp;
    heap[nod1].Path = heap[nod2].Path;
    heap[nod1].Nod = heap[nod2].Nod;
    heap[nod2].Path = temp;
    heap[nod2].Nod = tempNod;
}

void reader (int* nr) {
    while(buff[pos] < '0' || buff[pos] > '9') {
        pos++;
        if (pos == MASK) {
            fread (buff, 1, MASK, fin);
            pos = 0;
        }
    }

    int temp = 0;
    while (buff[pos] >= '0' && buff[pos] <= '9') {
        temp = temp * 10 + (buff[pos] - '0');
        pos++;
        if (pos == MASK) {
            fread (buff, 1, MASK, fin);
            pos = 0;
        }
    }

    *nr = temp;
}

int main () {
    fin = fopen("dijkstra.in", "r");
    fout = fopen("dijkstra.out", "w");

    fread (buff, 1, MASK, fin);

    int Noduri, Muchii;
    reader(&Noduri);
    reader(&Muchii);
    int i;
    for (i = 0; i < Muchii; i++) {
        int orig, dest, cost;
        reader(&orig);
        reader(&dest);
        reader(&cost);

        heaplevel++;
        prevy[heaplevel] = nod[orig];
        nod[orig] = heaplevel;

        struct Muchie Cobai;
        Cobai.Vec = dest;
        Cobai.Cost = cost;
        muchii[heaplevel] = Cobai;
    }

    struct NodeData Cobai;
    Cobai.Nod = 1;
    Cobai.Path = 0;
    add(Cobai);

    for (i = 2; i <= Noduri; i++) {
        struct NodeData Cobai;
        Cobai.Nod = i;
        Cobai.Path = 1100000000;
        add(Cobai);
    }

    ///ce-i dezastru' asta nuclear
    while (heapsize >= 1) {
        int curNod = heap[1].Nod;
        int ind = nod[heap[1].Nod];
        rmv(1);
        while (ind) {
            int vecin = muchii[ind].Vec;

            if (heap[inHeap[vecin]].Path > muchii[ind].Cost + heap[inHeap[curNod]].Path) {
                heap[inHeap[vecin]].Path = muchii[ind].Cost + heap[inHeap[curNod]].Path;
                coborare(inHeap[vecin]);
                urcare(inHeap[vecin]);
            }

            ind = prevy[ind];
        }
    }

    int destin;
    for (destin = 2; destin <= Noduri; destin++) {
        if (heap[inHeap[destin]].Path > 1000000000)
            fprintf(fout, "0 ");
        else
            fprintf(fout, "%d ", heap[inHeap[destin]].Path);
    }

    return 0;
}