Cod sursa(job #2499217)

Utilizator RedXtreme45Catalin RedXtreme45 Data 25 noiembrie 2019 17:57:12
Problema Ubuntzei Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
#define Nmax 2001
#define Kmax 17
#define INF 0x3f3f3f3f
#define MAX 1000001
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n,m,k,dist[Nmax],v[Kmax];
struct NodCost{
    int nod,cost;
    bool operator <(const NodCost &var)const{
        return cost>var.cost;
    }
};
vector <NodCost> G[Nmax],g[Kmax];
void dijkstra(int start)
{
    memset(dist,INF ,sizeof dist);
    priority_queue <NodCost> q;
    q.push({start,0});
    dist[start]=0;
    while (!q.empty()){
        int x=q.top().nod;
        int y=q.top().cost;
        q.pop();
        if (y!=dist[x]) continue;
        for (auto j:G[x]){
            if (dist[j.nod]>y+j.cost)
            {
                dist[j.nod]=y+j.cost;
                q.push({j.nod,dist[j.nod]});
            }
        }
    }
}
int dp[MAX][Kmax],min1=INF;
void rezolvare()
{
    int stare,i,j;
    for (stare=1; stare<=((1<<k)-1); stare++)
    {
            for (i=0; i<k; i++)
            {
                if (((1<<i) & stare)>0 && dp[stare][i]!=INF)
                {
                    for (auto j:g[i])
                    {
                            if (((1<<j.nod) & stare)==0)
                                dp[stare+(1<<j.nod)][j.nod]=min(dp[stare+(1<<j.nod)][j.nod],dp[stare][i]+j.cost);
                    }
                }
            }

    }
}
int main()
{
    int a,b,c,i;
    fin>>n>>m>>k;
    v[0]=1;
    for (i=1;i<=k;i++)
        fin>>v[i];
    k++;
    v[k]=n;
    for (i=1;i<=m;i++){
        fin>>a>>b>>c;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    for (i=0;i<=k;i++)
    {
        dijkstra(v[i]);
        for (int j=0;j<=k;j++)
        {
            if (i!=j)
            {
                g[i].push_back({j,dist[v[j]]});
            }
        }
    }
    k++;
    for (i=1;i<=(1<<k)-1;i++)
    {
        for (int j=0;j<k;j++)
        {
            dp[i][j]=INF;
        }
    }
    dp[1][0]=0;
    rezolvare();
    for (i=1;i<k;i++)
    {
       min1=min(min1,dp[(1<<k)-1][i]);
    }
    fout<<min1;
    return 0;
}