Cod sursa(job #2566714)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 3 martie 2020 00:00:05
Problema Ubuntzei Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const int MAXN = 2005, MAXK = 16, INF = 0x3f3f3f3f, MAX = 1 << MAXK;

vector<pair<int, int>> graph[MAXN];
unordered_map<int, int> dp[MAXN];
bitset<MAX> inQ[MAXN];
int pos[MAXN], n, m, k;
queue<pair<int, int>> q;

void init()
{
    for (int i = 1; i <= n; ++i)
        pos[i] = -1;
}

void read()
{
    fin >> n >> m >> k;
    init();
    for (int i = 0; i < k; ++i) {
        int x;
        fin >> x;
        pos[x] = i;
    }
    for (int i = 0; i < m; ++i) {
        int x, y, c;
        fin >> x >> y >> c;
        graph[x].pb({y, c});
        graph[y].pb({x, c});
    }
}

void solve()
{
    q.push({1, 0});
    inQ[1][0] = true;
    while (!q.empty()) {
        int node = q.front().first, mask = q.front().second;
        q.pop();
        inQ[node][mask] = false;
        for (const auto& it: graph[node]) {
            int newM = mask;
            if (pos[it.first] != -1)
                newM |= (1 << pos[it.first]);
            if (dp[it.first][newM] == 0 && (it.first != 1 || newM != 0))
                dp[it.first][newM] = INF;
            if (dp[it.first][newM] > dp[node][mask] + it.second) {
                dp[it.first][newM] = dp[node][mask] + it.second;
                if (inQ[it.first][newM] == false) {
                    inQ[it.first][newM] = true;
                    q.push({it.first, newM});
                }
            }
        }
    }
}

int main()
{
    read();
    solve();
    fout << dp[n][(1 << k) - 1];
    return 0;
}