Cod sursa(job #2713736)

Utilizator Arsene_DenisaArsene Denisa Arsene_Denisa Data 28 februarie 2021 15:35:10
Problema Ubuntzei Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#include<queue>

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

const int inf=2e9;

int dp[131072][18];
int dist[2001][2001];
int l[18];

vector<pair<int, int> > v[18];

int main() {
    int n, m, i, j, x, y, c, k;

    fin>>n>>m;
    fin>>k;
    l[0]=1;
    l[k+1]=n;
    for(i=1;i<=k;i++) {
        fin>>l[i];
    }
    for(i=1; i<=m; i++) {
        fin>>x>>y>>c;
        v[y].push_back({x, c});
        v[x].push_back({y, c});
    }
    for(i=0;i<=k+2;i++) {
        for(j=0;j<=n+1;j++) {
                dist[i][j]=inf;
        }
    }
    for(i=0;i<=1<<(k+2)+5;i++) {
        for(j=0;j<k+2;j++) {
            dp[i][j]=inf;
        }
    }
    priority_queue<pair<int, int> >h;
    for(i=0;i<=k+1;i++) {
        dist[i][l[i]]=0;
        h.push({l[i], 0});
        while(!h.empty()) {
            int nod=h.top().first;
               h.pop();
            for(int j=0;j<v[nod].size();j++) {
                int newn=v[nod][j].first;
                int cost=v[nod][j].second;
                if(cost+dist[i][nod]<dist[i][newn]) {
                    dist[i][newn]=cost+dist[i][nod];
                    h.push({newn, -dist[i][newn]});
                }
            }
        }
        for(int po=1;po<=n;po++) {
            cout<<dist[i][po]<<" ";
        }
    }

    dp[1][0] = 0;
    int mask;
    for(mask=0; mask<(1<<k+2); mask++) {
        for(i=0; i<=k+1; i++) {
            if(mask&(1<<i)==0) continue;
                for(int t=0;t<=k+1;t++) {
                    if(mask&(1<<t)==0) continue;

                    dp[mask|(1<<t)][t]=min(dp[mask|(1<<t)][t], dp[mask][i]+dist[i][l[t]]);
                }
            }
    }
    fout<<dp[(1<<(k+2))-1][k+1];


    return 0;
}