Pagini recente » Cod sursa (job #1529150) | Cod sursa (job #2072111) | Cod sursa (job #2043356) | Cod sursa (job #2477) | Cod sursa (job #2713289)
#include <fstream>
#include <bitset>
#include <queue>
#include <vector>
using namespace std;
ifstream cin ("ubuntzei.in");
ofstream cout ("ubuntzei.out");
bitset <32768> viz[2002];
struct idk {
int cost, nod, stare;
};
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];
void dijkstra() {
int cost = 0, nod = 1, stare = loc[1];
pq.push({cost, nod, stare});
while (!pq.empty()) {
cost = pq.top().cost;
nod = pq.top().nod;
stare = pq.top().stare;
pq.pop();
if (viz[nod][stare])
continue;
viz[nod][stare] = 1;
// cout << cost << ' ' << nod << ' ' << stare << '\n';
if (stare == (1 << k) - 1 && nod == n) {
cout << cost;
exit(0);
}
for (auto it : v[nod]) {
if (loc[it.first]) {
int stare2 = (stare | loc[it.first]);
if (viz[it.first][stare2])
continue;
pq.push({cost + it.second, it.first, stare2});
} else {
if (viz[it.first][stare])
continue;
pq.push({cost + it.second, it.first, stare});
}
}
}
}
int main() {
int m, x, y, C;
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
cin >> x;
loc[x] = (1 << (i - 1));
}
for (int i = 1; i <= m; ++i) {
cin >> x >> y >> C;
v[x].push_back({y, C});
v[y].push_back({x, C});
}
dijkstra();
return 0;
}