Pagini recente » Cod sursa (job #1227028) | Cod sursa (job #1615585) | Cod sursa (job #1721272) | Cod sursa (job #938015) | Cod sursa (job #1583149)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#define inf 1000000
using namespace std;
vector<pair <int , int > > graf[2002];
priority_queue < pair < int , pair < int , int > > > q;
int n,m,k,kuri[2002],dp[2002][1<<15],ors_max;
void citire()
{
scanf("%d%d",&n,&m);
scanf("%d",&k);
for (int i=1; i<=k; ++i)
{
int x;
scanf("%d",&x);
kuri[x]=i;
}
for (int i=1; i<=m; ++i)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
graf[x].push_back(make_pair(y,c));
graf[y].push_back(make_pair(x,c));
}
}
void dijkstra()
{
q.push(make_pair(0,make_pair(1,0)));
memset(dp,inf,sizeof(dp));
while (!q.empty())
{
int nod=q.top().second.first;
int c=-q.top().first;
int ors=q.top().second.second;
q.pop();
if (ors>ors_max)
ors_max=ors;
for (vector <pair <int ,int > > :: iterator it=graf[nod].begin(); it!=graf[nod].end(); ++it)
{
int x=it->first;
int y=c+it->second;
int op=ors & (1<<(kuri[x]-1));
if (kuri[x] && (ors & (1<<(kuri[x]-1))))
{
if (dp[x][ors] > y)
{
dp[x][ors]=y;
q.push(make_pair(-y,make_pair(x,ors)));
}
}
else if (kuri[x] && op==0)
{
if (dp[x][ors+(1<<(kuri[x]-1))] > y)
{
dp[x][ors+(1<<(kuri[x]-1))]=y;
q.push(make_pair(-y,make_pair(x,ors+(1<<(kuri[x]-1)))));
}
}
else if (!kuri[x] && dp[x][ors] > y)
{
dp[x][ors]=y;
q.push(make_pair(-y,make_pair(x,ors)));
}
}
}
}
int main()
{
freopen("ubuntzei.in","r",stdin);
freopen("ubuntzei.out","w",stdout);
citire();
dijkstra();
if (k==0)
printf("%d",dp[n][0]);
printf("%d",dp[n][(1<<(k))-1]);
return 0;
}