Cod sursa(job #3197804)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 27 ianuarie 2024 14:54:16
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 6.13 kb
#include <bits/stdc++.h>

#define file_name "ubuntzei"

#define file_name_input file_name".in"

#define file_name_output file_name".out"

#define fast_read 1

//#undef fast_read

#define local_user 1

#undef local_user


class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            assert(fread(buff, 1, 4096, fin));
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

};


class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

    #if local_user

        #if fast_read

            InParser fin("file.in");
            OutParser fout("file.out");

        #else

            std :: ifstream fin("file.in");
            std :: ofstream fout("file.out");

        #endif // fast_read

    #else

        #if fast_read

            InParser fin(file_name_input);
            OutParser fout(file_name_output);

        #else

            std :: ifstream fin(file_name_input);
            std :: ofstream fout(file_name_output);

        #endif // fast_read

    #endif // file_is_good

void __attribute__((constructor)) read();

void read(){

    #if fast_read

        std :: ios_base::sync_with_stdio(false);
        std :: cin.tie(nullptr);
        std :: cout.tie(nullptr);

    #else

        std :: ios :: sync_with_stdio(false);
        std :: ios_base::sync_with_stdio(false);
        std :: cin.tie(nullptr);
        std :: cout.tie(nullptr);
        fin.tie(nullptr);
        fout.tie(nullptr);

    #endif // fast_read

}

#include <bits/stdc++.h>
#define DIM 2001

using namespace std;

struct nod{

    int node, value;


    bool operator < (nod x) const{

        return (value > x.value);

    }

};

nod temp;

vector < pair <int, int> > G[5 * DIM];

int dp[(1LL << 16) + 2][20];

int cost[DIM][DIM];

int event[DIM];

int n, m, x, y, z, k, i, mask, q, j, answer = 1e9;

void Dijkstra(int source){

    priority_queue <nod> Q;

    temp.node = source;

    temp.value = 0;

    Q.push(temp);

    for(int i=1;i<=n;i++)

        cost[source][i] = 1e9;

    cost[source][source] = 0;

    while(!Q.empty()){

        int node = Q.top().node;

        int value = Q.top().value;

        Q.pop();

        if(cost[source][node] != value)

            continue;

        vector <  pair <int, int> > :: iterator j;

        for(j = G[node].begin() ; j != G[node].end() ; j++){

            if(cost[source][j -> first] > cost[source][node] + j -> second){

                cost[source][j -> first] = cost[source][node] + j -> second;

                temp.node = j -> first;

                temp.value = cost[source][j -> first];

                Q.push(temp);

            }

        }

    }

}

int main(){

    fin >> n >> m >> k;

    for(i=1;i<=k;i++)

        fin >> event[i];

    event[k + 2] = n;

    event[k + 1] = 1;

    while(m--){

        fin >> x >> y >> z;

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

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

    }

    for(i=1;i<=k+2;i++)

        Dijkstra(event[i]);

    for(i=1;i<=k;i++)

        for(mask = 1 ; mask <= ((1LL << (k)) - 1) ; mask++)

            dp[mask][i] = 1e9;

    for(i=1;i<=k;i++)

        dp[(1LL << (i - 1))][i] = cost[1][event[i]];


    for(mask = 1 ; mask <= ((1LL << k) - 1) ; mask++){

        if(__builtin_popcount(mask) == 1)

            continue;

        for(j=0;j<k;j++){

            if(((mask >> j) & 1)){

                for(q=0;q<k;q++){

                    if((((mask >> q) & 1)) && q != j)

                        dp[mask][j + 1] = min(dp[mask][j + 1], dp[mask - (1LL << j)][q + 1] + cost[event[q + 1]][event[j + 1]]);

                }

            }

        }

    }

    for(i=1;i<=k;i++)

        answer = min(answer, dp[((1LL << k) - 1)][i] + cost[event[i]][n]);

    if(!k)

        fout << cost[1][n];

    else

        fout << answer;

    return 0;

}