Pagini recente » Cod sursa (job #3203196) | Cod sursa (job #3153747) | Cod sursa (job #2343212) | Cod sursa (job #2343303) | Cod sursa (job #2871487)
#include <bits/stdc++.h>
#define ii pair<int, int>
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
bitset<2003> imp_city;
vector<vector<ii>> edges(2003);
vector<vector<int>> dist(2003, vector<int>(2003, 2e9));
void find_dist(int node) {
priority_queue<ii, vector<ii>, greater<ii>> nodes;
nodes.push({0, node});
dist[node][node] = 0;
while (!nodes.empty()) {
int cur_dist = nodes.top().first, cur_node = nodes.top().second;
nodes.pop();
for (auto itr : edges[cur_node]) {
int dest = itr.first, cost = itr.second;
if (imp_city[dest] and dest < node and dist[dest][node] == cost + cur_dist) {
nodes.push({dist[dest][node], dest});
} else if (dist[node][dest] > cost + cur_dist) {
dist[node][dest] = dist[dest][node] = cost + cur_dist;
nodes.push({cost + cur_dist, dest});
}
}
}
}
int32_t main() {
int n, m, k;
fin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
int num; fin >> num;
imp_city[num] = true;
}
imp_city[1] = imp_city[n] = true;
for (int i = 1; i <= m; ++i) {
int a, b, t;
fin >> a >> b >> t;
edges[a].push_back({b, t});
edges[b].push_back({a, t});
}
vector<int> act_cities;
for (int i = 1; i <= n; ++i) {
if (imp_city[i]) {
find_dist(i);
act_cities.push_back(i);
}
}
k += 2;
vector<vector<int>> min_cost(1 << k, vector<int>(k, 2e9));
min_cost[1][0] = 0;
for (int mask = 1; mask < (1 << k); ++mask) {
for (int i = 0; i < k; ++i) {
for (int bit = 0; bit < k; ++bit) {
if (mask & (1 << bit)) {
continue;
}
int nmask = mask | (1 << bit);
min_cost[nmask][bit] = min(min_cost[mask][i] + dist[act_cities[i]][act_cities[bit]], min_cost[nmask][bit]);
}
}
}
fout << min_cost[(1 << k) - 1][k - 1];
return 0;
}