Pagini recente » Cod sursa (job #1635240) | Cod sursa (job #762094) | Cod sursa (job #2401057) | Cod sursa (job #595741) | Cod sursa (job #865727)
Cod sursa(job #865727)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
const int maxn = 2002;
int n,m,k;
int must[20];
int best[maxn][1<<15];
struct edge
{
int to,cost;
};
struct state
{
int nod,conf;
};
vector<edge> graf[maxn];
vector<edge> :: iterator it;
queue<state> q;
int bsearch(const int &nod)
{
int i,step = (1<<3);
for(i=0;step;step>>=1)
if(i+step <= k && must[i+step] <= nod)
i += step;
return must[i] == nod ? i-1 : -1;
}
void bford()
{
state c;
int aux;
int x;
while(!q.empty())
{
c = q.front();
q.pop();
for(it = graf[c.nod].begin();it != graf[c.nod].end(); ++ it)
{
aux = c.conf;
x = bsearch(it->to);
if(x != -1)
aux |= (1 << x);
if(best[c.nod][c.conf] + it->cost < best[it->to][aux] || !best[it->to][aux])
{
best[it->to][aux] = best[c.nod][c.conf] + it->cost;
q.push((state){it->to,aux});
}
}
}
}
int main()
{
int i,x,y,z;
in >> n >> m >> k;
for(i=1;i<=k;++i)
{
in >> x;
must[i] = x;
}
sort(must+1,must+k+1);
for(;m;--m)
{
in >> x >> y >> z;
graf[x].push_back((edge){y,z});
graf[y].push_back((edge){x,z});
}
q.push((state){1,0});
best[1][0] = 1;
bford();
out << best[n][(1 << k)-1]-1;
return 0;
}