Pagini recente » Cod sursa (job #1918030) | Cod sursa (job #1082847) | Cod sursa (job #1481268) | Cod sursa (job #731575) | Cod sursa (job #2291557)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
const int inf = 0x3f3f3f3f;
const int maxN = 752;
const int maxK = 16;
using namespace std;
long long sol, val, x, n, i, j, p, m, k, v[maxN], w[maxN];
long long d[maxK][maxN][maxK];
long long config, nb1;
long long dp[1 << maxK][maxK];
void write();
void solve();
void read();
struct pq
{
long long x, c;
bool operator < (const pq & e) const
{
return c > e.c;
}
} el;
priority_queue < pq > H;
struct edge
{
long long x;
long long y;
long long c;
long long d;
} e;
vector < edge > V[maxN];
int main()
{
read();
solve();
write();
return 0;
}
int bitc(long long x)
{
int n1 = 0;
while (x)
{
++ n1;
x &= x - 1;
}
return n1;
}
void read()
{
freopen("gather.in", "r", stdin);
scanf("%u %u %u", &k, &n, &m);
for (i = 1; i <= k; ++ i)
scanf("%u", &x),
-- x,
v[x] = i,
w[i] = x;
++ k;
w[0] = 0;
while (m --)
{
scanf("%u %u %u %u", &e.x, &e.y, &e.c, &e.d);
-- e.x;
-- e.y;
V[e.x].push_back(e);
swap(e.x, e.y);
V[e.x].push_back(e);
}
}
void solve()
{
int node;
sol = inf;
for (i = 0; i < k; ++ i)
for (j = 0; j < k; ++ j)
{
el.x = w[i];
el.c = 0;
H.push(el);
for (x = 0; x < n; ++ x)
d[i][x][j] = inf;
d[i][w[i]][j] = 0;
while (!H.empty())
{
el = H.top();
x = el.x;
H.pop();
for (p = 0; p < V[x].size(); ++ p)
if (V[x][p].d >= j)
{
node = V[x][p].y;
if (d[i][node][j] > d[i][x][j] + V[x][p].c)
{
d[i][node][j] = d[i][x][j] + V[x][p].c;
el.x = node;
el.c = d[i][node][j];
H.push(el);
}
}
}
}
memset(dp, inf, sizeof(dp));
dp[1][0] = 0;
for (config = 1; config < 1 << k; ++ config)
{
int nb1 = bitc(config);
for (i = 0 ; i < k ; ++ i)
if (config & (1 << i))
for (j = 0; j < k; ++ j)
if (config & (1 << j) && d[j][w[i]][nb1 - 2] != inf)
dp[config][i] = min(dp[config][i], dp[config ^ (1 << i)][j] + (nb1 - 1) * d[j][w[i]][nb1 - 2]);
}
}
void write()
{
freopen("gather.out", "w", stdout);
for (i = 0; i < k; ++ i)
if (dp[(1 << k) - 1][i] < sol)
sol = dp[(1 << k) - 1][i];
printf("%u", sol);
}