Pagini recente » Cod sursa (job #703151) | Cod sursa (job #571348) | Cod sursa (job #1503209) | Cod sursa (job #749428) | Cod sursa (job #2027512)
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
const int maxn = 2005;
int dp[maxn][(1 << 16)];
int poz[20];
class cmp {
public:
bool operator()( pair<int, int> a, pair<int, int> b ) {
return a.second > b.second;
}
}; priority_queue<pair<int, int>, vector< pair<int, int> >, cmp> pq;
vector <pair <int, int> > v[maxn];
int get(int conf, int x)
{
if(poz[x] == -1)
return conf;
if(conf & (1 << poz[x]))
return conf;
return conf ^ (1 << poz[x]);
}
int main()
{
int n, m, k;
in >> n >> m >> k;
memset(poz, -1, sizeof(poz));
for(int i = 0; i < k; i++)
{
int x;
in >> x;
poz[x] = i;
}
for(int i = 1; i <= m; i++)
{
int x, y, cost;
in >> x >> y >> cost;
v[x].push_back(make_pair(y, cost));
v[y].push_back(make_pair(x, cost));
}
int nrconf = (1 << k);
for(int i = 0; i <= n; i++)
for(int j = 0; j < nrconf; j++)
dp[i][j] = (1 << 30);
pq.push(make_pair(1, 0));
dp[1][0] = 0;
while(!pq.empty())
{
pair<int, int> p = pq.top();
cerr << p.first << " ";
pq.pop();
for(auto it : v[p.first])
{
for(int i = 0; i < nrconf; i++)
{
int nod = it.first;
int cost = it.second;
int nou = get(i, nod);
if(dp[nod][nou] > dp[p.first][i] + cost)
{
dp[nod][nou] = dp[p.first][i] + cost;
pq.push(make_pair(nod, dp[nod][nou]));
}
}
}
}
out << dp[n][nrconf - 1] << "\n";
return 0;
}