Pagini recente » Cod sursa (job #2990755) | Cod sursa (job #2777432) | Cod sursa (job #296335) | Cod sursa (job #1717655) | Cod sursa (job #2538921)
#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]
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 used)
{
int counter = 0;
for(int i = 0;i<=k;i++)
{
counter+=secondCheck(1<<i,used);
}
return counter;
}
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;
}