Pagini recente » Cod sursa (job #1908651) | Cod sursa (job #2896001) | Cod sursa (job #95227) | Cod sursa (job #1677606) | Cod sursa (job #2774161)
#include <bits/stdc++.h>
#define dim 2002
using namespace std;
ifstream fin ("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n,k,put[22],ord[dim],Sol[18][131073],sol[dim];
const int inf=2000000000;
struct el
{
int nod,cost,st;
bool operator < (const el &A) const
{
return cost>A.cost;
}
};
priority_queue<el> pq;
vector<el> a[dim],aa[dim];
void dijkstra ()
{
int i,j;
for (i=1; i<=k; i++)
for (j=1; j<=put[k]-1; ++j)
Sol[i][j]=inf;
Sol[1][1]=0;
pq.push({1,0,1});
while (!pq.empty())
{
el x=pq.top();
pq.pop();
if (x.cost==Sol[x.nod][x.st])
for (auto y: aa[x.nod])
if((x.st&put[y.nod-1])!=0 && x.cost+y.cost<Sol[y.nod][x.st])
{
Sol[y.nod][x.st]=x.cost+y.cost;
pq.push({y.nod,Sol[y.nod][x.st],x.st});
}
else if((x.st&put[y.nod-1])==0&&x.cost+y.cost<Sol[y.nod][x.st|put[y.nod-1]])
{
Sol[y.nod][x.st|put[y.nod-1]]=x.cost+y.cost;
pq.push({y.nod,x.cost+y.cost,x.st|put[y.nod-1]});
}
}
fout<<Sol[k][put[k]-1];
}
void dijk ()
{
while (!pq.empty())
{
el x=pq.top();
pq.pop();
if (x.cost==sol[x.nod])
for (auto y: a[x.nod])
if (x.cost+y.cost<sol[y.nod])
{
sol[y.nod]=x.cost+y.cost;
pq.push({y.nod,sol[y.nod]});
}
}
}
int32_t main()
{
int i,j,m,x,y,z;
fin>>n>>m>>k;
put[0]=1;
for (i=1; i<=20; i++)
put[i]=put[i-1]*2;
for (i=1; i<=k; i++)
fin>>ord[i];
ord[++k]=1;
ord[++k]=n;
sort(ord+1,ord+k+1);
for (i=1; i<=m; i++)
{
fin>>x>>y>>z;
a[x].push_back({y,z,0});
a[y].push_back({x,z,0});
}
for (i=1; i<=k; i++)
{
for (j=1; j<=n; j++)
sol[j]=inf;
sol[ord[i]]=0;
pq.push ({ord[i],0});
dijk();
for (j=1; j<=k; j++)
if (i!=j && sol[ord[j]]!=inf)
aa[i].push_back({j,sol[ord[j]],0});
}
dijkstra();
return 0;
}