Pagini recente » Cod sursa (job #2685991) | Cod sursa (job #1971525) | Cod sursa (job #1492233) | Cod sursa (job #388303) | Cod sursa (job #1095071)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("ubuntzei.in");
ofstream fout ("ubuntzei.out");
const int N = 2005, K = 20, oo = 5e8;
priority_queue <pair <int, int>, vector <pair <int, int> >, greater <pair <int, int> > > H;
vector <int> g[N], c[N], city, d(N, oo);
int n, m, k, a[K][K], D[1 << K][K];
int Dynamic (int stare, int nod) {
if (stare && !nod)
return oo;
if (D[stare][nod] < oo)
return D[stare][nod];
for (int i = 0, p = 1; p <= stare; i++, p <<= 1)
if (p & stare)
D[stare][nod] = min(D[stare][nod], Dynamic(stare - p, i) + a[nod][i]);
return D[stare][nod];
}
void Dijkstra() {
while (H.size()) {
int nod = H.top().second, cost = H.top().first;
H.pop();
if (cost > d[nod])
continue;
for (int i = 0; i < g[nod].size(); ++i) {
int newn = g[nod][i], newc = c[nod][i];
if (d[newn] > d[nod] + newc) {
d[newn] = d[nod] + newc;
H.push (make_pair (d[nod] + newc, newn));
}
}
}
}
int main() {
fin >> n >> m >> k;
for (int x, i = 0; i < k; ++i) {
fin >> x;
city.push_back (x);
}
for (int x, y, cost; m; --m) {
fin >> x >> y >> cost;
g[x].push_back (y);
c[x].push_back (cost);
g[y].push_back (x);
c[y].push_back (cost);
}
fin.close();
for (unsigned i = 0; i < city.size(); ++i) {
H.push (make_pair (0, city[i]));
d[city[i]] = 0;
Dijkstra();
a[0][i+1] = a[i+1][0] = d[1];
for (int j = 0; j < i; ++j)
a[i+1][j+1] = a[j+1][i+1] = d[city[j]];
a[k+1][i+1] = a[i+1][k+1] = d[n];
for (vector <int> :: iterator it = d.begin(); it != d.end(); ++it)
*it = oo;
}
if (!k) { //Caz exceptat
H.push (make_pair (0, 1));
d[1] = 0;
Dijkstra();
fout << d[n];
return 0;
}
for (int i = 0; i < (1 << (k + 1)); ++i)
for (int j = 0; j <= k + 1; ++j)
D[i][j] = oo;
D[0][0] = 0;
fout << Dynamic((1 << (k + 1)) - 1, k+1);
}