Cod sursa(job #2230270)

Utilizator AndreiTurcasTurcas Andrei AndreiTurcas Data 9 august 2018 16:30:00
Problema Ubuntzei Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define lim 2000
#define oo 0x3f3f3f3f
#define mp make_pair
using namespace std;

ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");

int n, m, Ref = 0, Dist[lim+1][1<<16], sv = 0;
vector<pair<int,int> > G[lim+1];
queue<pair<int,int> > Q;
bool pas[lim+1];

void Read()
{
    f >> n >> m;
    int k; f >> k;
    for(int i = 1; i <= k; ++i)
    {
        int t;
        f >> t;
        pas[t] = 1;
        Ref |= 1 << t;
    }
    for(int i = 1; i <= m; ++i)
    {
        int x, y, c;
        f >> x >> y >> c;
        G[x].push_back(mp(y, c));
        G[y].push_back(mp(x, c));
    }
}

void BelFord()
{
    Q.push(mp(1, 0));
    while(!Q.empty())
    {
        int v = Q.front().first, s = Q.front().second;
        Q.pop();
        for(vector<pair<int,int> > :: iterator it = G[v].begin(); it != G[v].end(); ++it)
        {
            int v2 = it->first, d = it->second, nexts = s;
            if(pas[v2] && ((s & (1 << nexts)) == 0)) nexts |= 1 << v2;
            if(Dist[v2][nexts] == 0 || Dist[v2][nexts] > Dist[v][s] + d)
            {
                Dist[v2][nexts] = Dist[v][s] + d;
                Q.push(mp(v2, nexts));
                sv = max (sv, nexts);
            }
        }
    }
    g << Dist[n][sv];
}

int main()
{
    Read();
    BelFord();
}