Cod sursa(job #2143694)

Utilizator felixiPuscasu Felix felixi Data 26 februarie 2018 10:45:10
Problema Ubuntzei Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 2000;
const int KMAX = 15;
const int INF  = (1 << 30);

int d[NMAX+5][(1 << KMAX)];
bool viz[NMAX+2][(1 << KMAX)];
int spec[NMAX+5];
struct stare
{
    int nod,con,cost;
    bool operator < (const stare& o) const {
        return cost > o.cost;
    }
};
vector<pair<int,int>> G[NMAX+2];

void dijkstra()
{
    priority_queue<stare> Q;
    Q.push({1, 0, 0});
    while( !Q.empty() ) {
        int nod = Q.top().nod;
        int cf = Q.top().con;
        int cost = Q.top().cost;
        Q.pop();
        if( viz[nod][cf] ) continue;

        viz[nod][cf] = 1;
        for( auto& pp : G[nod] ) {
            int x = pp.first;
            int ncost = cost + pp.second;
            int mk = cf | spec[x];
            if( !viz[x][mk] && ncost < d[x][mk] ) {
                d[x][mk] = ncost;
                Q.push({x, mk, ncost});
            }
        }
    }
}

int main()
{
    int N, M, K;
    in >> N >> M >> K;
    for( int i = 0;  i < K;  ++i ) {
        int x;
        in >> x;
        spec[x] = (1 << i);
    }
    for( int i = 1;  i <= M;  ++i ) {
        int x,y,c;
        in >> x >> y >> c;
        G[x].push_back({y,c});
        G[y].push_back({x,c});
    }
    for( int i = 1;  i <= N;  ++i ) for( int j = 0;  j < (1 << K);  ++j )
        d[i][j] = INF;
    dijkstra();
    out << d[N][(1 << K) - 1] << '\n';
    return 0;
}