Cod sursa(job #2037313)

Utilizator CammieCamelia Lazar Cammie Data 11 octombrie 2017 23:43:08
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda hlo_cj_av_l2 Marime 2.41 kb
#include <fstream>
#include <cstring>
#include <vector>

#define inf 0x3f3f3f3f
#define MAXN 2002

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

struct two{
    int nod, c;
};

struct two2{
    int node, pr;
};

vector <two> G[MAXN];
two2 heap[MAXN * 75];

int n, m, k, prieten[MAXN], x, z, sol[MAXN][25], viz[MAXN][25], nn, aux, nr_p;

inline void Read() {
    int y;

    fin >> n >> m >> k;

    for (int i = 1; i <= k; i++) {
        fin >> x; prieten[x] = 1;
    }

    for (int i = 1; i <= m; i++) {
        fin >> x >> y >> z;

        G[x].push_back({y, z});
        G[y].push_back({x, z});
    }
}

inline void Init() {
    memset(sol, inf, sizeof(sol));
}

inline void Up(int son) {
    int father;

    while (son > 1) {
        father = son / 2;
        if (sol[heap[father].node][heap[father].pr] > sol[heap[son].node][heap[son].pr]) {
            swap(viz[father], viz[son]);
            swap(heap[father], heap[son]);

            son = father;
        }
        else
            return;
    }
}

inline void Down(int father) {
    int left_son, right_son, pozz;

    while (father * 2 <= nn) {
        left_son = father * 2; right_son = left_son + 1; pozz = left_son;

        if (sol[heap[left_son].node][heap[left_son].pr] > sol[heap[right_son].node][heap[right_son].pr])
            pozz++;

        if (sol[heap[pozz].node][heap[pozz].pr] < sol[heap[father].node][heap[father].pr]) {
            father = pozz;
        }
        else
            return;
    }
}

inline int Dijkstra(int start) {
    heap[1] = {1, 0};
    sol[1][0] = 0; nn = 1;

    while (nn) {
        z = heap[1].node;
        aux = heap[1].pr;

        for (auto x : G[z]) {
            nr_p = aux;

            if (prieten[x.nod] == 1)
                nr_p++;

            if (sol[x.nod][nr_p] > sol[z][aux] + x.c) {
                sol[x.nod][nr_p] = sol[z][aux] + x.c;

                if (viz[x.nod][nr_p] == 0) {
                    viz[x.nod][nr_p] = ++nn;
                    heap[nn] = {x.nod, nr_p};
                }
                Up(viz[x.nod][nr_p]);
            }
        }

        viz[z][aux] = 0; heap[1] = heap[nn--]; viz[heap[1].node][heap[1].pr] = 1; Down(1);
    }

    return sol[n][k];
}

int main () {
    Read();
    Init();
    fout << Dijkstra(1);

    fin.close(); fout.close(); return 0;
}