Cod sursa(job #1789832)

Utilizator ajeccAjechiloae Eugen ajecc Data 27 octombrie 2016 15:50:00
Problema Ubuntzei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.46 kb
#include <bits/stdc++.h>
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A, x) memset(A, x, sizeof(A))
#define cpy(A, B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
const long long INFLL = (1LL<<62);
const int INFINT = 2000000000;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
/*template <typename T>
string to_string(const T& n){
    ostringstream os;
    os << n;
    return os.str();
}
*/
/*void invers_modular(int a, int b, int &d, int &x, int &y)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
        return ;
    }
    int x0, y0;
    invers_modular(b, a%b, d, x0, y0);
    x=y0;
    y=x0-a/b*y0;
}*/ // daca x<0 se aduna cu mod pana e mai mare, x fiind rezultatul

/*ull putere(ull baza, ull exponent, ull MOD)
{
    if(exponent == 0) return 1;
    if(exponent % 2 == 0) return putere((baza * baza) % MOD, exponent / 2, MOD) % MOD;
    return ((baza % MOD) * (putere(baza, exponent - 1, MOD) % MOD) % MOD);
}*/
ifstream fin("ubuntzei.in"); /// modifica cu numele corespunzator
ofstream fout("ubuntzei.out"); /// modifica cu numele corespunzator

const int NMAX = 2001;

int n, m, k, v[18], combinare[18], cost[NMAX], dp[(1 << 15) + 1][16];
VP graf[NMAX];
bool c[NMAX];
map<pair<int, int>, int> costuri;
V retinem_combinarile;

void bf(int start)
{
    for1(i, n)
        cost[i] = INFINT;
    cost[start] = 0;
    queue<int> coada;
    coada.push(start);

    while(!coada.empty())
    {
        int nod = coada.front();
        coada.pop();
        for(auto i: graf[nod])
            if(cost[i.first] > cost[nod] + i.second)
            {
                cost[i.first] = cost[nod] + i.second;
                coada.push(i.first);
            }
    }
    for1(i, k)
        costuri[{start, v[i]}] = costuri[{v[i], start}] = cost[v[i]];
    costuri[{start, n}] = costuri[{n, start}] = cost[n];
}

void comb(int p, int stop)
{
    if(p == stop)
    {
        int index = 0;
        for1(i, p)
            index = index | (1 << (combinare[i] - 1));

        for1(i, p)
        {
            dp[index][combinare[i]] = INFINT;
            for1(j, p)
                if(j != i)
                    dp[index][combinare[i]] = min(dp[index][combinare[i]], dp[index ^ (1 << (combinare[i] - 1))][combinare[j]] + costuri[{v[combinare[i]], v[combinare[j]]}]);
        }
    }
    else
        for(int i = combinare[p] + 1; i <= k; i++)
        {
            combinare[p + 1] = i;
            comb(p + 1, stop);
        }
}

int main()
{
    fin >> n >> m;
    fin >> k;
    for1(i, k)
        fin >> v[i];

    for1(nrm, m)
    {
        int a, b, c;
        fin >> a >> b >> c;
        graf[a].pb({b, c});
        graf[b].pb({a, c});
    }
    bf(1);
    for1(i, k)
        bf(v[i]);
    for1(i, k)
        dp[1 << (i - 1)][i] = costuri[{v[i], n}];

    if(k == 0)
        return fout << costuri[{1, n}], 0;
    if(k == 1)
        return fout << costuri[{1, v[1]}] + costuri[{v[1], n}], 0;
    for(int i = 2; i <= k; i++)
        comb(0, i);

    int sol = INFINT;
    for1(i, k)
        sol = min(sol, dp[(1 << k) - 1][i] + costuri[{1, v[i]}]);

    fout << sol;
    return 0;
}