Cod sursa(job #2576435)

Utilizator andreighinea1Ghinea Andrei-Robert andreighinea1 Data 6 martie 2020 19:26:08
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define kmax 17
#define nmax 2001
#define inf 0x3f3f3f3f

using namespace std;

ifstream f("ubuntzei.in");
ofstream o("ubuntzei.out");

int n,m,k,x,y,z;
int d[nmax][nmax],dp[1<<kmax][kmax],c[kmax];
bool inqueue[nmax];
vector<pair<int,int> > g[nmax];

struct cmp{
    inline bool operator () (int a,int b){
        return d[a]>d[b];
    }
};

priority_queue<int,vector<int>,cmp> q;

void read(){
    int i;
    f >> n >> m >> k;
    k+=2;
    c[0]=1;
    c[k-1]=n;
    for(i=1;i<k-1;++i)
        f >> c[i];
    sort(c,c+k);

    for(i=1;i<=m;++i){
        f >> x >> y >> z;
        g[x].push_back({y,z});
        g[y].push_back({x,z});
    }
}

void dijkstra(int start){
    int i,x,l,v,c;
    for(i=1;i<=n;++i)
        d[start][i]=inf,
        inqueue[i]=false;
    d[start][start]=0;
    q.push(start);

    while(!q.empty()){
        x=q.top();
        q.pop();
        inqueue[x]=false;

        l=g[x].size();
        for(i=0;i<l;++i){
            v=g[x][i].first;
            c=g[x][i].second+d[start][x];
            if(c<d[start][v]){
                d[start][v]=c;
                if(!inqueue[v]){
                    q.push(v);
                    inqueue[v]=true;
                }
            }
        }
    }
}

void solve(){
    int conf,iconf,i,j;
    for(i=1;i<=k;++i)
        dijkstra(c[i]);

    for(conf=0;conf<(1<<k);++conf)
        for(i=0;i<k;++i)
            dp[conf][i]=inf;
    dp[1][0]=0;

    for(conf=1;conf<(1<<k);++conf){
        for(i=0;i<k;++i){
            if((1<<i)&conf){ // daca i face parte din conf
                for(j=0;j<k;++j){
                    if(((1<<j)&conf) && i!=j){
                        dp[conf][i]=min(dp[conf][i], dp[conf-(1<<i)][j]+d[c[j]][c[i]]);
                    }
                }
            }
        }
    }
    o << dp[(1<<k)-1][k-1] << '\n';
}

int main()
{
    read();
    solve();
    return 0;
}