Pagini recente » Cod sursa (job #324565) | Profil patricia625 | tema | Cod sursa (job #296452) | Cod sursa (job #1082096)
#include <fstream>
#include <algorithm>
#include <vector>
#define Nmax 210
#define Mmax 10099
#define Kmax 20
#define Inf 2000000000
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
int N,M,K,sol,C[Nmax][Nmax],U[Kmax];
int main()
{
f>>N>>M>>K;
for(int i=1;i<=K;++i)f>>U[i];
for(int i=1;i<=M;++i)
{
int x,y,c;
f>>x>>y>>c;
C[x][y]=C[y][x]=c;
}
for(int i=1;i<=N;++i)
for(int j=1;j<=N;++j)
if(i!=j)C[i][j]=Inf;
for(int k=1;k<=N;++k)
for(int i=1;i<=N;++i)
for(int j=1;j<=N;++j)
if(i!=j && (C[i][j]>C[i][k]+C[k][j]))C[i][j]=C[i][k]+C[k][j];
if(!K)
{
g<<C[1][N]<<'\n';
return 0;
}
sort(U+1,U+1+K);
sol=Inf;
do
{
int cost=C[1][U[1]];
for(int i=1;i<K;++i)cost+=C[U[i]][U[i+1]];
if(cost<sol)sol=cost;
}while (next_permutation(U+1,U+1+K));
g<<sol<<'\n';
f.close();g.close();
return 0;
}