Cod sursa(job #2072402)

Utilizator vazanIonescu Victor Razvan vazan Data 21 noiembrie 2017 20:21:47
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.86 kb
#include<cstdio>
#include<algorithm>
using namespace std;

#define NMAX 50100
#define MMAX 250100
int val[2 * MMAX], nxt[2 * MMAX], last[NMAX], len[2 * MMAX];
int cnt = 0;

int add(int a, int b, int c){
    cnt++;
    val[cnt] = b;
    len[cnt] = c;
    nxt[cnt] = last[a];
    last[a] = cnt;
}


int heap[4 * NMAX], hsize = 0, dist[NMAX], viz[NMAX], inheap[NMAX];
const int INF = 2000000000;

bool debug(){
    for(int i = 1; i <= NMAX; i++)
        if(inheap[i] != 0 && heap[inheap[i]] != i)
            return 1;
    return 0;
}

void push_h(int x){
    if(hsize == 0){
        heap[1] = x;
        inheap[x] = 1;
        hsize = 1;
        return;
    }
    int cr;
    if(inheap[x])
        cr = inheap[x];
    else{
        inheap[x] = hsize + 1;
        cr = hsize + 1;
        hsize++;
    }
    /*cr = hsize + 1;
    hsize++;*/
    heap[cr] = x;
    while(dist[heap[cr]] < dist[heap[cr / 2]]){
        swap(heap[cr], heap[cr / 2]);
        swap(inheap[heap[cr]], inheap[heap[cr / 2]]);
        cr = cr / 2;
    }
}

void pop_h(){
    swap(inheap[heap[1]], inheap[heap[hsize]]);
    swap(heap[1], heap[hsize]);
    inheap[heap[hsize]] = 0;
    heap[hsize] = 0;
    hsize--;
    int cr = 1;
    while(1){
        if(dist[heap[cr]] > dist[heap[cr * 2]]){
            if(dist[heap[cr * 2]] > dist[heap[cr * 2 + 1]]){
                swap(heap[cr], heap[cr * 2  + 1]);
                swap(inheap[heap[cr]], inheap[heap[cr * 2 + 1]]);
                cr = cr * 2 + 1;
            }
            else{
                swap(heap[cr], heap[cr * 2]);
                swap(inheap[heap[cr]], inheap[heap[cr * 2]]);
                cr = cr * 2;
            }
        }
        else if(dist[heap[cr]] > dist[heap[cr * 2 + 1]]){
            swap(heap[cr], heap[cr * 2 + 1]);
            swap(inheap[heap[cr]], inheap[heap[cr * 2 + 1]]);
            cr = cr * 2 + 1;
        }
        else{
            break;
        }
    }
}

int top_h(){
    return heap[1];
}


int main(){
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);
    int n, m, a, b, c;
    scanf("%d%d", &n, &m);
    for(int i = 0; i <= n; i++)
        dist[i] = INF;
    dist[1] = 0;
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        //add(b, a, c);
    }
    push_h(1);
    while(hsize > 0){
        int k = top_h(), mch = last[k];
        viz[k] = 1;
        while(mch != 0){
            int u = val[mch], d = dist[k] + len[mch];
            if(d < dist[u] && !viz[u]){
                dist[u] = d;
                push_h(u);
            }
            mch = nxt[mch];
        }
        pop_h();
    }
    for(int i = 2; i <=n; i++){
        if(dist[i] == INF)
            printf("0 ");
        else
            printf("%d ", dist[i]);
    }
    return 0;
}