Pagini recente » Cod sursa (job #2581258) | Cod sursa (job #2574361) | Cod sursa (job #835069) | Cod sursa (job #289263) | Cod sursa (job #2323056)
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n, m, k;
int cities[16];
int cost[2005][2005];
int dp[32800][16];
vector <pair<int, int>> graph[2005];
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
class cmp
{
public:
const bool operator () (const pair<int, int> &a, const pair<int, int> &b)
{
return a.second > b.second;
}
};
void dijkstra(int source)
{
priority_queue <pair<int, int>, vector<pair<int, int>>, cmp> pq;
for (int i = 1; i <= n; ++i) cost[source][i] = 1e9;
cost[source][source] = 0;
pq.push({source, 0});
while (pq.size())
{
int node = pq.top().first;
int cur_Cost = pq.top().second;
pq.pop();
if (cost[source][node] != cur_Cost) continue;
for (auto x : graph[node])
{
if (cost[source][x.first] > cost[source][node] + x.second)
{
cost[source][x.first] = cost[source][node] + x.second;
pq.push({ x.first, cost[source][x.first] });
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
fin.tie(0); fout.tie(0);
fin >> n >> m >> k;
for (int i = 1; i <= k; ++i) fin >> cities[i];
for (int i = 1; i <= m; ++i)
{
int x, y, c;
fin >> x >> y >> c;
graph[x].push_back({ y, c });
graph[y].push_back({ x, c });
cost[x][y] = c;
cost[y][x] = c;
}
dijkstra(1);
for (int i = 1; i <= k; ++i) dijkstra(cities[i]);
int bitmask = (1 << k);
for (int i = 2; i < bitmask; ++i)
for (int j = 1; j <= k; ++j)
dp[i][j] = 1e9;
for (int bit = 0; bit < bitmask; ++bit)
for (int i = 1; i <= k; ++i)
if ( (1<<i)&bit )
for ( int j = 1; j <= k; ++j )
if ( (1<<j)&bit && j != i ) dp[bit][i] = min(dp[bit][i],
dp[bit^(1<<i)][j]+cost[i][j]);
int ans = 1e9;
for (int i = 1; i <= k; ++i) ans = min(ans, dp[(1 << k) - 1][i] + cost[i][n]);
fout << ans+1 << '\n';
}