Cod sursa(job #2715069)

Utilizator danhHarangus Dan danh Data 2 martie 2021 21:40:55
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;

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

const int NMAX = 2005;
const int inf = (1<<30) - 1;

int dist[17][17], drum[NMAX];
struct comp
{
    bool operator()(int a, int b)
    {
        return drum[a] > drum[b];
    }
};

vector<pair<int, int> > v[NMAX];
priority_queue<int, vector<int>, comp>pq;

int dp[1<<17][17];
int n, k;
vector<int> special;

bitset<NMAX> viz;

void dijkstra(int x)
{
    pq.push({x});
    drum[x] = 0;
    while(!pq.empty())
    {
        x = pq.top();
        pq.pop();
        if(viz[x] == 1)
            continue;
        else
            viz[x] = 1;
        for(auto el : v[x])
        {
            int new_node = el.first;
            int new_cost = drum[x] + el.second;
            if(new_cost < drum[new_node])
            {
                drum[new_node] = new_cost;
                pq.push({new_node});
            }
        }
    }
}

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

    fin>>n>>m>>k;
    special.push_back(1);
    for(i=1; i<=k; i++)
    {
        fin>>x;
        special.push_back(x);
    }
    special.push_back(n);
    k+=2;
    for(i=1; i<=m; i++)
    {
        fin>>x>>y>>c;
        v[x].push_back({y, c});
        v[y].push_back({x, c});
    }

    for(i=0; i<k; i++)
    {
        for(j=0; j<=n; j++)
        {
            drum[j] = inf;
        }
        viz.reset();
        dijkstra(special[i]);
        for(j=0; j<k; j++)
        {
            dist[i][j] = drum[special[j]];
        }

    }


    for(i=0; i<(1<<k); i++)
    {
        for(j=0; j<k; j++)
        {
            dp[i][j] = inf;
        }
    }

    int mask;

    dp[1][0] = 0;
    for(mask=0; mask<(1<<k); mask++)
    {
        for(i=1; i<k; i++)
        {
            if((1<<i) & mask)
            {
                for(j=0; j<k; j++)
                {
                    if(dist[i][j])
                    {
                        int c = dist[i][j];
                        dp[mask][i] = min(dp[mask][i], dp[mask - (1<<i)][j] + c);
                    }
                }
            }
        }
    }

    int rez = inf;

    fout<<dp[(1<<k)-1][k-1];

    return 0;
    fin.close();
    fout.close();

}