Cod sursa(job #2037796)

Utilizator CammieCamelia Lazar Cammie Data 12 octombrie 2017 19:55:42
Problema Ubuntzei Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>

#define MAXN 2002
#define inf 0x3f3f3f3f

using namespace std;

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

int n, k, nr_p, aux, z, prieten[MAXN], m, sol[MAXN][25], val;

struct str{
    int node, pr, c, cont;

    bool operator < (const str& other) const {
        return c > other.c;
    }
};

struct two{
    int nod, cost;
};

vector <two> G[MAXN];
priority_queue <str> Q;

inline void Read() {
    int x, y;

    fin >> n >> m;

    fin >> 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 int verify(int sursa, int contorr) {
    val = 1 << sursa;

    if ((contorr & val) == 0) {
        return 1;
    }
    return 0;
}

inline int Dijkstra(int start) {
    int contor, contt;
    sol[start][0] = 0;
    Q.push({start, 0, 0, 0});

    while (!Q.empty()) {
        z = Q.top().node;
        aux = Q.top().pr;
        contor = Q.top().cont;

        for (auto xx : G[z]) {
            if (prieten[xx.nod] == 1) {
                if (verify(xx.nod, contor)) {
                    contt = (contor | val);
                    nr_p = aux + 1;
                }
                else {
                    contt = contor;
                    nr_p = aux;
                }
            }
            else {
                nr_p = aux;
                contt = contor;
            }

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

                Q.push({xx.nod, nr_p, sol[xx.nod][nr_p], contt});
            }
        }
        Q.pop();
    }

    return sol[n][k];
}

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

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