Cod sursa(job #2447773)

Utilizator capmareAlexCapmare Alex capmareAlex Data 14 august 2019 15:44:38
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2 kb
#include <bits/stdc++.h>
#define inf 2147483647
#define ff first
#define ss second
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n, m, loc[2005], k, cost[2005][2005], costm[2005][2005], d[2005], g[2005][2005];
vector< pair<int, int> > v[2005];
int pd[(1<<17)+5][17];
void dijkstra(int k)
{
    priority_queue < pair< int, int > > q;
    for(int i = 1; i < 2005; ++i) d[i] = inf;
    d[k] = 0;
    q.push({0,k});
     while(!q.empty())
    {
        int cost=-q.top().ff;
        int nod=q.top().ss;
        q.pop();
        if(cost!=d[nod])
            continue;
        for(auto x: v[nod])
            if(d[x.ff]>d[nod]+x.ss)
        {
            d[x.ff]=d[nod]+x.ss;
            q.push({-d[x.ff],x.ff});
        }
    }
    for(int i = 1; i <= n; ++i)
        if(i !=  k&& d[i]!=inf)
    {
        costm[i][k] = costm[k][i] = d[i];
    }
}
int main()
{
    fin >> n >> m;
    fin >> k;
    for(int i = 0; i <k; ++i)
    {
        fin >> loc[i];
    }
    loc[k++]=1;
    loc[k++]=n;
    for( int  i = 1; i <= m; ++i)
    {
        int x, y, z;
        fin >> x >> y >> z;
        cost[x][y] = z;
        cost[y][x] = z;
        v[x].push_back({y,z});
        v[y].push_back({x,z});
    }
    sort(loc,loc+k);
    for( int i = 0; i < k; ++i)
    {
        dijkstra(loc[i]);
    }
    for(int i = 1; i <= k ; ++i )
        for( int j = 1 ; j<=k; ++j)
            g[i-1][j-1] = costm[loc[i-1]][loc[j-1]];

    for (int i = 0; i < (1<<k); ++i)
        for (int j = 0; j < k; ++j)
            pd[i][j] = inf;
    pd[1][0] = 0;
    int mn = inf;
    for (int conf = 1; conf < (1<<k); ++conf)
        for (int last = 0; last < k; ++last){
            if(pd[conf][last] != inf)
                for (int i = 0; i < k; ++i)
                    if(g[last][i] != 0 && (conf&(1<<i)) == 0)
                        pd[conf^(1<<i)][i] = min(pd[conf^(1<<i)][i], pd[conf][last]+g[last][i]);
        }
   fout<<pd[(1<<k) -1][k-1];
    return 0;
}