Cod sursa(job #2565076)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 2 martie 2020 12:00:03
Problema Ubuntzei Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include<fstream>
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<climits>

using namespace std;

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

const int NMax = 2000, KMax = 17, oo = 1e9;
const long long OO = 1e15;

vector <int> G[NMax+5], V;
priority_queue <pair<int,int>> Q;

int N, M, K, D[NMax+5], Use[NMax+5], C[NMax+5][NMax+5];
long long DP[1 << 19][KMax+5];

void Read() {
    in >> N >> M >> K;
    for (int i = 1, x; i <= K; i++)
        in >> x, V.push_back(x);

    V.push_back(1);
    V.push_back(N);
    K += 2;

    for (int i = 0,x,y,c; i < M; i++) {
        in >> x >> y >> c;
        C[x][y] = C[y][x] = c;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

void Dijkstra (int Node) {
    for (int i = 1; i <= N; i++)
        D[i] = oo;

    memset(Use,0,sizeof Use);
    D[Node] = 0;

    Q.push(make_pair(D[Node], Node));

    while(!Q.empty()) {
        int Nod = Q.top().second; Q.pop();

        if(Use[Nod]) continue;
        Use[Nod] = 1;

        for (auto Vecin : G[Nod]) {
            int Cost = C[Nod][Vecin];

            if(D[Vecin] > D[Nod]+Cost) {
                D[Vecin] = D[Nod] + Cost;
                Q.push(make_pair(-D[Vecin], Vecin));
            }
        }
    }
    for(auto x : V)
        C[Node][x] = D[x];
}

void Solve() {
    for(auto x : V)
        Dijkstra(x);

    for(int i = 0; i < (1 << K); i++)
        for (int j = 0; j < K; j++)
            DP[i][j] = OO;

    DP[1][0] = 0;

    for (int Conf = 1; Conf < (1 << K); Conf++)
        for (int i = 0; i < K; i++) {
            if (Conf & (1 << i))
                for (int j = 0; j < K; j++) {
                    DP[Conf][i] = min(DP[Conf][i], DP[Conf - (1 << i)][j] + C[V[i]][V[j]]);
                }
        }

    out << DP[(1 << K) - 1][K - 1] << '\n';
}

int main() {
    Read();
    Solve();
}