Cod sursa(job #1210871)

Utilizator ArchazeyBaltatu Andrei-Mircea Archazey Data 21 iulie 2014 14:56:52
Problema Ubuntzei Scor 75
Compilator cpp Status done
Runda boji_round9 Marime 2.63 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;

struct cell
{
    int nod,cost;
    bool const operator <(const cell& e) const
        {
            return cost>e.cost;
        }
};

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

const int conf=(1<<15);
const int oo=1<<30;
const int NMAX=2005;
const int KMAX=16;

int n,m,k,a[KMAX];
int dp[KMAX][NMAX];
vector<cell>v[NMAX];
priority_queue<cell>Q;
int mat[KMAX][conf][KMAX];
int sol;

inline void Citire()
{
    int i,x;
    cell ca;
    fin>>n>>m;
    fin>>k;
    for (i=1;i<=k;i++) fin>>a[i];
    for (i=1;i<=m;i++)
        {
            fin>>x>>ca.nod>>ca.cost;
            v[x].push_back(ca);
            swap(x,ca.nod);
            v[x].push_back(ca);
        }
}

inline void DIJKSTRA(int poz,int x)
{
    int i;
    cell ca,aux;
    for (i=1;i<=n;i++) dp[poz][i]=oo;
    dp[poz][x]=0;
    ca.nod=x;
    ca.cost=0;
    Q.push(ca);
    while (!Q.empty())
        {
            ca=Q.top();
            Q.pop();
            if (ca.cost==dp[poz][ca.nod])
                {
                    for (vector<cell>::iterator it=v[ca.nod].begin();it!=v[ca.nod].end();it++)
                        {
                            aux=*it;
                            if (dp[poz][aux.nod]>dp[poz][ca.nod]+aux.cost)
                                {
                                    dp[poz][aux.nod]=dp[poz][ca.nod]+aux.cost;
                                    aux.cost=dp[poz][aux.nod];
                                    Q.push(aux);
                                }
                        }
                }
        }
}

inline void Preprocesare()
{
    int i;
    DIJKSTRA(0,1);
    for (i=1;i<=k;i++) DIJKSTRA(i,a[i]);
}

inline void SOLVE()
{
    int i,j,l,aux;
    if (k)
    {for (i=1;i<=k;i++)
        for (j=1;j<=(1<<k)-1;j++)
            for (l=1;l<=k;l++)
                mat[i][j][l]=oo;
    for (i=1;i<=k;i++) mat[i][(1<<(i-1))][i]=dp[0][a[i]];
    for (i=1;i<=k;i++)
        for (j=1;j<=(1<<k)-1;j++)
            if (j&(1<<(i-1)))
                for (l=1;l<=k;l++)
                    if (l!=i && j&(1<<(l-1)))
                        for (aux=1;aux<=k;aux++)
                            if (aux!=l && j&(1<<(aux-1)))
                                mat[i][j][l]=min(mat[i][j][l],mat[i][j-(1<<(l-1))][aux] + dp[aux][a[l]]);
    sol=oo;
    for (i=1;i<=k;i++)
        for (j=1;j<=k;j++)
            sol=min(sol,mat[i][(1<<k)-1][j]+dp[j][n]);
    fout<<sol<<"\n";}
    else fout<<dp[0][n]<<"\n";
}

int main()
{
    Citire();
    Preprocesare();
    SOLVE();
    return 0;
}