Pagini recente » Cod sursa (job #1619024) | Cod sursa (job #2058815) | Cod sursa (job #2149309) | Cod sursa (job #213432) | Cod sursa (job #1734537)
#include <bits/stdc++.h>
#define maxN 2002
#define maxM 10002
#define maxS (1 << 15) + 2
#define maxK 17
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define inf 1000000000
using namespace std;
int n, m, k, c[maxK], dp[maxK][maxS], dist[maxN][maxN], ans;
vector < pii > V[maxN];
struct pq
{
int x, c;
bool operator < (const pq & e) const
{
return c > e.c;
}
};
priority_queue < pq > H;
class InputReader
{
public:
InputReader() {}
InputReader(const char *file_name)
{
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n)
{
while(buffer[cursor] < '0' || buffer[cursor] > '9')
{
advance();
}
n = 0;
while('0' <= buffer[cursor] && buffer[cursor] <= '9')
{
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance()
{
++ cursor;
if(cursor == SIZE)
{
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
void read()
{
int i;
InputReader cin("ubuntzei.in");
cin >> n >> m;
cin >> k;
for (i = 1; i <= k; ++ i)
cin >> c[i];
if (k == 0)
{
k = c[1] = 1;
}
while (m --)
{
int x, y, cost;
cin >> x >> y >> cost;
V[x].pb(mp(y, cost));
V[y].pb(mp(x, cost));
}
}
void Dijkstra()
{
int i, j, x;
pq a;
for (x = 1; x <= k; ++ x)
{
for (i = 1; i <= n; ++ i)
dist[x][i] = inf;
dist[x][c[x]] = 0;
a.x = c[x];
a.c = 0;
H.push(a);
while (!H.empty())
{
pq el = H.top();
H.pop();
int y, nod = el.x;
for (j = 0; j < V[nod].size(); ++ j)
if (dist[x][y = V[nod][j].f] > dist[x][nod] + V[nod][j].s)
{
int cost = dist[x][nod] + V[nod][j].s;
dist[x][y = V[nod][j].f] = cost;
a.x = y;
a.c = cost;
H.push(a);
}
}
}
}
void Dp()
{
int i, j, st;
ans = inf;
for (st = 1; st < (1 << k); ++ st)
for (i = 0; i < k; ++ i)
if (st & (1 << i))
{
dp[i][st] = inf;
if (st == (1 << i))
dp[i][st] = dist[i + 1][1];
else
{
for (j = 0; j < k; ++ j)
if (i != j && st & (1 << j) && dp[i][st] > dp[j][st ^ (1 << i)] + dist[i + 1][c[j + 1]])
dp[i][st] = dp[j][st ^ (1 << i)] + dist[i + 1][c[j + 1]];
}
if (st == (1 << k) - 1 && ans > dp[i][st] + dist[i + 1][n])
ans = dp[i][st] + dist[i + 1][n];
}
}
void solve()
{
Dijkstra();
Dp();
}
void write()
{
freopen("ubuntzei.out", "w", stdout);
printf("%d\n", ans);
}
int main()
{
read();
solve();
write();
return 0;
}