Cod sursa(job #2572025)

Utilizator Leonard123Mirt Leonard Leonard123 Data 5 martie 2020 11:16:19
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define maxn 2000
#define sd second
#define ft first
using namespace std;
int n,m,k,p[20],cost[maxn],c[20][20],sol[150000][20],rez,x,y,z;
vector<pair<int,int> >:: iterator it;
vector<pair<int,int> >v[maxn];
queue<int> q;

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

void bellman(int x){
  for(int i=1; i<=n; i++)
    cost[i]=inf;
  cost[x]=0;
  q.push(x);
  while(!q.empty()){
    x=q.front(); q.pop();
    for(it=v[x].begin(); v[x].end()!=it; it++)
      if(cost[(*it).ft]>cost[x]+(*it).sd){
        cost[(*it).ft]=cost[x]+(*it).sd;
        q.push((*it).ft);
      }
  }
}

int main(){
  ccin>>n>>m>>k;
  for(int i=1; i<=k; i++)
    ccin>>p[i];
  for(int i=1; i<=m; i++)
    ccin>>x>>y>>z, v[x].pb({y,z}), v[y].pb({x,z});
  p[0]=1; p[k+1]=n;
  for(int i=0; i<=k+1; i++){
    bellman(p[i]);
    for(int j=0; j<=k+1; j++)
      c[i][j]=cost[p[j]];
  }
  if(k==0){
    ccout<<cost[n];
    return 0;
  }else{
    int r=1<<k;
    for(int i=0; i<=r; i++)
      for(int j=0; j<=k; j++)
        sol[i][j]=inf;
    sol[0][0]=0;
    for(int i=1; i<=r; i++)
      for(int j=0; j<=k; j++)
        if(i&&(1<<j))
          for(int excluded=(i-(1<<j)),x=0; x<=k; x++)
            sol[i][j+1]=min(sol[i][j+1],sol[excluded][x]+c[x][j+1]);
    rez=inf;
    for(int i=1; i<=k; i++)
      rez=min(rez,sol[r-1][i]+c[i][k+1]);
    ccout<<rez;
  }
  return 0;
}