Pagini recente » Istoria paginii utilizator/georgiana_m | Cod sursa (job #1687568) | Cod sursa (job #971925) | Cod sursa (job #457979) | Cod sursa (job #2479040)
#include <bits/stdc++.h>
#define NMAX 2000
#define KMAX 66000
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
const long long INF = 200000000001;
struct Edge {
long long dest, cost;
};
struct Drum{
int bitmask;
int dest;
long long cost;
bool operator<(const Drum& other) const
{
return cost>other.cost;
}
};
vector<Edge> G[NMAX + 1];
priority_queue<Drum> pq;
long long dp[KMAX][NMAX + 1];
int n, m, k, f[NMAX + 1], poz[NMAX + 1];
void Bellman() {
for( int i = 0; i <= (1<<k) - 1; i ++ )
for( int j = 1; j <= n; j ++ )
dp[i][j] = INF;
while( !pq.empty() ) {
int node = pq.top().dest;
long long cost = pq.top().cost;
int config = pq.top().bitmask;
pq.pop();
if( dp[config][node] == INF ) {
dp[config][node] = cost;
if( config == (1<<k) - 1 && node == n )
break;
for( auto it : G[node] ) {
int newconfig = config;
if( f[it.dest] == 1 )
newconfig = config | ( 1<<poz[it.dest] );
if( dp[newconfig][it.dest] > cost + it.cost )
pq.push({ newconfig, it.dest, it.cost + cost });
}
}
}
}
int main() {
fin>>n>>m;
fin>>k;
for( int i = 1; i <= k; i ++ ) {
int x;
fin>>x;
f[x] = 1;
poz[x] = i - 1;
}
for( int i = 1; i <= m; i ++ ) {
long long x, y, p;
fin>>x>>y>>p;
G[x].push_back({y,p});
G[y].push_back({x,p});
}
pq.push({0,1,0});
Bellman();
fout<<dp[(1<<k) - 1][n];
return 0;
}