Pagini recente » Cod sursa (job #2149783) | Cod sursa (job #712520) | Cod sursa (job #2422618) | Cod sursa (job #1961339) | Cod sursa (job #2481690)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
#define inf INT_MAX
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
struct cmp
{
bool operator() (pair <int, int> &a, pair <int, int> &b)
{
return a.second>b.second;
}
};
bool viz[2001];
int n,m,k;
int v[17];
int dp[17][2001],d[17][(1<<15)+1];
vector < pair <int, int> > g[2001];
priority_queue < pair <int, int> , vector < pair <int, int> >, cmp> h;
void dijkstra(int t, int ns)
{
for(int i=1;i<=n;i++)
if(ns!=i)
d[t][i]=inf;
h.push({ns,0});
while(!h.empty())
{
int nod=h.top().first;
int cost=h.top().second;
h.pop();
if(viz[nod])
continue;
for(int i=0;i<g[nod].size();i++)
{
int v=g[nod][i].first;
int c=g[nod][i].second;
if(d[t][v]>cost+c&&viz[v]==0)
{
d[t][v]=cost+c;
h.push({v,d[t][v]});
}
}
viz[nod]=1;
}
}
int main()
{
fin>>n>>m>>k;
for(int i=1;i<=k;i++)
fin>>v[i];
for(int i=1;i<=m;i++)
{
int x, y, z;
fin>>x>>y>>z;
g[x].push_back({y,z});
g[y].push_back({x,z});
}
v[++k]=1;
for(int i=1;i<=k;i++)
dijkstra(i,v[i]);
if(k==1)
{
fout<<d[1][n];
return 0;
}
else
k--;
for(int i=1;i<=k;i++)
for(int st=1;st<(1<<k);st++)
dp[i][st]=inf;
for(int i=1;i<=k;i++)
dp[i][1<<(i-1)]=d[i][1];
for(int st=1;st<(1<<k);st++)
for(int i=1;i<=k;i++)
if(dp[i][st]!=inf)
{
for(int j=1;j<(1>>k);j++)
if(i!=j&&(((st>>(j-1))&1)==0))
{
int stare=(st|(1<<(j-1)));
dp[j][stare]=min(dp[j][stare],dp[i][st]+d[i][v[j]]);
}
}
int sol=inf;
for(int i=1;i<=k;i++)
sol=min(sol,dp[i][(1<<k)-1]+d[i][n]);
fout<<sol;
return 0;
}