Pagini recente » Cod sursa (job #2124975) | Cod sursa (job #2713314)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin ("ubuntzei.in");
ofstream cout ("ubuntzei.out");
struct idk {
int cost, nod;
};
auto cmp = [](idk x, idk y) {
return (x.cost > y.cost);
};
priority_queue <idk, vector <idk>, decltype(cmp)> pq(cmp);
vector <pair <int, int> > v[2002];
int n, k;
int loc[2002], r[20];
int dp[65540][17], d[17][17], dist[17][2002];
void dijkstra(int nod, int i) {
pq.push({0, nod});
while (!pq.empty()) {
int cost = pq.top().cost;
int nod = pq.top().nod;
pq.pop();
if (dist[i][nod] != 0 && dist[i][nod] < cost)
continue;
for (auto it : v[nod]) {
if (loc[it.first] == i)
continue;
if (dist[i][it.first] != 0 && dist[i][it.first] < cost + it.second)
continue;
pq.push({cost + it.second, it.first});
dist[i][it.first] = cost + it.second;
}
}
}
int main() {
int m, x, y, C;
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
cin >> x;
r[i] = x;
loc[x] = i;
}
for (int i = 1; i <= m; ++i) {
cin >> x >> y >> C;
v[x].push_back({y, C});
v[y].push_back({x, C});
}
r[0] = 1, loc[1] = 0;
dijkstra(1, 0);
for (int i = 1; i <= k; ++i)
dijkstra(r[i], i);
for (int i = 0; i <= k; ++i) {
for (int j = i + 1; j <= k; ++j) {
d[i][j] = d[j][i] = dist[i][r[j]];
}
}
int lim = (1 << (k + 1));
for (int stare = 0; stare < lim; ++stare)
for (int i = 0; i <= k; ++i)
dp[stare][i] = 1e9;
dp[1][0] = 0;
for (int stare = 1; stare < lim; ++stare) {
for (int i = 0; i <= k; ++i) {
if (!(stare & (1 << i)))
continue;
for (int j = 0; j <= k; ++j) {
if (i == j)
continue;
if (!(stare & (1 << j)))
continue;
dp[stare][i] = min(dp[stare][i], dp[stare - (1 << i)][j] + d[i][j]);
}
}
}
int ans = 2e9;
for (int i = 0; i <= k; ++i) {
ans = min(ans, dp[lim - 1][i] + dist[i][n]);
}
cout << ans;
return 0;
}