Pagini recente » Cod sursa (job #1536771) | Cod sursa (job #3142037) | Teorema chineza a resturilor - generalizari si aplicatii | Cod sursa (job #2056214) | Cod sursa (job #3003219)
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using ii = pair<int, int>;
const int MN = 2071;
const int MK = 16;
const int INF = 1e9;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
} fi("ubuntzei.in");
ofstream fo("ubuntzei.out");
int n, m, k;
int A[MK], Cost[MN][MN], Procesat[MN];
vector<ii> L[MN];
int DP[MK + 3][1 << MK], Prieten[MN];
void dijk(int u) {
for(int i = 1; i <= n; ++i) Cost[u][i] = INF, Procesat[i] = 0;
priority_queue<ii> PQ;
Cost[u][u] = 0;
PQ.push({0, u});
while(!PQ.empty()) {
int nod = PQ.top().second, cost = PQ.top().first;
PQ.pop();
if(Cost[u][nod] != -cost) continue;
if(Procesat[nod]) continue;
Procesat[nod] = 1;
for(auto [it, w] : L[nod]) {
if(Cost[u][it] > Cost[u][nod] + w) {
Cost[u][it] = Cost[u][nod] + w;
PQ.push({-Cost[u][it], it});
}
}
}
for(int i = 1; i <= n; ++i)
Cost[i][u] = Cost[u][i];
}
int Loc[MK + 3];
int main() {
fi >> n >> m >> k;
for(int i = 1; i <= k; ++i) fi >> A[i], Prieten[A[i]] = i;
for(int i = 1; i <= k; ++i) {
Loc[i] = A[i];
}
Loc[k + 1] = 1;
Loc[k + 2] = n;
int u, v, w;
for(int i = 1; i <= m; ++i) {
fi >> u >> v >> w;
L[u].push_back({v, w});
L[v].push_back({u, w});
}
for(int i = 1; i <= k + 2; ++i) {
dijk(Loc[i]);
}
// while(cin >> u >> v) {
// cout << Cost[u][v] << "\n";
// }
for(int i = 0; i < (1 << k); ++i) {
for(int j = 1; j <= k + 2; ++j) {
DP[j][i] = INF;
}
}
DP[k + 1][0] = 0;
for(int i = 0; i < (1 << k); ++i) {
for(int j = 0; j < k; ++j) {
if(!(i & (1 << j))) {
for(int u = 1; u <= k + 2; ++u) {
DP[j+1][i | (1 << j)] = min(DP[j+1][i | (1 << j)], DP[u][i] + Cost[Loc[u]][Loc[j + 1]]);
}
}
}
}
int re = INF;
for(int i = 1; i <= k + 2; ++i)
re = min(re, DP[i][(1 << k) - 1] + Cost[Loc[i]][n]);
fo << re << "\n";
return 0;
}