Untitled
unknown
plain_text
2 years ago
1.7 kB
8
Indexable
#include<iostream>
using namespace std;
int Component[25] = {0};
int L; // so linh kien can mua
int Sunny[25] ={0};
int N; // so linh kien cho troi
int QuantityCombo[35][25] = {0};
int PriceCombo[35] = {0};
int nComponentCombo[25];
int M; // so goi combo
int VCB[20] = {0}; // danh sach counting sort lk da mua
int minPrime = 10000000;
int price;
void buy(int k)
{
price += PriceCombo[k];
for(int i =0; i < nComponentCombo[k]; i++)
{
VCB[QuantityCombo[k][i]]++;
}
}
void unBuy(int k)
{
price -= PriceCombo[k];
for(int i =0; i < nComponentCombo[k]; i++)
{
VCB[QuantityCombo[k][i]]--;
}
}
void backTrack(int k)
{
if(price > minPrime)
{
return;
}
if(k == M)
{
// tinh price
for(int i =0; i < L; i++)
{
if(VCB[Component[i]] == 0)
{
price += Sunny[Component[i]];
}
}
return;
}
// lua chon cua cac candidate
for(int i = 0; i< 2; i++)
{
if(i == 0)
{
backTrack(k + 1);
}
if(i == 1)
{
buy(k);
backTrack(k + 1);
unBuy(k);
}
}
}
int main()
{
freopen("Text.txt","r",stdin);
int T;
cin>>T;
for(int tc =1; tc < T; tc++)
{
cin >> N;
for(int i =0; i< N; i++) // cin gia linh kien cho troi
{
cin>>Sunny[i];
}
cin>>M; // cin so luong combo
for(int i =0; i< M; i++)
{
cin>>PriceCombo[i];
cin>> nComponentCombo[i];
for(int j = 0; j < nComponentCombo[i]; j++)
{
cin>>QuantityCombo[i][j];
}
}
cin>>L;// so linh kien can mua
for(int i = 0; i < L; i++)
{
cin>>Component[i];
}
// process
backTrack(0);
//cout ans
cout <<"#"<<tc<<" "<<price<<endl;
}
return 0;
}Editor is loading...