USACO Shopping Offers, DP

无语致死,使用的最原始的五维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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值