Cod sursa(job #2550602)

Utilizator baltoi.teodorTeodor Baltoi baltoi.teodor Data 18 februarie 2020 21:42:37
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
ifstream fin (file+".in");
ofstream fout (file+".out");
const string file = "ubuntzei";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 2005, kmax = 17;

int n, m, c[nmax][nmax], d[nmax][nmax], pd[(1<<kmax)+5][kmax], g[nmax][nmax];
vector<int> v[nmax], k;

int main()
{

    fin >> n >> m;
    int nr;
    fin >> nr;
    for (int i = 1; i <= nr; ++i){
        int x;
        fin >> x;
        k.push_back(x);
    }
    while(m--){
        int x, y, z;
        fin >> x >> y >> z;
        v[y].push_back(x);
        v[x].push_back(y);
        c[x][y] = c[y][x] = z;
    }
    k.push_back(1);
    k.push_back(n);
    sort(k.begin(), k.end());
    for (int i = 0; i < nmax; ++i)
        for (int j = 0; j < nmax; ++j)
            d[i][j] = inf;
    for (auto start : k){
        d[start][start] = 0;
        priority_queue< pi, vector<pi>, greater<pi> > q;
        q.push({0, start});
        while(!q.empty()){
            int nod = q.top().ss;
            if(d[start][nod] != q.top().ff){
                q.pop();
                continue;
            }
            q.pop();
            for (auto x : v[nod])
                if(d[start][x] > d[start][nod]+c[nod][x]){
                    d[start][x] = d[start][nod]+c[nod][x];
                    q.push({d[start][x], x});
                }
        }
    }
    for (int i = 0; i < k.size(); ++i)
        for (int j = 0; j < k.size(); ++j)
            g[i][j] = d[k[i]][k[j]];
    for (int i = 0; i < (1<<kmax)+5; ++i)
        for (int j = 0; j < kmax; ++j)
            pd[i][j] = inf;
    pd[1][0] = 0;
    for (int conf = 1; conf < (1<<k.size()); ++conf)
        for (int last = 0; last < k.size(); ++last)
            if(pd[conf][last] != inf)
                for (int j = 0; j < k.size(); ++j)
                    if((conf&(1<<j)) == 0 && g[last][j] != inf && pd[conf^(1<<j)][j] > pd[conf][last]+g[last][j])
                        pd[conf^(1<<j)][j] = pd[conf][last]+g[last][j];
    fout << pd[(1<<k.size())-1][k.size()-1] << "\n";
    return 0;
}