Pagini recente » Cod sursa (job #1766929) | Cod sursa (job #583892) | Cod sursa (job #1514956) | Cod sursa (job #2671956) | Cod sursa (job #2171941)
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int NMAX = 2000;
struct oras
{
int x, config, dist;
};oras aux;
struct drum
{
int x, dist;
};drum nou;
class MyCmp
{
public:
bool operator ()(const oras &a, const oras &b)
{
if (a.dist == b.dist)
return a.config > b.config;
return a.dist > b.dist;
}
};
priority_queue <oras, vector<oras>, MyCmp> pq;
vector <drum> g[NMAX + 5];
bool viz[NMAX + 5][33000];
int ok[NMAX + 5];
int main()
{
freopen("ubuntzei.in", "r", stdin);
freopen("ubuntzei.out", "w", stdout);
int n, m, k, x, y, config, dist;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; ++i)
ok[i] = -1;
for (int i = 0; i < k; ++i)
scanf("%d", &x), ok[x] = i;
for (int i = 1; i <= m; ++i)
{
scanf("%d %d %d", &x, &y, &dist);
nou.dist = dist;
nou.x = y; g[x].push_back(nou);
nou.x = x; g[y].push_back(nou);
}
aux.config = 0; aux.x = 1; aux.dist = 0;
pq.push(aux);
while (!pq.empty())
{
x = pq.top().x;
config = pq.top().config;
dist = pq.top().dist;
pq.pop();
if (viz[x][config] == 1)
continue;
viz[x][config] = 1;
if (x == n && config == ((1 << k) - 1))
{
printf("%d\n", dist);
return 0;
}
for (int i = 0; i < g[x].size(); ++i)
{
aux.x = g[x][i].x;
aux.dist = dist + g[x][i].dist;
aux.config = config;
if (ok[aux.x] != -1 && ((1 << ok[aux.x]) & config) == 0)
aux.config += (1 << ok[aux.x]);
if (viz[aux.x][aux.config] == 0)
pq.push(aux);
}
}
return 0;
}