無語致死,使用的最原始的五維DP,參考 http://www.cppblog.com/Ylemzy/articles/100170.html
注意count數組裏除了要加入 捆綁打折的商品信息,也要加入商品的單價
/*
ID: wangxin12
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;
#define MAX 6
#define S 106
ifstream fi("shopping.in");
ofstream fo("shopping.out");
int map[MAX][MAX][MAX][MAX][MAX];
int count[S][MAX];
int modity[MAX];
int demand[MAX];
int s, b;
int decode(int code) {
int i;
for( i = 1; i <= 5; i++) {
if(code == modity[i])
return i;
else if(!modity[i]) {
modity[i] = code;
return i;
}
}
return -1;
}
void read() {
fi>>s;
for(int i = 1; i <= s; i++) {
int n, c, k, p;
fi>>n;
for(int j = 1; j <= n; j++) {
fi>>c>>k;
int tmp = decode(c);
count[i][tmp] = k;
}
fi>>p;
count[i][0] = p;
}
fi>>b;
for(int j = 1; j <= b; j++) {
int c, k, p;
fi>>c>>k>>p;
int tmp = decode(c);
demand[tmp] = k;
count[s + j][0] = p;
count[s + j][tmp] = 1;
}
s += b;
}
void dp() {
int a1, a2, a3, a4, a5;
for(a1 = 0; a1 <= demand[1]; a1++)
for(a2 = 0; a2 <= demand[2]; a2++)
for(a3 = 0; a3 <= demand[3]; a3++)
for(a4 = 0; a4 <= demand[4]; a4++)
for(a5 = 0; a5 <= demand[5]; a5++) {
for(int j = 1; j <= s; j++) {
if(a1 >= count[j][1] && a2 >= count[j][2] && a3 >= count[j][3] && a4 >= count[j][4] && a5 >= count[j][5]) {
int tmp = map[a1 - count[j][1]][a2 - count[j][2]][a3 - count[j][3]][a4 - count[j][4]][a5 - count[j][5]] + count[j][0];
if(map[a1][a2][a3][a4][a5] > tmp || !map[a1][a2][a3][a4][a5])
map[a1][a2][a3][a4][a5] = tmp;
}
}
}
}
void output() {
fo<<map[demand[1]][demand[2]][demand[3]][demand[4]][demand[5]]<<endl;
}
int main() {
read();
dp();
output();
fi.close();
fo.close();
return 0;
}
第一次的代碼怎麼都不對,無語死了
/*
ID: wangxin12
PROG: shopping
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;
#define MAX 6
#define Max(a,b) (a > b ? a : b)
#define Min(a,b) (a < b ? a : b)
ifstream fi("in.txt");
ofstream fo("out.txt");
int map[MAX][MAX][MAX][MAX][MAX];
int count[100][1000];
int cost[100];
int demand[100];
int price[100];
int modity[MAX];
int s; //the number of special offers
void read() {
fi>>s;
for(int i = 1; i <= s; i++) {
int num;
fi>>num;
for(int j = 1; j <= num; j++) {
int a, b;
fi>>a>>b;
count[i][a] = b;
}
int cos;
fi>>cos;
cost[i] = cos;
}
int m;
fi>>m;
for(int k = 1; k <= m; k++) {
int x, y, z;
fi>>x>>y>>z;
modity[k] = x;
demand[x] = y;
price[x] = z;
}
}
void dp() {
int a1, a2, a3, a4, a5;
for(a1 = 0; a1 <= demand[modity[1]]; a1++) {
for(a2 = 0; a2 <= demand[modity[2]]; a2++) {
for(a3 = 0; a3 <= demand[modity[3]]; a3++) {
for(a4 = 0; a4 <= demand[modity[4]]; a4++) {
for(a5 = 0; a5 <= demand[modity[5]]; a5++) {
int originalCost = price[modity[1]] * a1 + price[modity[2]] * a2 + price[modity[3]] * a3 + price[modity[4]] * a4 + price[modity[5]] * a5;
for(int i = 1; i <= s; i++) {
if(a1 >= count[i][modity[1]] && a2 >= count[i][modity[2]] && a3 >= count[i][modity[3]] && a4 >= count[i][modity[4]] && a5 >= count[i][modity[5]]) {
int updatedCost = map[a1 - count[i][modity[1]]][a2 - count[i][modity[2]]][a3 - count[i][modity[3]]][a4 - count[i][modity[4]]][a5 - count[i][modity[5]]] + cost[i];
map[a1][a2][a3][a4][a5] = Min(originalCost, updatedCost);
}
}
}
}
}
}
}
}
void OutPut() {
fo<<map[demand[modity[1]]][demand[modity[2]]][demand[modity[3]]][demand[modity[4]]][demand[modity[5]]]<<endl;
//fo<<map[demand[1]][demand[2]][demand[3]][demand[4]][demand[5]]<<endl;
}
int main() {
read();
dp();
OutPut();
fi.close();
fo.close();
return 0;
}