Cod sursa(job #2538925)

Utilizator rd211Dinucu David rd211 Data 5 februarie 2020 12:48:29
Problema Gather Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("gather.in");
ofstream fout("gather.out");
const int NMAX = 760;
struct coridor
{
    int ending;
    int lungime, capacitate;
};
vector<coridor> graf[NMAX];
int k, n, m;
long long dp[NMAX][1<<18]; //[coridor][folositi]
int dpBiti[1<<18];
long long res = INT_MAX;
int nodes[NMAX];
//nefolosit = 0, folosit = 1
bool checkIfUsed(int node, int used)
{
    int place = nodes[node]-1;
    if(place==-1) return false;
    if((used^(1<<(place)))<used) return false;
    return true;
}
bool secondCheck(int node, int used)
{
    if((used^node)<used) return true;
    return false;
}
int addUsed(int node, int used)
{
    int place = nodes[node]-1;
    used+=(1<<(place));
    return used;
}
int countBits(int i)
{
     i = i - ((i >> 1) & 0x55555555);
     i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
     return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
void calc(int node, int used)
{
    if(checkIfUsed(node, used) && countBits(used)==k-1)
        res = min(res, dp[node][used]);
    for(coridor cor: graf[node])
    {
        if(cor.capacitate>=countBits(used))
        {
            if(cor.capacitate>=countBits(used)+1){
                if(checkIfUsed(node, used))
                {
                    if(dp[cor.ending][addUsed(node, used)]>(long long)dp[node][used]+(long long)cor.lungime*(countBits(used)+2))
                    {
                        dp[cor.ending][addUsed(node, used)] = (long long)dp[node][used]+(long long)cor.lungime*(countBits(used)+2);
                        calc(cor.ending, addUsed(node, used));
                    }
                }
            }
            if(dp[cor.ending][used]>(long long)dp[node][used]+cor.lungime*(countBits(used)+1))
            {
                dp[cor.ending][used] = (long long)dp[node][used]+(long long)cor.lungime*(countBits(used)+1);
                calc(cor.ending, used);
            }
        }
    }
}
int main()
{
    fin>>k>>n>>m;
    for(int i = 0; i<k; i++)
    {
        int x;
        fin>>x;
        nodes[x]=i+1;
    }
    for(int i = 0; i<m; i++)
    {
        int a, b, c, d;
        fin>>a>>b>>c>>d;
        graf[a].push_back({b,c,d});
        graf[b].push_back({a,c,d});
    }
    for(int i = 1; i<=n; i++)
        fill(dp[i],dp[i]+NMAX,INT_MAX);

    dp[1][0] = 0;
    calc(1,0);
    fout<<res;
    return 0;
}