Cod sursa(job #2037299)

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

#define inf 0x3f3f3f3f
#define MAXN 2002

using namespace std;

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

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

struct two{
    int nod, c;
};

struct str{
    int node, pr, cost;

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

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

inline void Read() {
    int y, z;

    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 int Dijkstra(int start) {
    sol[start][0] = 0;
    Q.push({start, 0, 0});

    while (!Q.empty()) {
        z = Q.top().node;
        aux = Q.top().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;
                Q.push({x.nod, nr_p, sol[x.nod][nr_p]});
            }
        }
        Q.pop();
    }

    return sol[n][k];
}

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

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