您的位置:首页 > Web前端

USACO Shopping Offers, DP

2012-09-26 08:01 369 查看
无语致死,使用的最原始的五维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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: