Pagini recente » Cod sursa (job #661009) | Cod sursa (job #1736987) | Cod sursa (job #1013034) | Cod sursa (job #132057) | Cod sursa (job #2562953)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define inf 1000000000
using namespace std;
ifstream fin ("ubuntzei.in");
ofstream fout ("ubuntzei.out");
vector<pair<int,int> >v[2010];
priority_queue <pair<int,int >,vector< pair<int,int> >,greater< pair<int,int > > >pq;
pair<int,int >tt;
int n,m,a,b,c,ap[18],k,d[18][2000],ma[524300][20];
int main()
{
fin>>n>>m>>k;
ap[1]=1;
for(int i=1;i<=k;i++)
fin>>ap[i+1];
ap[k+2]=n;
for(;m;m--)
{
fin>>a>>b>>c;
v[a].push_back(make_pair(c,b));
v[b].push_back(make_pair(c,a));
}
///dijkstra din cele k orase+orasul 1+orasul n
k=k+2;
for(int i=0;i<=k;i++)
{
///ap[i]
for(int j=0;j<=n;j++)
d[i][j]=inf;
d[i][ap[i]]=0;
pq.push(make_pair(0,ap[i]));
while(!pq.empty())
{
tt=pq.top();
pq.pop();
if(tt.first==d[i][tt.second])
{
for(auto it:v[tt.second])
{
if(it.first+tt.first<d[i][it.second])
{
d[i][it.second]=it.first+tt.first;
pq.push(make_pair(d[i][it.second],it.second));
}
}
}
}
}
///nr. pare pt. ca nu avem nod 0
for(int i=6;i<=(1<<k);i++)
for(int j=1;j<=k;j++)
ma[i][j]=inf;
for(int i=1;i<=(1<<k);i++)
ma[(1<<i)][i]=0;
for(int i=6;i<=(1<<k)-2;i=i+4)
{
for(int j=2;j<k;j++)
{
if(i&(1<<j))
{
for(int h=1;h<k;h++)
{
if((1<<h)&(i-(1<<j)))
{
///cout<<i<<" "<<j<<" "<<ma[i][j]<<" "<<h<<" "<<d[h][ap[j]]<<endl;
ma[i][j]=min(ma[i][j],ma[i-(1<<j)][h]+d[h][ap[j]]);
}
}
}
}
}
int minn=inf;
for(int i=2;i<k;i++)
{
///fout<<ma[(1<<k)-2][i]<<" "<<ap[i]<<" "<<d[k][ap[i]]<<endl;
minn=min(minn,ma[(1<<k)-2][i]+d[k][ap[i]]);
}
fout<<minn;
return 0;
}
/*
for(int i=6;i<=(1<<k);i++)
for(int j=1;j<=k;j++)
ma[i][j]=inf;
for(int i=1;i<=(1<<k);i++)
ma[(1<<i)][i]=0;
for(int i=6;i<=(1<<k)-2;i=i+4)
{
for(int j=2;j<k;j++)
{
if(i&(1<<j))
{
for(int h=2;h<k;h++)
{
if((1<<h)&(i-(1<<j)))
{
cout<<i<<" "<<j<<" "<<ma[i][j]<<" "<<h<<" "<<d[h][ap[j]]<<endl;
ma[i][j]=min(ma[i][j],ma[i-(1<<j)][h]+d[h][ap[j]]);
}
}
}
}
}
for(int i=2;i<k;i++)
{
fout<<ma[(1<<k)-2][i]<<" "<<ap[i]<<endl;
}
*/