Cod sursa(job #2542138)

Utilizator andra1782Andra Alazaroaie andra1782 Data 9 februarie 2020 15:53:43
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#define INF 1000000000
#define MAXK 17
#define MAXN 2001
int n,v[MAXK],dj[MAXK][MAXN],d[1<<MAXK][MAXK];

struct edge{
  int y,cost;
};

struct comp{
  bool operator()(const edge &a, const edge &b){
    return a.cost>b.cost;
  }
};

std::vector<edge>l[MAXN];

int main(){
  FILE *fin=fopen("ubuntzei.in","r");
  FILE *fout=fopen("ubuntzei.out","w");
  int m,k,i,j,x,y,cost;

  fscanf(fin,"%d%d%d",&n,&m,&k);
  v[0]=1;
  for(i=1; i<=k; i++)
    fscanf(fin,"%d",&v[i]);
  v[k+1]=n;
  k+=2;
  for(i=0; i<m; i++){
    fscanf(fin,"%d%d%d",&x,&y,&cost);
    l[x].push_back({y,cost});
    l[y].push_back({x,cost});
  }
  std::priority_queue<edge,std::vector<edge>,comp>q;
  for(x=0; x<k; x++){
    for(int i=1; i<=n; i++)
      dj[x][i]=INF;
    q.push({v[x],0});
    while(!q.empty()){
      edge f=q.top();
      q.pop();
      if(dj[x][f.y]==INF){
        dj[x][f.y]=f.cost;
        for(auto i: l[f.y])
          if(dj[x][i.y]==INF)
            q.push({i.y,f.cost+i.cost});
      }
    }
  }
  for(i=0; i<(1<<k); i++)
    for(j=0; j<k; j++)
      d[i][j]=INF;
  d[1][0]=0;
  for(int mask=3; mask<(1<<k); mask++)
    for(i=1; i<k; i++)
      if(mask&(1<<i))
        for(j=0; j<k; j++)
          if(i!=j && mask&(1<<j))
            d[mask][i]=std::min(d[mask][i],d[mask^(1<<i)][j]+dj[j][v[i]]);
  fprintf(fout,"%d\n",d[(1<<k)-1][k-1]);
  fclose(fin);
  fclose(fout);
  return 0;
}