Cod sursa(job #3270770)

Utilizator Edi17roAnghel Eduard Edi17ro Data 24 ianuarie 2025 13:21:47
Problema Ubuntzei Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
const int NMAX = 2000;
int n, m, k;
int locviz;
uint64_t rez;
bitset<20> localitate;
bitset<20> viz;

struct date
{
    int nod;
    int cost;
};
vector<date> g[NMAX + 5];

struct point
{
    int nod;
    int cost;

    bool operator < (const point& a) const
    {
        return cost > a.cost;
    }
};
priority_queue<point> q;

void init()
{
    while(!q.empty())
    {
        q.pop();
    }
}

void dij(int& x)
{
    while(!q.empty())
    {
        int nod = q.top().nod;
        int cost = q.top().cost;
        q.pop();

        if(locviz == k && nod == n)
        {
            rez += cost;
            return;
        }

        if(localitate[nod] == 1 && viz[nod] == 0)
        {
            rez += cost;
            x = nod;
            viz[nod] = 1;
            return;
        }

        for(auto i : g[nod])
        {
            q.push({i.nod, cost + i.cost});
        }
    }
}

void solve()
{
    int loc = 1;

    while(locviz <= k)
    {
        init();
        q.push({loc, 0});
        dij(loc);
        ++locviz;
    }
}

int main()
{
    in >> n >> m;
    in >> k;

    for(int i = 1; i <= k; ++i)
    {
        int x;
        in >> x;
        localitate[x] = 1;
    }

    for(int i = 1; i <= m; ++i)
    {
        int u, v, c;
        in >> u >> v >> c;

        g[u].push_back({v, c});
        g[v].push_back({u, c});
    }

    solve();

    out << rez;
    return 0;
}