Cod sursa(job #1513958)

Utilizator lacraruraduRadu Matei Lacraru lacraruradu Data 30 octombrie 2015 12:38:44
Problema Ubuntzei Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.47 kb
#include <fstream>

using namespace std;

#define N 2005
#define M 10005
#define K 20
#define INF (1 << 31) - 1

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

int n, m, k;
int lst[N], vf[2 * M], urm[2 * M], cost[2 * M], nr;
int c[K], d[N], pozc[N];

int h[N], poz[N], nh;

int lst2[K], vf2[500], urm2[500], cost2[500], nr2;
int a[1 << K][K];

void schimba(int i1, int i2)
{
    int aux = h[i1];
    h[i1] = h[i2];
    h[i2] = aux;
    poz[h[i1]] = i1;
    poz[h[i2]] = i2;
}

void urca(int i)
{
    while(i >= 2 && d[h[i]] < d[h[i / 2]])
    {
        schimba(i, i / 2);
        i /= 2;
    }
}

void coboara(int i)
{
    int bun = i, fs = 2 * i, fd = 2 * i + 1;
    if(fs <= nh && d[h[fs]] < d[h[bun]])
        bun = fs;
    if(fd <= nh && d[h[fd]] < d[h[bun]])
        bun = fd;
    if(bun != i)
    {
        schimba(i, bun);
        coboara(bun);
    }
}

void adauga(int x)
{
    h[++nh] = x;
    poz[x] = nh;
    urca(nh);
}

void sterge(int i)
{
    poz[h[i]] = 0;
    schimba(i, nh);
    nh--;
    urca(i);
    coboara(i);
}

void dijkstra(int x)
{
    for(int i = 1; i <= n; i++)
    {
        d[i] = INF;
        poz[i] = 0;
    }
    for(int i = 1; i <= nh; i++)
        h[i] = 0;
    nh = 0;

    d[x] = 0;
    adauga(x);
    //out << x << '\n';
    while(nh)
    {
        x = h[1];
        sterge(1);

        for(int i = lst[x]; i; i = urm[i])
        {
            int y = vf[i];
            int c = cost[i];
            if(d[x] + c < d[y])
            {
                d[y] = d[x] + c;
                if(poz[y] == 0)
                    adauga(y);
                else
                    urca(poz[y]);
            }
        }
    }

    /*for(int i = 1; i <= n; i++)
        out << d[i] << '\t';
    out << '\n';*/
}

int minim(int x, int y)
{
    return (x < y) ? x : y;
}

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

    c[0] = 1;
    c[1] = n;
    pozc[1] = 0;
    pozc[n] = 1;
    k += 2;
    for(int i = 2; i < k; i++)
    {
        in >> c[i];
        pozc[c[i]] = i;
    }

    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        in >> x >> y >> c;
        vf[++nr] = y;
        cost[nr] = c;
        urm[nr] = lst[x];
        lst[x] = nr;
        vf[++nr] = x;
        cost[nr] = c;
        urm[nr] = lst[y];
        lst[y] = nr;
    }

    for(int i = 0; i < k; i++)
    {
        dijkstra(c[i]);
        for(int j = 0; j < k; j++)
            if(c[i] != c[j])
            {
                vf2[++nr2] = c[i];
                cost2[nr2] = d[c[j]];
                urm2[nr2] = lst2[c[j]];
                lst2[c[j]] = nr2;
            }
    }

    /*for(int i = 0; i < k; i++)
    {
        out << c[i] << '\n';
        for(int j = lst2[c[i]]; j; j = urm2[j])
            out << vf2[j] << '\t' << cost2[j] << '\n';
        out << '\n';
    }*/

    for(int i = 1; i <= (1 << k) - 1; i += 2)
        for(int j = 0; j < k; j++)
            a[i][j] = INF;
    a[1][0] = 0;

    for(int i = 3; i <= (1 << k) - 1; i += 2)
        for(int j = 1; j < k; j++)
        {
            a[i][j] = INF;
            if(!(i & (1 << j)))
                continue;
            for(int l = lst2[c[j]]; l; l = urm2[l])
                if(i & (1 << pozc[vf2[l]]) && a[i - (1 << j)][pozc[vf2[l]]] != INF)
                    a[i][j] = minim(a[i][j], a[i - (1 << j)][pozc[vf2[l]]] + cost2[l]);
        }

    out << a[(1 << k) - 1][1];
    return 0;
}