Pagini recente » Cod sursa (job #1716921) | Cod sursa (job #2769900) | Cod sursa (job #1969151) | Clasamentul arhivei de probleme | Cod sursa (job #2773955)
#include <bits/stdc++.h>
#define mod 1000000007
#define int long long
#define dim 2002
using namespace std;
ifstream fin ("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n,k,viz[dim],asoc[dim],put[22],ord[dim];
const int inf=100000000000000;
struct el
{
int nod,cost,st;
bool operator < (const el &A) const
{
return cost>A.cost;
}
};
priority_queue<el> pq;
vector<el> a[dim];
void dijkstra ()
{
int i,j,sol[n+1][put[k+1]];
for (i=1;i<=n;i++)
for (j=1;j<=put[k+1]-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: a[x.nod])
if(viz[y.nod]==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(viz[y.nod]==1 && x.cost+y.cost<sol[y.nod][x.st|put[asoc[y.nod]]])
{
sol[y.nod][x.st|put[asoc[y.nod]]]=x.cost+y.cost;
pq.push({y.nod,x.cost+y.cost,x.st|put[asoc[y.nod]]});
}
}
fout<<sol[n][put[k+1]-1];
}
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];
viz[ord[i]]=1;
}
viz[1]=1,viz[n]=1;
ord[0]=1,ord[++k]=n;
sort(ord+1,ord+k+1);
for (i=1;i<=k;i++)
asoc[ord[i]]=i;
for (i=1; i<=m; i++)
{
fin>>x>>y>>z;
a[x].push_back({y,z,0});
a[y].push_back({x,z,0});
}
dijkstra();
return 0;
}