Pagini recente » Cod sursa (job #2656051) | Cod sursa (job #304343) | Cod sursa (job #1240542) | Cod sursa (job #2258476) | Cod sursa (job #1210951)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
struct cell
{
int nod,cost;
bool const operator <(const cell& e) const
{
return cost>e.cost;
}
};
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const int conf=(1<<15);
const int oo=1<<30;
const int NMAX=2005;
const int KMAX=16;
int n,m,k,a[KMAX];
int dp[KMAX][NMAX];
vector<cell>v[NMAX];
priority_queue<cell>Q;
int mat[conf][KMAX];
int sol;
inline void Citire()
{
int i,x;
cell ca;
fin>>n>>m;
fin>>k;
for (i=1;i<=k;i++) fin>>a[i];
for (i=1;i<=m;i++)
{
fin>>x>>ca.nod>>ca.cost;
v[x].push_back(ca);
swap(x,ca.nod);
v[x].push_back(ca);
}
}
inline void DIJKSTRA(int poz,int x)
{
int i;
cell ca,aux;
for (i=1;i<=n;i++) dp[poz][i]=oo;
dp[poz][x]=0;
ca.nod=x;
ca.cost=0;
Q.push(ca);
while (!Q.empty())
{
ca=Q.top();
Q.pop();
if (ca.cost==dp[poz][ca.nod])
{
for (vector<cell>::iterator it=v[ca.nod].begin();it!=v[ca.nod].end();it++)
{
aux=*it;
if (dp[poz][aux.nod]>dp[poz][ca.nod]+aux.cost)
{
dp[poz][aux.nod]=dp[poz][ca.nod]+aux.cost;
aux.cost=dp[poz][aux.nod];
Q.push(aux);
}
}
}
}
}
inline void Preprocesare()
{
int i;
DIJKSTRA(0,1);
for (i=1;i<=k;i++) DIJKSTRA(i,a[i]);
}
inline void SOLVE()
{
int i,j,l,aux;
if (k)
{for (j=1;j<=(1<<k)-1;j++)
for (l=1;l<=k;l++)
mat[j][l]=oo;
for (i=1;i<=k;i++) mat[(1<<(i-1))][i]=dp[0][a[i]];
for (j=1;j<=(1<<k)-1;j++)
for (l=1;l<=k;l++)
if (j&(1<<(l-1)))
for (aux=1;aux<=k;aux++)
if (aux!=l && j&(1<<(aux-1)))
mat[j][l]=min(mat[j][l],mat[j-(1<<(l-1))][aux] + dp[aux][a[l]]);
sol=oo;
for (j=1;j<=k;j++)
sol=min(sol,mat[(1<<k)-1][j]+dp[j][n]);
fout<<sol<<"\n";}
else fout<<dp[0][n]<<"\n";
}
int main()
{
Citire();
Preprocesare();
SOLVE();
return 0;
}