Cod sursa(job #3200209)

Utilizator eugenioMarinescu Eugenio eugenio Data 3 februarie 2024 20:19:13
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <bits/stdc++.h>
#define inf 300001
#define nmax 2005
using namespace std;

struct heap
{
    int ind;
    int cost;
    bool operator < (const heap &other) const
    {
        return (cost>other.cost);
    }
};

priority_queue<heap> pq;
vector<vector<heap>> g(nmax);
set<int> c;
long long n, m, x, y, z, k, d[nmax], total, mini, nxt;

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

int main()
{
    ifstream cin("ubuntzei.in");
    ofstream cout("ubuntzei.out");
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m;
    cin>>k;
    for(int i=1; i<=k; i++)
    {
        cin>>x;
        c.insert(x);
    }

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

    bfs(1);
    while(true)
    {
        mini=inf, nxt=0;
        for(auto x : c)
        {
            if(d[x]<mini)
            {
                mini=d[x];
                nxt=x;
            }
        }
        if(mini==inf)
        {
            total+=d[n];
            cout<<total;
            exit(0);
        }
        c.erase(nxt);
        total+=d[nxt];
        bfs(nxt);
    }
}