Pagini recente » Cod sursa (job #729317) | Cod sursa (job #855226) | Cod sursa (job #1087938) | Cod sursa (job #1714187) | Cod sursa (job #2538895)
#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;
int dp[NMAX][1<<18]; //[coridor][folositi]
int 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;
}
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++)
{
if(used%2==1) counter++;
used/=2;
}
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)]>dp[node][used]+cor.lungime*(countBits(used)+2))
{
dp[cor.ending][addUsed(node, used)] = dp[node][used]+cor.lungime*(countBits(used)+2);
calc(cor.ending, addUsed(node, used));
}
}
}
if(dp[cor.ending][used]>dp[node][used]+cor.lungime*(countBits(used)+1))
{
dp[cor.ending][used] = dp[node][used]+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;
}