Cod sursa(job #3200440)

Utilizator eugenioMarinescu Eugenio eugenio Data 4 februarie 2024 18:03:31
Problema Ubuntzei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.55 kb
#include <bits/stdc++.h>
#define nmax 2001
#define kmax 16
#define inf 200001
using namespace std;

class InParser
{
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch()
    {
        ++sp;
        if (sp == 4096)
        {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume)
    {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n)
    {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n)
    {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-')
        {
            n = 0;
            sgn = -1;
        }
        else
        {
            n = c - '0';
        }
        while (isdigit(c = read_ch()))
        {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};


vector<vector<pair<int,int>>> g(nmax);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
int n, m, k, x, y, c;
int ub[kmax], cost[nmax][nmax], dp[(1<<kmax)+1][kmax+1];

void bfs(int s)
{
    for(int i=1; i<=n; i++)
        cost[s][i]=inf;
    cost[s][s]=0;
    pq.push({0,s});
    while(!pq.empty())
    {
        int nod=pq.top().second;
        pq.pop();
        for(auto x : g[nod])
        {
            int nnod=x.first;
            int ccost=x.second;
            if(cost[s][nnod]>cost[s][nod]+ccost)
            {
                cost[s][nnod]=cost[s][nod]+ccost;
                pq.push({cost[s][nnod],nnod});
            }
        }
    }
}

int main()
{
    InParser cin("ubuntzei.in");
    OutParser cout("ubuntzei.out");
    cin>>n>>m>>k;
    for(int i=1; i<=k; i++)
        cin>>ub[i];

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

    ub[k+1]=1;
    ub[k+2]=n;

    for(int i=1; i<=k+2; i++)
        bfs(ub[i]);

    if(k==0)
    {
        cout<<cost[1][n];
        exit(0);
    }

    for(int i=1; i<=k; i++)
        for(int mask=1; mask<=((1<<k)-1); mask++)
            dp[mask][i]=inf;

    for(int i=1; i<=k; i++)
        dp[(1<<(i-1))][i]=cost[1][ub[i]];


    for(int mask=1; mask<=((1<<k)-1); mask++)
    {
        if(__builtin_popcount(mask)<2)
            continue;
        for(int j=0; j<k; j++)
        {
            if(mask&(1<<j))
            {
                for(int p=0; p<k; p++)
                {
                    if(mask&(1<<p) && p!=j)
                    {
                        dp[mask][j+1]=min(dp[mask][j+1], dp[mask-(1<<j)][p+1]+cost[ub[p+1]][ub[j+1]]);
                    }
                }
            }
        }
    }

    int sol=inf;
    for(int i=1; i<=k; i++)
        sol=min(sol,dp[(1<<k)-1][i] + cost[ub[i]][n]);
    cout<<sol;
}