题目连接:http://ace.delos.com/usacoprob2?a=wMuZi94AAjz&S=shopping
题目大意:有一个超市搞优惠活动。有s种优惠,每一种优惠 i (0<= i < s )都包含ni (1<=ni<=5)个商品 , 商品ci有kij(0<=j<ni,1<=kij<=5)个,优惠后的价格为price[i](price[i] 小于这些商品常规价的和)。求买b种商品,每个商品num[i]个的最低价格。注意,不能多买,虽然有时候多买会更便宜。坑爹= =!
思路:五维dp,代码巨丑 = =!官方貌似转化成图用dijkstra+heap优化搞的,好神奇=。= 有时间研究研究。
状态方程:
temp_p=price[i]+ans[n0-offer_n[i][0]][n1-offer_n[i][1]][n2-offer_n[i][2]][n3-offer_n[i][3]][n4-offer_n[i][4]];
ans[n0][n1][n2][n3][n4]=min(ans[n0][n1][n2][n3][n4],temp_p);
感觉输入的信息好多,输入看了好几遍才把输入信息稿清楚。= =
代码:
/*
ID: czq1992
LANG: C++
TASK: shopping
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int s,offer_c[100][1000],offer_k[100][6],price[100],offer_n[100][6],offer_num[100];
int b,code[6],num[6],p[6];
int temp[6];
int ans[6][6][6][6][6];
bool check(int n0,int n1,int n2,int n3,int n4,int t)
{
if(n0<offer_n[t][0] || n1<offer_n[t][1] || n2<offer_n[t][2] || n3<offer_n[t][3] || n4<offer_n[t][4]) return 0;
else return 1;
}
void solve()
{
int n0,n1,n3,n4,n2,temp_p;
int i,k,j;
for(n0=0;n0<=num[0];n0++)
for(n1=0;n1<=num[1];n1++)
for(n2=0;n2<=num[2];n2++)
for(n3=0;n3<=num[3];n3++)
for(n4=0;n4<=num[4];n4++)
ans[n0][n1][n2][n3][n4]=n0*p[0]+n1*p[1]+n2*p[2]+n3*p[3]+n4*p[4];//不优惠的价格
for(i=0;i<s;i++)
for(n0=0;n0<=num[0];n0++)
for(n1=0;n1<=num[1];n1++)
for(n2=0;n2<=num[2];n2++)
for(n3=0;n3<=num[3];n3++)
for(n4=0;n4<=num[4];n4++)
{
if(check(n0,n1,n2,n3,n4,i))
{
temp_p=price[i]+ans[n0-offer_n[i][0]][n1-offer_n[i][1]][n2-offer_n[i][2]][n3-offer_n[i][3]][n4-offer_n[i][4]];
ans[n0][n1][n2][n3][n4]=min(ans[n0][n1][n2][n3][n4],temp_p);
}
}
cout<<ans[num[0]][num[1]][num[2]][num[3]][num[4]]<<endl;
}
int main()
{
int i,j,k,n;
freopen("shopping.in","r",stdin);
freopen("shopping.out","w",stdout);
cin>>s;
for(i=0;i<s;i++)
{
cin>>offer_num[i];//每种优惠包含商品的种类
for(j=0;j<offer_num[i];j++) cin>>offer_c[i][j]>>offer_k[i][j];//每种商品的编号和数量
cin>>price[i];//每种优惠的价格
}
cin>>b;//要买的商品种类
for(i=0;i<b;i++)cin>>code[i]>>num[i]>>p[i];//每种商品的编号,数量,常规价格
for(i=0;i<s;i++)
{
memset(temp,0,sizeof(temp));
for(j=0;j<offer_num[i];j++)
{
for(k=0;k<b;k++)
{
if(offer_c[i][j] == code[k]) {temp[k]=offer_k[i][j];break;}
}
}
for(j=0;j<5;j++) offer_n[i][j]=temp[j];//
}
solve();
return 0;
}
/*
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 3660 KB]
Test 2: TEST OK [0.000 secs, 3660 KB]
Test 3: TEST OK [0.000 secs, 3660 KB]
Test 4: TEST OK [0.000 secs, 3660 KB]
Test 5: TEST OK [0.000 secs, 3660 KB]
Test 6: TEST OK [0.000 secs, 3660 KB]
Test 7: TEST OK [0.000 secs, 3660 KB]
Test 8: TEST OK [0.000 secs, 3660 KB]
Test 9: TEST OK [0.000 secs, 3660 KB]
Test 10: TEST OK [0.011 secs, 3660 KB]
Test 11: TEST OK [0.022 secs, 3660 KB]
Test 12: TEST OK [0.011 secs, 3660 KB]
All tests OK.
*/