Pagini recente » Cod sursa (job #1567647) | Cod sursa (job #2045598) | Cod sursa (job #1005285) | Cod sursa (job #271848) | Cod sursa (job #2972053)
#include <bits/stdc++.h>
#define INF 1000000001
#define L 2005
#define S 20
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
int n, m, k;
vector <pair <int, int>> G[L];
vector <int> nodes;
int dist[L], cost[S][S];
bool vis[S];
void init_vis(){
for (int i = 0; i < S; i++)
vis[i] = false;
}
void dijkstra(int src){
priority_queue <pair <int, int>> pq;
int i, node, cost;
for (i = 1; i <= n; i++)
dist[i] = INF;
dist[src] = 0;
pq.push({-dist[src], src});
while (!pq.empty()){
cost = -pq.top().first;
node = pq.top().second;
pq.pop();
if (cost == dist[node])
for (auto it : G[node])
if (cost + it.second < dist[it.first]){
dist[it.first] = cost + it.second;
pq.push({-dist[it.first], it.first});
}
}
for (i = 1; i <= n; i++)
if (dist[i] == INF)
dist[i] = 0;
}
int main(){
fin >> n >> m >> k;
nodes.push_back(1);
for (int i = 0; i < k; i++){
int x;
fin >> x;
nodes.push_back(x);
}
nodes.push_back(n);
for (int i = 0; i < m; i++){
int a, b, c;
fin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
for (int i = 0; i < (int)nodes.size(); i++){
dijkstra(nodes[i]);
for (int j = 0; j < (int)nodes.size(); j++)
cost[i][j] = dist[nodes[j]];
}
n = nodes.size();
int ans = INF;
int x, s;
init_vis();
x = s = 0;
vis[x] = true;
for (int i = 0; i < n - 2; i++){
int mn = INF, p;
for (int j = 1; j < n - 1; j++)
if (!vis[j] && mn > cost[x][j]){
mn = cost[x][j];
p = j;
}
vis[p] = true;
x = p;
s += mn;
}
s += cost[x][n - 1];
ans = min(ans, s);
init_vis();
s = 0;
x = n - 1;
vis[x] = true;
for (int i = 0; i < n - 2; i++){
int mn = INF, p;
for (int j = 1; j < n - 1; j++)
if (!vis[j] && mn > cost[x][j]){
mn = cost[x][j];
p = j;
}
vis[p] = true;
x = p;
s += mn;
}
s += cost[x][0];
ans = min(ans, s);
fout << ans << "\n";
return 0;
}