Cod sursa(job #2023196)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 18 septembrie 2017 15:20:14
Problema Gather Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.75 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
#define maxN 752
#define maxK 16
#define ll unsigned int
using namespace std;
ll sol, val, x, n, i, j, p, m, k, v[maxN], w[maxN];
ll d[maxK][maxN][maxK];
ll config, nb1;
ll dp[1 << maxK][maxK];
struct pq
{
    ll x, c;
    bool operator < (const pq & e) const
    {
        return c > e.c;
    }
} el;
priority_queue < pq > H;
struct edge
{
    ll x;
    ll y;
    ll c;
    ll d;
} e;
vector < edge > V[maxN];
int bitc(ll 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);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}