Cod sursa(job #2797262)

Utilizator AlexNicuNicu Alexandru AlexNicu Data 9 noiembrie 2021 17:37:48
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

#define N_MAX 2000
#define MAX_MASK ( 1 << 15 )
#define K_MAX 15
#define INF ( 1 << 30 )

struct node
{
    int nod, cost, mask;
    bool operator<(const node &a) const
    {
        return cost > a.cost;
    }
};

vector<pair<int, int>> G[N_MAX + 1];
priority_queue<node> dijkstra;
int dp[K_MAX][MAX_MASK], ubuntzei[K_MAX], k;

int cauta_ubuntzel(int val) {
    int poz;
    poz = 0;
    while ( poz < k && ubuntzei[poz] != val )
        poz++;
    return poz;
}

void DIJKSTRA() {
    int mask;
    while ( !dijkstra.empty() ) {
        node elem = dijkstra.top();
        dijkstra.pop();
        if ( dp[elem.nod][elem.mask] == INF || dp[elem.nod][elem.mask] == elem.cost ) {
            for (pair<int, int> copil: G[elem.nod]) {
                mask = elem.mask;
                if (cauta_ubuntzel(copil.first) < k) {
                    mask = mask | (1 << cauta_ubuntzel(copil.first));
                }
                if (dp[copil.first][mask] > elem.cost + copil.second) {
                    dp[copil.first][mask] = elem.cost + copil.second;
                    dijkstra.push({dp[copil.first][mask], copil.first, mask});
                }
            }
        }
    }
}

int main() {
    int n, m, x, y, cost, i, j, minn, p;
    cin >> n >> m >> k;
    for ( i = 0; i < k; i++ )
        cin >> ubuntzei[i];
    for ( i = 0; i < m; i++ ) {
        cin >> x >> y >> cost;
        G[x].emplace_back(y, cost);
        G[y].emplace_back(x, cost);
    }
    minn = INF;
    for ( i = 0; i < k; i++ ) {
        for ( p = 1; p <= n; p++ ) {
            for (j = 0; j < (1 << k); j++) {
                dp[p][j] = INF;
            }
        }
        dp[1][0] = 1;
        dijkstra.push({1, 1, 0});
        DIJKSTRA();
        minn = min ( minn, dp[n][(1 << k) - 1] -1 );
    }
    cout << minn;
    return 0;
}