Cod sursa(job #2859631)

Utilizator TiberiwTiberiu Amarie Tiberiw Data 1 martie 2022 17:50:38
Problema Ubuntzei Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <bits/stdc++.h>
#define Dmax 2005
#define inf 0x3F3F3F3F
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
int n,m,oras[20],Cost[20][20],C[262150][20],k;
vector<pair<int,int> >G[Dmax];
priority_queue<pair<int,int> >Q;

void Hamilton()
{
    int n2 = k+1;
    n2++;
    int lim = 1<<n2;
    for(int i = 1; i < lim; i++)
        for(int j = 0; j < n2; j++)
            C[i][j] = inf;
    C[1][0] = 0;
    for(int mask = 1; mask < lim; mask++)
        for(int j = 0; j < n2; j++)
            if((mask & (1<<j))&&(C[mask][j]!=inf))
                for(int l = 0; l < n2; l++)
                    if((!(mask & (1<<l)))&&Cost[j][l]!=inf)
                {
                     int newMask = mask|(1<<l);
                     C[newMask][l] = min(C[newMask][l],C[mask][j] + Cost[j][l]);
                }
    int sol = C[(1<<n2)-1][k+1];
    g<<sol;




}


void Dijkstra(int nod)
{
    int start = oras[nod];
    int D[Dmax];
    for(int i = 1; i <= n; i++)
        D[i] = inf;
    D[start] = 0;
    Q.push({0,start});
    while(!Q.empty())
    {
        int x = Q.top().first;
        int y = Q.top().second;
        Q.pop();
        x = -x;
        if(x > D[y])
            continue;
        for(vector<pair<int,int> >::iterator it = G[y].begin(); it < G[y].end(); it++)
        {
            int Vecin = it->first;
            int Cost = it->second;
            if(D[Vecin] > D[y] + Cost)
            {
                D[Vecin] = D[y] + Cost;
                Q.push({-D[Vecin],Vecin});
            }
        }
    }

    for(int i = 0; i <= k+1; i++)
    Cost[nod][i] = Cost[i][nod] = D[oras[i]];





}

int main()
{
    f>>n>>m>>k;
    for(int i = 0; i < k; i++)
        f>>oras[i];
    oras[k] = 1,oras[k+1] = n;
    for(int i = 0; i <= k+1; i++)
        for(int j = 0; j <= k+1; j++)
            Cost[i][j] = inf;
    for(int i = 1; i <= m; i++)
    {
        int x,y,z;
        f>>x>>y>>z;
        G[x].push_back({y,z});
        G[y].push_back({x,z});
    }

    for(int i = 0; i <= k+1; i++)
        Dijkstra(i);

 Hamilton();

    return 0;
}