Pagini recente » Cod sursa (job #2793976) | Istoria paginii utilizator/cupacatenumarate | Cod sursa (job #2020837) | Cod sursa (job #1623369) | Cod sursa (job #2972068)
#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], v[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 = 0;
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 = 0;
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);
*/
if (n >= 3){
for (int i = 0; i < n - 2; i++)
v[i] = i + 1;
do{
s = cost[0][v[0]];
for (int i = 1; i < n - 2; i++)
s += cost[v[i - 1]][v[i]];
s += cost[v[n - 3]][n - 1];
ans = min(ans, s);
} while (next_permutation(v, v + n - 2));
}
fout << ans << "\n";
return 0;
}