Cod sursa(job #3255884)

Utilizator tudor_costinCostin Tudor tudor_costin Data 12 noiembrie 2024 17:03:11
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const int Nmax=2e3+5,inf=2e9;
int c[25];
bitset<Nmax> viz;
bool isk[Nmax];
int val[Nmax];
int dp[(1<<20)][20];
vector<pair<int,int>> v[Nmax];
int g[20][20];
int main()
{
    int n,m,k;
    fin>>n>>m;
    fin>>k;
    c[0]=1;
    c[k+1]=n;
    val[1]=0;
    val[n]=k+1;
    isk[1]=1;
    isk[n]=1;
    for(int i=1;i<=k;i++)
    {
        fin>>c[i];
        val[c[i]]=i;
        isk[c[i]]=1;
    }
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        fin>>x>>y>>c;
        v[x].push_back({y,c});
        v[y].push_back({x,c});
    }
    for(int i=0;i<=k+1;i++)
    {
        priority_queue<pair<int,int>> pq;
        pq.push({0,c[i]});
        viz.reset();
        while(!pq.empty())
        {
            pair<int,int> cur=pq.top();
            pq.pop();
            if(viz[cur.second]) continue;
            viz[cur.second]=1;
            if(isk[cur.second])
            {
                g[i][val[cur.second]]=-cur.first;
            }
            for(pair<int,int> u:v[cur.second])
            {
                if(!viz[u.first])
                {
                    pq.push({cur.first-u.second,u.first});
                }
            }
        }
    }
    k++;
    for(int mask=0;mask<(1<<k);mask++)
    {
        for(int j=0;j<k;j++)
        {
            dp[mask][j+1]=inf;
            if((mask&(1<<j))!=0)
            {
                int prev=mask-(1<<j);
                if(prev==0)
                {
                    dp[mask][j+1]=g[0][j+1];
                }
                else
                {
                    for(int b=0;b<k;b++)
                    {
                        dp[mask][j+1]=min(dp[mask][j+1],dp[prev][b+1]+g[b+1][j+1]);
                    }
                }
            }
            ///cout<<dp[mask][j+1]<<' '<<mask<<' '<<j+1<<'\n';
        }
    }
    fout<<dp[(1<<k)-1][k]<<'\n';
    return 0;
}