Cod sursa(job #2641627)

Utilizator Iulia14iulia slanina Iulia14 Data 12 august 2020 11:02:08
Problema Gather Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.79 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
ifstream cin ("gather.in");
ofstream cout ("gather.out");
long long dist[20][20][20];
long long dp[1 << 16][16];
long long vc[800];
long long det[20];
long long viz[800];
struct ura{
    long long d, ind;
    bool operator < (const ura &other) const{
        return d > other.d;
    }
};
priority_queue <ura> q;
struct ura2{
    long long c, x, nrdet;
};
vector <ura2> lista[755];
long long biti[1 << 16];
int main()
{
    int k, n, m, i, j;
    cin >> k >> n >> m;
    det[0] = 1;
    for (i = 1; i <= k; i++)
    {
        cin >> det[i];
        vc[det[i]] = i;
    }
    for (i = 1; i <= m; i++)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        lista[a].push_back({c, b, d});
        lista[b].push_back({c, a, d});
    }
    for (int tot = 0; tot <= k; tot++)
        for (i = 0; i <= k; i++)
        {
            for (j = 1; j <= k; j++)
            {
                dist[i][j][tot] = 1e17;
            }
            for (j = 1; j <= n; j++)
                viz[j] = 0;
            q.push({0, det[i]});
            while (!q.empty())
            {
                long long d = q.top().d;
                int ind = q.top().ind;
                q.pop();
                if (viz[ind] == 0)
                {
                    viz[ind] = 1;
                    if (vc[ind])
                        dist[i][vc[ind]][tot] = d;
                    for (int h = 0; h < lista[ind].size(); h++)
                    {
                        if (viz[lista[ind][h].x] == 0 && lista[ind][h].nrdet >= tot)
                            q.push({d + lista[ind][h].c, lista[ind][h].x});
                    }
                }

            }
        }

    for (i = 1; i < (1 << (k + 1)); i++)
        for (j = 0; j < k + 1; j++)
            if (i & (1 << j))
            {
                biti[i] = biti[i ^ (1 << j)] + 1;
                j = k + 1;
            }
    for (i = 1; i < (1 << (k + 1)); i++)
        for (j = 1; j <= k; j++)
            dp[i][j] = 1e17;
    for (j = 1; j <= k; j++)
        dp[1 + (1 << j)][j] = dist[0][j][0];
    for (i = 3; i < (1 << (k + 1)); i += 2)
        for (j = 1; j <= k; j++)
            if (i & (1 << j))
                for (int h = 1; h <= k; h++)
                    if ((i & (1 << h)) && j != h)
                        dp[i][j] = min(dp[i][j], dp[i ^ (1 << j)][h] + dist[h][j][biti[i ^ (1 << j)] - 1] * (biti[i ^ (1 << j)]));
long long    sol = 1e17;
   /* for (i = 3; i < (1 << (k + 1)); i += 2)
    {
        for (j = 1; j <= k; j++)
            cout << dp[i][j] << " ";
        cout << endl;
    }*/
    for (i = 1; i <= k; i++)
        sol = min(sol, dp[(1 << (k + 1)) - 1][i]);
    cout << sol;
    return 0;
}