Pagini recente » Cod sursa (job #1101415) | Cod sursa (job #2055418) | Cod sursa (job #2230838) | Cod sursa (job #91238) | Cod sursa (job #2838417)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const int MAX=205;
const int INF=1e9;
int n,m,k,a,b,d,dtotal,c[MAX],dist[MAX][MAX],st[MAX],viz[MAX];
void bck(int p)
{
if(p==k+1)
{
int dx=0;
for(int i=1;i<=p;i++)
dx+=dist[st[i-1]][st[i]];
if(dx<dtotal)
dtotal=dx;
}
else
for(int i=1;i<=k;i++)
if(!viz[c[i]])
{
viz[c[i]]=1;
st[p]=c[i];
bck(p+1);
viz[c[i]]=0;
}
}
int main()
{
fin >> n >> m >> k;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
dist[i][j]=INF;
for(int i=1;i<=k;i++)
fin >> c[i];
while(m--)
{
fin >> a >> b >> d;
dist[a][b]=dist[b][a]=d;
}
for(int p=1;p<=n;p++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dist[i][p]+dist[p][j]<dist[i][j])
dist[i][j]=dist[i][p]+dist[p][j];
dtotal=INF;
st[0]=1;
st[k+1]=n;
bck(1);
fout << dtotal;
return 0;
}