Pagini recente » Cod sursa (job #746776) | Rating Griga Victor-Cristian (vicvic) | Rating Bujor Mihai Alexandru (BujorelActimel) | Cod sursa (job #3255386) | Cod sursa (job #2840728)
#include <fstream>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
ifstream fin( "ubuntzei.in" );
ofstream fout( "ubuntzei.out" );
using ll = long long;
const ll INF = (ll)1e18;
const int DIM = 2005;
const int MAXN = 19;
vector<pair<int, int>> G[DIM];
int stop[MAXN];
ll cost[MAXN][MAXN];
ll minCostCyc( int n ) {
vector<vector<ll>> dp((1 << n), vector<ll>(n, INF));
dp[1][0] = 0;
for ( int mask = 1; mask < (1 << n); ++mask ) {
if ( (mask & 1) == 0 ) continue;
for ( int lst = 0; lst < n; ++lst ) {
for ( int nxt = 0; nxt < n; ++nxt ) {
if ( (mask & (1 << nxt)) == 0 && cost[lst][nxt] != INF ) {
dp[mask + (1 << nxt)][nxt] = min(dp[mask + (1 << nxt)][nxt], dp[mask][lst] + cost[lst][nxt]);
}
}
}
}
return dp[(1 << n) - 1][n - 1];
}
ll dist[DIM];
void dijkstra( int b ) {
priority_queue<pair<ll, int>> pq;
pq.push({0, b});
dist[b] = 0;
while ( !pq.empty() ) {
int u = pq.top().second;
pq.pop();
for ( auto ngh : G[u] ) {
if ( dist[u] + ngh.second < dist[ngh.first] ) {
dist[ngh.first] = dist[u] + ngh.second;
pq.push({-dist[ngh.first], ngh.first});
}
}
}
}
int main() {
int n, m, k, u, v, c;
fin >> n >> m >> k;
for ( int i = 0; i < k; ++i ) {
fin >> stop[i];
}
stop[k++] = 1; stop[k++] = n;
while ( m-- ) {
fin >> u >> v >> c;
G[u].push_back({v, c});
G[v].push_back({u, c});
}
for ( int i = 0; i < k; ++i ) {
for ( int j = 0; j < k; ++j ) {
cost[i][j] = INF;
}
}
for ( int i = 0; i < k; ++i ) {
for ( int x = 1; x <= n; ++x ) {
dist[x] = INF;
}
dijkstra(stop[i]);
for ( int j = 0; j < k; ++j ) {
if ( i == j ) continue;
cost[i][j] = dist[stop[j]];
}
}
fout << minCostCyc(k);
fin.close();
fout.close();
return 0;
}