Cod sursa(job #2720861)

Utilizator NashikAndrei Feodorov Nashik Data 11 martie 2021 12:50:18
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.78 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int kk[20],cost[2005],d[25][25],dp[25][100005];
priority_queue <pair<int,int> > pq;
vector <pair<int,int> > v[2005];
//ifstream cin("ubuntzei.in");
//ofstream cout("ubuntzei.out");
int main()
{
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=k;i++){
        cin>>kk[i];
        //f[kk[i]]=1;
    }

    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        v[a].push_back(make_pair(b,c));
        v[b].push_back(make_pair(a,c));
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<=n;j++){
            cost[j]=999999999;
        }
        pq.push(make_pair(0,kk[i]));
        cost[kk[i]]=0;
        while(pq.empty()==false){
            pair<int,int> a=pq.top();
            pq.pop();
            int co=-a.first;
            int nod=a.second;
            for(auto u:v[nod]){
                if(co+u.second<cost[u.first]){
                    cost[u.first]=co+u.second;
                    pq.push(make_pair(-cost[u.first],u.first));
                }
            }
        }
        for(int j=1;j<=k;j++){
            d[i][j]=cost[kk[j]];
        }
    }
    /// facem pentru 1
    for(int j=1;j<=n;j++){
        cost[j]=999999999;
    }
    pq.push(make_pair(0,1));
    cost[1]=0;
    while(pq.empty()==false){
        pair<int,int> a=pq.top();
        pq.pop();
        int co=-a.first;
        int nod=a.second;
        for(auto u:v[nod]){
            if(co+u.second<cost[u.first]){
                cost[u.first]=co+u.second;
                pq.push(make_pair(-cost[u.first],u.first));
            }
        }
    }
    for(int j=1;j<=k;j++){
        d[20][j]=cost[kk[j]];
    }
    ///facem pentru n
    for(int j=1;j<=n;j++){
        cost[j]=999999999;
    }
    pq.push(make_pair(0,n));
    cost[n]=0;
    while(pq.empty()==false){
        pair<int,int> a=pq.top();
        pq.pop();
        int co=-a.first;
        int nod=a.second;
        for(auto u:v[nod]){
            if(co+u.second<cost[u.first]){
                cost[u.first]=co+u.second;
                pq.push(make_pair(-cost[u.first],u.first));
            }
        }
    }
    for(int j=1;j<=k;j++){
        d[21][j]=cost[kk[j]];
    }
    /*
    for(int i=1;i<=k;i++){
        for(int j=1;j<=k;j++){
            cout<<d[i][j]<<" ";
        }
        cout<<"\n";
    }
    */
    //cout<<"\n\n\n\n";
    //for(int j=1;j<=k;j++){
        //cout<<d[20][j]<<" ";
    //}
    //cout<<"\n";
    //for(int j=1;j<=k;j++){
        //cout<<d[21][j]<<" ";
    //}
    //cout<<"\n";
    int numb=(1<<(k))-1;
    //cout<<numb<<"\n";

    for(int i=1;i<=k;i++){
        for(int j=0;j<=100000;j++){
            dp[i][j]=999999999;
        }
    }
    for(int i=1;i<=k;i++){
        dp[i][(1<<(i-1))]=d[20][i];
    }
    for(int masca=1;masca<=numb;masca++){
        for(int i=1;i<=k;i++){
            int bit=i-1,temp;
            if(masca & (1<<bit)){
                temp=(masca xor (1<<bit));
                int scos=i;
                int ok=0;
                for(int fin=1;fin<=k;fin++){
                    if((masca & (1<<(fin-1))) and scos!=fin){
                        dp[scos][masca]=min(dp[scos][masca],dp[fin][temp]+d[fin][scos]);
                    }
                }
            }
        }
    }
    //for(int i=1;i<=k;i++){
        //for(int masca=1;masca<=numb;masca++){
            //cout<<dp[i][masca]<<" ";
        //}
        //cout<<"\n";
    //}
    //cout<<"\n";
    int minim=999999999;
    for(int i=1;i<=k;i++){
        minim=min(minim,dp[i][numb]+d[21][i]);
    }
    if(k==0){
        cout<<cost[1];
        return 0;
    }
    cout<<minim;
    return 0;
}
/*
4 4
2 2 3
1 2 1
1 3 1
2 4 4
3 4 2
*/